0

Hi I have an array as shown:

var mainArray = ['Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Fernando', 'Gary', 'Harper'];

And I have an array of names I want to search from main array:

var searchArray = ['Fernando', 'Harper'];

I want a new array which should search 'Fernando' and 'Harper' add it to new array and then add the rest of the array to it.

var newArrAfterSearch = ['Fernando', 'Harper', 'Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Gary']

Tried a few things unsuccessfully like:

var newArrAfterSearch = [];
mainArray.forEach(name => 
 var nameFound = searchArray.find(searchName => searchName === name);
 if(nameFound) {
   newArrAfterSearch = [... mainArray, nameFound];
 }
)
Uzair Khan
  • 2,812
  • 7
  • 30
  • 48
  • This might be a duplicate of: https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – N.A. Apr 29 '20 at 11:52

3 Answers3

1

You can use filter and includes, filter out all the values from mainArray which appears in searchArray, then merge searchArray with mainArray

const mainArray = ['Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Fernando', 'Gary', 'Harper'];
const searchArray = ['Fernando', 'Harper'];

const rest = mainArray.filter(v => !searchArray.includes(v))
console.log([...searchArray, ...rest])

If your searchArray has values which are not available in mainArray, while filtering values from mainArray we can keep track of occurred values and while merging we can filter searchArray as well

const mainArray = ['Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Fernando', 'Gary', 'Harper'];
const searchArray = ['Fernando', 'Harper', 'something random'];

const mapper = Object.create(null)
const rest = mainArray.filter(v =>{
 mapper[v] = true
 return !searchArray.includes(v)
})
console.log([...searchArray.filter(v=> mapper[v]), ...rest])
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
1

You can create a partition function using Array.reduce(), and then flatten the results with Array.flat().

The partition() function takes a predicate, and returns an array with 2 sub arrays. Any item that the predicate returned true form would be pushed to the 1st sub array (index 0), and all others to the 2nd sub array (index 1).

const mainArray = ['Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Fernando', 'Gary', 'Harper'];
const searchArray = ['Fernando', 'Harper'];

const partition = (predicate, arr) =>
  arr.reduce((r, s) => {   
    r[predicate(s) ? 0 : 1].push(s);
    
    return r;
  }, [[], []]);

const result = partition(s => searchArray.includes(s), mainArray)
  .flat();
  
console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

I think forEach to iterate the search array, splice to delete the item, and unshift to add it again at the beginning will be straight forward:

var mainArray = ['Adam', 'Bailey', 'Cathy', 'Dave', 'Earl', 'Fernando', 'Gary', 'Harper'];
var searchArray = ['Fernando', 'Harper'];
searchArray.reverse().forEach(v => {
  var index = mainArray.indexOf(v);
  if(-1 !== index){
    mainArray.splice(index, 1);
    mainArray.unshift(v);
  }
})
console.log(mainArray);
Chanvin
  • 181
  • 9