Javascript - sort array based on another array

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

    Use the $.inArray() method from jQuery. You then could do something like this

    var sortingArr = [ 'b', 'c', 'b', 'b', 'c', 'd' ];
    var newSortedArray = new Array();
    
    for(var i=sortingArr.length; i--;) {
     var foundIn = $.inArray(sortingArr[i], itemsArray);
     newSortedArray.push(itemsArray[foundIn]);
    }
    

提交回复
热议问题