Javascript - sort array based on another array

后端 未结 22 1801
鱼传尺愫
鱼传尺愫 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:06

    You could try this method.

    const sortListByRanking = (rankingList, listToSort) => {
      let result = []
    
      for (let id of rankingList) {
        for (let item of listToSort) {
          if (item && item[1] === id) {
            result.push(item)
          }
        }
      }
    
      return result
    }
    

提交回复
热议问题