繁体   English   中英

如何根据另一个对象对数组进行排序?

[英]How to sort an array of objects based on another?

我有这些 arrays:

阵列 1:

const arr1 = [
  {_id: "13", qtde: 1, color: "red", size: "100x100"},
  {_id: "16", qtde: 5, color: "green", size: "200x200"},
  {_id: "1", qtde: 3, color: "yellow", size: "300x300"},
  {_id: "23", qtde: 2, color: "purple", size: "500x500"},
  {_id: "3", qtde: 0, color: "orange", size: "200x200"}
]

阵列 2:

const arr2 = [
  {_id: "23", name: "Produto-23", price: "48.99"},
  {_id: "13", name: "Produto-13", price: "58.99"},
  {_id: "3", name: "Produto-3", price: "58.99"},
  {_id: "1", name: "Produto-1", price: "58.99"},
  {_id: "16", name: "Produto-16", price: "58.99"}
]

预期 Output:

const arr2 = [
  {_id: "13", name: "Produto-13", price: "58.99"},
  {_id: "16", name: "Produto-16", price: "58.99"},
  {_id: "1", name: "Produto-1", price: "58.99"},
  {_id: "23", name: "Produto-23", price: "48.99"},
  {_id: "3", name: "Produto-3", price: "58.99"}
]

可以看出它们是不同的,但它们具有共同的属性_id ,但顺序不同。 我想要做的是根据其属性_id重新组织第二个数组以与第一个数组具有相同的顺序。

为了清楚起见,我不想合并 arrays 或更改除第二个顺序之外的任何内容。

我考虑过使用Array.sort ,但是由于我没有引用属性,所以我不知道如何在这里应用它。 基本思想是使用第一个数组的索引作为参考,因为它不会改变,但我不知道如何。

我怎么能那样做?

您可以在sort中使用.findIndex ,如下所示:

 const arr1 = [ {_id: "13", qtde: 1, color: "red", size: "100x100"}, {_id: "16", qtde: 5, color: "green", size: "200x200"}, {_id: "1", qtde: 3, color: "yellow", size: "300x300"}, {_id: "23", qtde: 2, color: "purple", size: "500x500"}, {_id: "3", qtde: 0, color: "orange", size: "200x200"} ] const arr2 = [ {_id: "23", name: "Produto-23", price: "48.99"}, {_id: "13", name: "Produto-13", price: "58.99"}, {_id: "3", name: "Produto-3", price: "58.99"}, {_id: "1", name: "Produto-1", price: "58.99"}, {_id: "16", name: "Produto-16", price: "58.99"} ] const arr2Sorted = arr2.sort((a,b) => { const indexOfA = arr1.findIndex(e => e._id === a._id); const indexOfB = arr1.findIndex(e => e._id === b._id); return indexOfA - indexOfB; }); console.log(arr2Sorted);

您需要从第一个数组创建查找 map,然后使用查找 map 对另一个数组进行排序。

 const createLookup = (arr, key) => arr.reduce((acc, curr, index) => ({...acc, [curr[key]]: index }), {}); const sortWithLookup = (arr, key, lookup) => arr.sort((a, b) => lookup[a[key]] - lookup[b[key]]); const arr1 = [ { _id: "13", qtde: 1, color: "red", size: "100x100" }, { _id: "16", qtde: 5, color: "green", size: "200x200" }, { _id: "1", qtde: 3, color: "yellow", size: "300x300" }, { _id: "23", qtde: 2, color: "purple", size: "500x500" }, { _id: "3", qtde: 0, color: "orange", size: "200x200" } ]; const arr2 = [ { _id: "23", name: "Produto-23", price: "48.99" }, { _id: "13", name: "Produto-13", price: "58.99" }, { _id: "3", name: "Produto-3", price: "58.99" }, { _id: "1", name: "Produto-1", price: "58.99" }, { _id: "16", name: "Produto-16", price: "58.99" } ]; const lookup = createLookup(arr1, '_id'); const sorted = sortWithLookup(arr2, '_id', lookup); sorted.forEach(item => console.log(JSON.stringify(item)));
 .as-console-wrapper { top: 0; max-height: 100%;important; }

暂无
暂无

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

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