简体   繁体   中英

how can I uniques my object where has the same id and add the amount + 1?

I have an array with objects like this:

Array [
  Object {
    "amount": 1,
    "color": "blue",
    "id": "1",
    "size": "S",
  },
  Object {
    "amount": 1,
    "color": "red",
    "id": "2",
    "size": "L",
  },
  Object {
    "amount": 1,
    "color": null,
    "id": "3",
    "size": null,
  },
  Object {
    "amount": 1,
    "color": "blue",
    "id": "1",
    "size": "S",
  },
  Object {
    "amount": 1,
    "color": "red",
    "id": "2",
    "size": "L",
  },
  Object {
    "amount": 1,
    "color": null,
    "id": "3",
    "size": null,
  },
  Object {
    "amount": 1,
    "color": "blue",
    "id": "1",
    "size": "S",
  },
  Object {
    "amount": 1,
    "color": "red",
    "id": "2",
    "size": "L",
  },
  Object {
    "amount": 1,
    "color": null,
    "id": "3",
    "size": null,
  },
  Object {
    "amount": 1,
    "color": "blue",
    "id": "1",
    "size": "S",
  },
  Object {
    "amount": 1,
    "color": "red",
    "id": "2",
    "size": "L",
  },
  Object {
    "amount": 1,
    "color": null,
    "id": "3",
    "size": null,
  },
  Object {
    "amount": 1,
    "color": "blue",
    "id": "1",
    "size": "S",
  },
  Object {
    "amount": 1,
    "color": "red",
    "id": "2",
    "size": "L",
  },
  Object {
    "amount": 1,
    "color": null,
    "id": "3",
    "size": null,
  },
]

How can I add all amounts +1 where has the same color and size and id, and removes the duplicated colors?

This one should work. I split it on two parts, so it is easier to understand.

 const src = [ { "amount": 1, "color": "blue", "id": "1", "size": "S", }, { "amount": 1, "color": "red", "id": "2", "size": "L", }, { "amount": 1, "color": null, "id": "3", "size": null, }, { "amount": 1, "color": "blue", "id": "1", "size": "S", }, { "amount": 1, "color": "red", "id": "2", "size": "L", }, { "amount": 1, "color": null, "id": "3", "size": null, }, { "amount": 1, "color": "blue", "id": "1", "size": "S", }, { "amount": 1, "color": "red", "id": "2", "size": "L", }, { "amount": 1, "color": null, "id": "3", "size": null, }, { "amount": 1, "color": "blue", "id": "1", "size": "S", }, { "amount": 1, "color": "red", "id": "2", "size": "L", }, { "amount": 1, "color": null, "id": "3", "size": null, }, { "amount": 1, "color": "blue", "id": "1", "size": "S", }, { "amount": 1, "color": "red", "id": "2", "size": "L", }, { "amount": 1, "color": null, "id": "3", "size": null, }, ]; const amountsByKey = src.reduce((collector, item) => { const item_key = JSON.stringify({ id: item.id, color: item.color, size: item.size }); collector[item_key] = item.amount + (collector[item_key] | 0); return collector; }, {}); const result = Object.entries(amountsByKey).map(([key, value]) => ({...JSON.parse(key), amount: value, })); console.log(result);

try:

"use strict";
const array = [
    {
        amount: 1,
        color: "blue",
        id: "1",
        size: "S",
    },
    {
        amount: 1,
        color: "red",
        id: "2",
        size: "L",
    },
    {
        amount: 1,
        color: null,
        id: "3",
        size: null,
    },
    {
        amount: 1,
        color: "blue",
        id: "1",
        size: "S",
    },
    {
        amount: 1,
        color: "red",
        id: "2",
        size: "L",
    },
    {
        amount: 1,
        color: null,
        id: "3",
        size: null,
    },
    {
        amount: 1,
        color: "blue",
        id: "1",
        size: "S",
    },
    {
        amount: 1,
        color: "red",
        id: "2",
        size: "L",
    },
    {
        amount: 1,
        color: null,
        id: "3",
        size: null,
    },
    {
        amount: 1,
        color: "blue",
        id: "1",
        size: "S",
    },
    {
        amount: 1,
        color: "red",
        id: "2",
        size: "L",
    },
    {
        amount: 1,
        color: null,
        id: "3",
        size: null,
    },
    {
        amount: 1,
        color: "blue",
        id: "1",
        size: "S",
    },
    {
        amount: 1,
        color: "red",
        id: "2",
        size: "L",
    },
    {
        amount: 1,
        color: null,
        id: "3",
        size: null,
    },
];
const colors = [];
for (let i = 0; i < array.length; i++) {
    const obj = array[i];
    // validate color, id, size has not indexed yet
    if (!colors.find((o) => o.id == obj.id && o.color == obj.color && o.size == obj.size)) {
        // add color to index
        colors.push({
            color: obj.color,
            id: obj.id,
            size: obj.size
        });
    }
    else {
        // if previous color already added
        // increase amount
        obj.amount++;
        // modify array
        array[i] = obj;
    }
}
console.clear();
console.log(array);

result

[
  { amount: 1, color: 'blue', id: '1', size: 'S' },
  { amount: 1, color: 'red', id: '2', size: 'L' },
  { amount: 1, color: null, id: '3', size: null },
  { amount: 2, color: 'blue', id: '1', size: 'S' },
  { amount: 2, color: 'red', id: '2', size: 'L' },
  { amount: 2, color: null, id: '3', size: null },
  { amount: 2, color: 'blue', id: '1', size: 'S' },
  { amount: 2, color: 'red', id: '2', size: 'L' },
  { amount: 2, color: null, id: '3', size: null },
  { amount: 2, color: 'blue', id: '1', size: 'S' },
  { amount: 2, color: 'red', id: '2', size: 'L' },
  { amount: 2, color: null, id: '3', size: null },
  { amount: 2, color: 'blue', id: '1', size: 'S' },
  { amount: 2, color: 'red', id: '2', size: 'L' },
  { amount: 2, color: null, id: '3', size: null }
]

Typescript Playground Test 结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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