简体   繁体   English

从对象中排序两个数组,具体取决于Javascript中的那些数组之一

[英]Sort two arrays from object depending on one of those arrays in Javascript

I have an array like this: 我有一个像这样的数组:

var arr = [
   {a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5]},
   {a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3]},
   {a: 3, b: ['apple', 'banana', 'orange', 'mango'], c: [14, 10, 5, 12]},
   {a: 4, b: ['apple', 'banana', 'orange', 'mango'], c: [99, 2, 105, 101]}
];

I need to sort both arr[x].b and arr[x].c depending on arr[x].c values, if that makes sense. 如果需要的话,我需要根据arr[x].c值对arr[x].barr[x].c进行排序。 So, the final array should look like this: 因此,最终数组应如下所示:

arr = [
   {a: 1, b: ['mango', 'banana', 'orange', 'apple'], c: [5, 7, 18, 42]},
   {a: 2, b: ['mango', 'apple', 'orange', 'banana'], c: [3, 4, 88, 101]},
   {a: 3, b: ['orange', 'banana', 'mango', 'apple'], c: [5, 10, 12, 14]},
   {a: 4, b: ['banana', 'apple', 'mango', 'orange'], c: [2, 99, 101, 105]}
];

For each object, construct a Map whose keys are fruit strings and values are the associated original number in the c array. 对于每个对象,构造一个Map其键是水果字符串,值是c数组中关联的原始数字。 Sort the c array, then sort the b array based on the difference between the items in the map: c数组进行排序,然后根据地图中各项之间的差异对b数组进行排序:

 const arr = [ {a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5]}, {a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3]}, {a: 3, b: ['apple', 'banana', 'orange', 'mango'], c: [14, 10, 5, 12]}, {a: 4, b: ['apple', 'banana', 'orange', 'mango'], c: [99, 2, 105, 101]} ]; Object.values(arr).forEach(({ b, c }) => { const fruitValues = c.reduce( (map, num, i) => map.set(b[i], num), new Map() ); c.sort((a, b) => a - b); b.sort((a, b) => fruitValues.get(a) - fruitValues.get(b)); }); console.log(arr); 

You can zip elements of b and c together, sort them by the c element, then unpack them back. 您可以将bc元素压缩在一起,按c元素对其进行排序,然后将其解压缩。 With this method, only one sort is necessary, even if you are sorting multiple lists. 使用此方法,即使要对多个列表进行排序,也仅需要一种sort

 const arr = [ {a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5]}, {a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3]}, {a: 3, b: ['apple', 'banana', 'orange', 'mango'], c: [14, 10, 5, 12]}, {a: 4, b: ['apple', 'banana', 'orange', 'mango'], c: [99, 2, 105, 101]} ]; Object.values(arr).forEach(row => { let sorting = row.b.map((e, i, a) => [e, row.c[i]]); sorting.sort(([b1, c1], [b2, c2]) => c1 - c2); row.b = sorting.map(([e, ]) => e); row.c = sorting.map(([, e]) => e); }); console.log(arr); 

You could take the indices of the master array for sorting, sort them by the values of it and map all wanted array with a new order. 您可以对主数组的索引进行排序,按其值对它们进行排序,然后以新顺序映射所有需要的数组。

 var array = [{ a: 1, b: ['apple', 'banana', 'orange', 'mango'], c: [42, 7, 18, 5] }, { a: 2, b: ['apple', 'banana', 'orange', 'mango'], c: [4, 101, 88, 3] }, { a: 3, b: ['apple', 'banana', 'orange', 'mango'], c: [14, 10, 5, 12] }, { a: 4, b: ['apple', 'banana', 'orange', 'mango'], c: [99, 2, 105, 101] }]; array.forEach(o => { var indices = [...ockeys()].sort((a, b) => oc[a] - oc[b]); ['b', 'c'].forEach(k => o[k] = indices.map(i => o[k][i])); }); console.log(array); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

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