Javascript - sort array based on another array

后端 未结 22 1482
鱼传尺愫
鱼传尺愫 2020-11-22 03:45

Is it possible to sort and rearrange an array that looks like this:

itemsArray = [ 
    [\'Anne\', \'a\'],
    [\'Bob\', \'b\'],
    [\'Henry\', \'b\'],
             


        
22条回答
  •  一生所求
    2020-11-22 04:29

    ES6

    const arrayMap = itemsArray.reduce(
      (accumulator, currentValue) => ({
        ...accumulator,
        [currentValue[1]]: currentValue,
      }),
      {}
    );
    const result = sortingArr.map(key => arrayMap[key]);
    

    More examples with different input arrays

提交回复
热议问题