在对象数组中对对象数组中的值进行排序

时间:2019-09-07 18:29:20

标签: javascript arrays filter

我想对所有itemName进行排序,并将所有适当的数量加在一起,以便能够确定订购最多的项目。

这是下面的对象数组。我想尝试过滤并映射对象数组,并列出itemName和每个对象的适当总数量,只是不确定如何链接在一起。

Figure

提供的对象数组

function OrderRepository() {

return orderLines.filter((itemName) => {
   orderLines.map((quantity) => {
     return 
  }
 }
}

所需结果

OrderRepository.prototype.getYesterdaysOrders = function getYesterdaysOrders() { var yesterdaysOrders = [ { id: 1, orderLines: [ { itemName: "Item 01", quantity: 1 }, { itemName: "Item 02", quantity: 3 }, { itemName: "Item 03", quantity: 25 }, { itemName: "Item 04", quantity: 12 }, ], }, { id: 2, orderLines: [ { itemName: "Item 01", quantity: 1 }, { itemName: "Item 08", quantity: 42 }, { itemName: "Item 09", quantity: 13 }, { itemName: "Item 12", quantity: 37 }, ], }, { id: 3, orderLines: [ { itemName: "Item 12", quantity: 16 }, ], }, { id: 4, orderLines: [ { itemName: "Item 10", quantity: 11 }, { itemName: "Item 11", quantity: 10 }, ], }, { id: 5, orderLines: [ { itemName: "Item 06", quantity: 7 }, { itemName: "Item 07", quantity: 2 }, { itemName: "Item 12", quantity: 14 }, ], }, { id: 6, orderLines: [ { itemName: "Item 05", quantity: 17 }, ], }, { id: 7, orderLines: [ { itemName: "Item 03", quantity: 5 }, { itemName: "Item 07", quantity: 2 }, ], }, { id: 8, orderLines: [ { itemName: "Item 02", quantity: 13 }, { itemName: "Item 07", quantity: 7 }, { itemName: "Item 09", quantity: 2 }, ], }, { id: 9, orderLines: [ { itemName: "Item 01", quantity: 4 }, { itemName: "Item 06", quantity: 17 }, { itemName: "Item 07", quantity: 3 }, ], }, { id: 10, orderLines: [ { itemName: "Item 11", quantity: 12 }, { itemName: "Item 12", quantity: 1 }, ], }, ]; return yesterdaysOrders; }; { itemName: "Item 01", quantity: 6 }

我想按商品名称列出该商品的总数量。因此,昨天订购商品01的总次数为6。

3 个答案:

答案 0 :(得分:2)

您可以借助Map对项目进行计数,并将所需结果呈现为数组。

var data = [{ id: 1, orderLines: [{ itemName: "Item 01", quantity: 1 }, { itemName: "Item 02", quantity: 3 }, { itemName: "Item 03", quantity: 25 }, { itemName: "Item 04", quantity: 12 }] }, { id: 2, orderLines: [{ itemName: "Item 01", quantity: 1 }, { itemName: "Item 08", quantity: 42 }, { itemName: "Item 09", quantity: 13 }, { itemName: "Item 12", quantity: 37 }] }, { id: 3, orderLines: [{ itemName: "Item 12", quantity: 16 }] }, { id: 4, orderLines: [{ itemName: "Item 10", quantity: 11 }, { itemName: "Item 11", quantity: 10 }] }, { id: 5, orderLines: [{ itemName: "Item 06", quantity: 7 }, { itemName: "Item 07", quantity: 2 }, { itemName: "Item 12", quantity: 14 }] }, { id: 6, orderLines: [{ itemName: "Item 05", quantity: 17 }] }, { id: 7, orderLines: [{ itemName: "Item 03", quantity: 5 }, { itemName: "Item 07", quantity: 2 }] }, { id: 8, orderLines: [{ itemName: "Item 02", quantity: 13 }, { itemName: "Item 07", quantity: 7 }, { itemName: "Item 09", quantity: 2 }] }, { id: 9, orderLines: [{ itemName: "Item 01", quantity: 4 }, { itemName: "Item 06", quantity: 17 }, { itemName: "Item 07", quantity: 3 }] }, { id: 10, orderLines: [{ itemName: "Item 11", quantity: 12 }, { itemName: "Item 12", quantity: 1 }] }],
    result = Array.from(
        data.reduce((m, { orderLines }) => {
            orderLines.forEach(({ itemName, quantity }) => m.set(itemName, (m.get(itemName) || 0) + quantity));
            return m;
        }, new Map),
        ([itemName, quantity]) => ({ itemName, quantity })
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

这是您要寻找的:

var hashmap = {}
for(var each of yesterdaysOrders) {
    for(var one of each['orderLines']) {
        if (one['itemName'] in hashmap) {
            hashmap[one['itemName']] += one['quantity']
        } else {
            hashmap[one['itemName']] = one['quantity']
        }
    }
}

答案 2 :(得分:0)

您可以使用JavaScript的reduce()方法来做到这一点。

let result = yesterdaysOrders.reduce((arr, currentValue) => {
  currentValue.orderLines.forEach(order => {
    const index = arr.findIndex(item => item.itemName === order.itemName);
    if (index === -1) {
      arr.push(order);
    } else {
        arr[index].quantity += order.quantity;
    } 
  });

  return arr;
}, []);

然后使用Array.sort()对结果数组进行排序。

// Sorting on item name in ascending order.
result.sort((a, b) => {
    if (a.itemName < b.itemName) {
      return -1;
    } else if (a.itemName > b.itemName) {
        return 1;
    } else {
        return 0;
    }
});

演示:

var yesterdaysOrders = [
    {
        id: 1,
        orderLines: [
            { itemName: "Item 01", quantity: 1 },
            { itemName: "Item 02", quantity: 3 },
            { itemName: "Item 03", quantity: 25 },
            { itemName: "Item 04", quantity: 12 },
        ],
    },
    {
        id: 2,
        orderLines: [
            { itemName: "Item 01", quantity: 1 },
            { itemName: "Item 08", quantity: 42 },
            { itemName: "Item 09", quantity: 13 },
            { itemName: "Item 12", quantity: 37 },
        ],
    },
    {
        id: 3,
        orderLines: [
            { itemName: "Item 12", quantity: 16 },
        ],
    },
    {
        id: 4,
        orderLines: [
            { itemName: "Item 10", quantity: 11 },
            { itemName: "Item 11", quantity: 10 },
        ],
    },
    {
        id: 5,
        orderLines: [
            { itemName: "Item 06", quantity: 7 },
            { itemName: "Item 07", quantity: 2 },
            { itemName: "Item 12", quantity: 14 },
        ],
    },
    {
        id: 6,
        orderLines: [
            { itemName: "Item 05", quantity: 17 },
        ],
    },
    {
        id: 7,
        orderLines: [
            { itemName: "Item 03", quantity: 5 },
            { itemName: "Item 07", quantity: 2 },
        ],
    },
    {
        id: 8,
        orderLines: [
            { itemName: "Item 02", quantity: 13 },
            { itemName: "Item 07", quantity: 7 },
            { itemName: "Item 09", quantity: 2 },
        ],
    },
    {
        id: 9,
        orderLines: [
            { itemName: "Item 01", quantity: 4 },
            { itemName: "Item 06", quantity: 17 },
            { itemName: "Item 07", quantity: 3 },
        ],
    },
    {
        id: 10,
        orderLines: [
            { itemName: "Item 11", quantity: 12 },
            { itemName: "Item 12", quantity: 1 },
        ],
    },
 ];


let result = yesterdaysOrders.reduce((arr, currentValue) => {
  currentValue.orderLines.forEach(order => {
    const index = arr.findIndex(item => item.itemName === order.itemName);
    if (index === -1) {
    arr.push(order);
  } else {
    arr[index].quantity += order.quantity;
  } 
  });
  
  return arr;
}, []);

// Sorting on item name in ascending order.
result.sort((a, b) => {
    if (a.itemName < b.itemName) {
      return -1;
    } else if (a.itemName > b.itemName) {
        return 1;
    } else {
        return 0;
    }
});

console.log(result);