javascript如何按嵌套对象属性在顶层对对象数组进行排序

b4lqfgs4  于 2021-09-23  发布在  Java
关注(0)|答案(2)|浏览(361)
var originalArray = [
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    },
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    }
];

例如,在上面的数组中, inventoryqt 是嵌套键,其值为对象数组。 Store2{ id: "something", time: 323 } 这是一个比任何项目都要高的时间 Store1 排序之后, Store2 将是列表中的第一个,并且 Store1 将处于第二位置,依此类推,时间递减
我的代码无法对商店进行排序。
它不应该对inventory&qt中的嵌套数组进行排序,而应该按时间降序对存储顺序进行排序

const sorted = originalArray
  .map(store => store.inventory
  .map(inv => inv.qt
  .map(item => Object.entries(item)[1])))
  .sort((a, b) => b[1].time - a[1].time)
  .map(item => item[1])

console.log(JSON.stringify(sorted));

预期产量

// sorted array
[
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    },
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    }
]
gcmastyq

gcmastyq1#

这应该起作用:

const highestTime = (inventory) => {
  let max = 0;
  for (const item of inventory) {
    for (const obj of item.qt) {
      if (obj.time > max) max = obj.time;
    }
  }
  return max;
};

const sortedArray = originalArray.sort((a, b) => highestTime(b.inventory) - highestTime(a.inventory));
q43xntqr

q43xntqr2#

这不是最有效的方法,但它确实有效

var originalArray = [
    {
        name: 'Store1',
        inventory: [
            { name: 'Oranges', qt: [{ id: "something", time: 11 }, { id: "something", time: 44 }, { id: "something", time: 53 }] },
            { name: 'Mango', qt: [{ id: "something", time: 3 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Kiwi', qt: [{ id: "something", time: 2 }, { id: "something", time: 91 }, { id: "something", time: 3 }] },
            { name: 'Papaya', qt: [{ id: "something", time: 8 }, { id: "something", time: 91 }, { id: "something", time: 3 }] }
        ]
    },
    {
        name: 'Store2',
        inventory: [
            { name: 'Pizza', qt: [{ id: "something", time: 31 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'Burger', qt: [{ id: "something", time: 1 }, { id: "something", time: 11 }, { id: "something", time: 23 }] },
            { name: 'IceCream', qt: [{ id: "something", time: 111 }, { id: "something", time: 11 }, { id: "something", time: 323 }] }
        ]
    }
];
const sorted = originalArray.slice().sort((a, b) => {
    maxA = Math.max(...a.inventory.flatMap(({qt})=>qt.map(({time})=>time)));
    maxB = Math.max(...b.inventory.flatMap(({qt})=>qt.map(({time})=>time)));
    return maxB-maxA
});
console.log(sorted);

相关问题