Skip to content
Advertisement

Sort array of objects based on another array of objects key

I have 2 arrays of objects

JavaScript

I need to sort array2 to match the same order of array1 based on the property name. In array2 there are 2 names properties equal with value B. These name B are together but i need the array to be in the exact order of array1. In array1 the first B is at index 0 and the second B is at index 3.

I made this attempts without luck.

JavaScript

Advertisement

Answer

You can try this, it finds the first elem in array2 matching the name, in order, from the array1.. removes it from array2 and then adds it to the sortedArray2.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement