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];
 }
)
1

3 Answers 3

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])

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.