
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Modify an array based on another array JavaScript
Suppose, we have a reference array of phrases like this −
const reference = ["your", "majesty", "they", "are", "ready"];
And we are required to join some of the elements of above array based on another array so if another array is this −
const another = ["your", "they are"];
The result would be like −
result = ["your", "majesty", "they are", "ready"];
Here, we compared the elements in both the arrays, we joined the elements of the first array if they existed together in the second array.
We are required to write a JavaScript function that takes in two such arrays and returns a new joined array.
Example
const reference = ["your", "majesty", "they", "are", "ready"]; const another = ["your", "they are"]; const joinByReference = (reference = [], another = []) => { const res = []; const filtered = another.filter(a => a.split(" ").length > 1); while(filtered.length) { let anoWords = filtered.shift(); let len = anoWords.split(" ").length; while(reference.length>len) { let refWords = reference.slice(0,len).join(" "); if (refWords == anoWords) { res.push(refWords); reference = reference.slice(len,reference.length); break; }; res.push(reference.shift()); }; }; return [...res, ...reference]; }; console.log(joinByReference(reference, another));
Output
This will produce the following output −
[ 'your', 'majesty', 'they are', 'ready' ]
- Related Articles
- Sorting Array based on another array JavaScript
- Sort array based on another array in JavaScript
- Filter array based on another array in JavaScript
- Order an array of words based on another array of words JavaScript
- Creating an array of objects based on another array of objects JavaScript
- Filter an array containing objects based on another array containing objects in JavaScript
- Sort object array based on another array of keys - JavaScript
- Get range of months from array based on another array JavaScript
- Filter an object based on an array JavaScript
- Shuffling string based on an array in JavaScript
- Splitting an array based on its first value - JavaScript
- Shifting string letters based on an array in JavaScript
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- Compute the truth value of an array XOR another array element-wise based on conditions in Numpy
- Build maximum array based on a 2-D array - JavaScript

Advertisements