Answer the question
In order to leave comments, you need to log in
How to sort an array so that the elements are in the same order as in another array?
There is an array:
You need to arrange its elements in the same order as in this array:
And get the output:
["290", "999", "222", "333", "987", "309", "666"]
["999", "222", "666"]
["999", "222", "666", "290", "333", "987", "309"]
Answer the question
In order to leave comments, you need to log in
let allElems = ["290", "999", "222", "333", "987", "309", "666"];
let pattern = ["999", "222", "666"];
let goodElems = [];
let badElems = [];
for (let elem of allElems){
pattern.includes(elem) ? goodElems.push(elem) : badElems.push(elem);
}
let filteredElems = goodElems.concat(badElems);
console.log(filteredElems);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question