本文介绍了基于另一个数组的javascript排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个全局数组
var g = [ "jack", "queen", "king", "10", "ace","7", "8", "9"];
和我的数组
var my = ["9","king","7","ace"];
此数组将根据全局数组 g $ c排序$ c>;
This array will be sorted according to the global array g
;
如果我对数组进行排序,输出将是
if I sort my array the output will be
["king","ace","7","9"]
我玩过很多数组但无法实现这一点。请帮我解决这个问题。提前致谢。
I played a lot with arrays but can't accomplish this. Please help me sorting out this issue. Thanks in advance.
推荐答案
一种可能的方式:
var g = ['jack', 'queen', 'king', '10', 'ace', '7', '8', '9'];
var my = ['9', 'king', '7', 'ace'];
my.sort(function(a, b) {
return g.indexOf(a) - g.indexOf(b);
});
console.log( my );
这篇关于基于另一个数组的javascript排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!