Hello.

For example, there is an array

['Маша','Паша','Даша'] 

and an array of objects

 [{id :1, name : 'Даша'},{id :2, name : 'Маша'},{id :3, name : 'Паша'}]. 

Ie, in each object of the array there is a property name with a value that is 100% in a regular array of strings.

It is necessary to sort the objects in the array by the name property so that they would be in the same order as the names in the regular array of strings, this is how it should be output:

  [{id :2, name : 'Маша'}, {id :3, name : 'Паша'}, {id :1, name : 'Даша'}] 

Thank you in advance.

    1 answer 1

    1. Assign a weight to each name. For example: Masha - 0, Pasha - 1, Dasha - 2.
    2. Sort by this topic.

    Something like this:

     'use strict'; const names = ['Маша', 'Паша', 'Даша']; const indexes = {}; const data = [{id: 1, name: 'Даша'}, {id: 2, name: 'Маша'}, {id: 3, name: 'Паша'}]; for (let i = 0; i < names.length; ++i) { indexes[names[i]] = i; } data.sort((a, b) => indexes[a.name] - indexes[b.name]); 

    Now in the data array will be:

     [ { id: 2, name: 'Маша' }, { id: 3, name: 'Паша' }, { id: 1, name: 'Даша' } ] 
    • 2
      You can use reduce for convolution: indexes = names.reduce((acc,el,i)=>(acc[el]=i, acc),{}) - Grundy