简体   繁体   English

Javascript使用下划线基于另一个数组顺序对数组顺序进行排序

[英]Javascript Sort array order based on another array order using underscore

Basically my question in title. 基本上我的标题问题。 I got two arrays of objects. 我得到了两个对象数组。

Array 1: 阵列1:

var firstArray = [
{id: "1", value: 1},
{id: "5", value: 0.5},
{id: "3", value: 0.25},
{id: "4", value: 0.125},
{id: "2", value: 0.0625}
];

Array 2: 阵列2:

var secondArray = [
{id: 1, name: "a"},
{id: 2, name: "b"},
{id: 3, name: "c"},
{id: 4, name: "d"},
{id: 5, name: "e"}
];

How can I sort my second array so that order will match first array order like this: 我如何排序我的第二个数组,以便该顺序将匹配第一个数组的顺序,如下所示:

Result: 结果:

var secondArray = [
{id: 1, name: "a"},
{id: 5, name: "e"},
{id: 3, name: "c"},
{id: 4, name: "d"},
{id: 2, name: "b"}
];

With plain ES6, you could take a Map for the index of the given id and sort it with the index of firstArray . 使用普通ES6,您可以使用Map作为给定id的索引,并使用firstArray的索引对其进行firstArray

A problem is the different type of id in the given arrays. 一个问题是给定数组中id类型不同。

 var firstArray = [{ id: "1", value: 1 }, { id: "5", value: 0.5 }, { id: "3", value: 0.25 }, { id: "4", value: 0.125 }, { id: "2", value: 0.0625 }], secondArray = [{ id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" }, { id: 4, name: "d" }, { id: 5, name: "e" }], map = new Map(firstArray.map(({ id }, i) => [+id, i])); secondArray.sort((a, b) => map.get(a.id) - map.get(b.id)); console.log(secondArray); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Giving an Underscore/Lodash answer since that's what the question title indicates the OP is after. 提供下划线/ Lodash答案,因为这就是问题标题指示操作员遵循的内容。 This code sorts the second array by the indexes of the corresponding id in the first array. 此代码通过第一个数组中相应ID的索引对第二个数组进行排序。

 $(function() { var firstArray = [ {id: "1", value: 1}, {id: "5", value: 0.5}, {id: "3", value: 0.25}, {id: "4", value: 0.125}, {id: "2", value: 0.0625} ]; var secondArray = [ {id: 1, name: "a"}, {id: 2, name: "b"}, {id: 3, name: "c"}, {id: 4, name: "d"}, {id: 5, name: "e"} ]; var sorted = _.sortBy(secondArray, function(obj) { return _.findIndex(firstArray, {id: obj.id.toString()}); }); console.log(sorted); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

XY problem? XY问题?

I sense you have plans to relate the two arrays by index after the second array is sorted. 我感觉到您有计划在对第二个数组进行排序后通过索引将两个数组关联起来。 I suggest an alternative means of relation 我建议另一种联系方式

 const firstArray = [ {id: "1", value: 1} , {id: "5", value: 0.5} , {id: "3", value: 0.25} , {id: "4", value: 0.125} , {id: "2", value: 0.0625} ] const secondArray = [ {id: 1, name: "a"} , {id: 2, name: "b"} , {id: 3, name: "c"} , {id: 4, name: "d"} , {id: 5, name: "e"} ] const thirdArray = firstArray.map (x => Object.assign (x, secondArray.find (y => y.id == x.id))) console.log (thirdArray) // [ { id: 1, value: 1, name: 'a' } // , { id: 5, value: 0.5, name: 'e' } // , { id: 3, value: 0.25, name: 'c' } // , { id: 4, value: 0.125, name: 'd' } // , { id: 2, value: 0.0625, name: 'b' } // ] 

Noting some casting (one array's id is a String, one is a Number), you can do this with ES6 .map() (different from Nina's Map ) and .find() : 注意一些转换(一个数组的id是一个字符串,一个数组是一个数字),您可以使用ES6 .map() (与Nina的Map有所不同)和.find()

 var firstArray = [ {id: "1", value: 1}, {id: "5", value: 0.5}, {id: "3", value: 0.25}, {id: "4", value: 0.125}, {id: "2", value: 0.0625} ]; var secondArray = [ {id: 1, name: "a"}, {id: 2, name: "b"}, {id: 3, name: "c"}, {id: 4, name: "d"}, {id: 5, name: "e"} ]; var result = firstArray.map(item => { return { id: Number(item.id), name: secondArray.find(element => element.id == item.id).name } }) console.log(result) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM