-2

Does anyone know of a way to group an array of objects by two property. I have array like this:

    [
    {
        index: 4,
        name: "car",
        price: [10,20,30]
    },
    {
        index: 4,
        name: "car",
        price: [40,50,60]
    },
    {
        index: 3,
        name: "bike",
        price: [40,50,60]
    },
    {
        index: 2,
        name: "scooter",
        price: [10,20,60]
    },
    {
        index: 2,
        name: "scooter",
        price: [70]
    },
]

so I would like that the output looks like this

[
    {
        index: 4,
        name: car,
        price: [40,50,60,10,20,30]
    },
    {
        index: 3,
        name: bike,
        price: [40,50,60]
    },
    {
        index: 2,
        name: scooter,
        price: [10,20,60, 70]
    }
]

Objects that have the same name and index, connect, i.e. connect the price array.

Elder
  • 341
  • 3
  • 8
  • 21
  • @0stone0 https://jsfiddle.net/ey3kdovu/ but I don't know how connect array – Elder Aug 29 '23 at 16:05
  • Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – jabaa Aug 29 '23 at 16:08

3 Answers3

0

You can do this like this if you are using javascript.

const inputArray = [
    {
        index: 4,
        name: "car",
        price: [10, 20, 30]
    },
    {
        index: 4,
        name: "car",
        price: [40, 50, 60]
    },
    {
        index: 3,
        name: "bike",
        price: [40, 50, 60]
    },
    {
        index: 2,
        name: "scooter",
        price: [10, 20, 60]
    },
    {
        index: 2,
        name: "scooter",
        price: [70]
    },
];

const resultArray = [];

inputArray.forEach(item => {
    const existingItem = resultArray.find(
        obj => obj.index === item.index && obj.name === item.name
    );

    if (existingItem) {
        // Merge the price arrays
        existingItem.price = existingItem.price.concat(item.price);
    } else {
        // Create a new object
        resultArray.push({ ...item });
    }
});

console.log(resultArray);

Try this and let me know whether it works or not.

Venkat
  • 14
  • 1
0

You could take an emoty object for grouing along with an array for the grouping keys and build a string with values, like

4|car

of

{ index: 4, name: "car", price: [10, 20, 30] }

for a key which reflects index and name.

Then push all prices to the according group. At the end take only the values as result.

const
    data = [{ index: 4, name: "car", price: [10, 20, 30] }, { index: 4, name: "car", price: [40, 50, 60] }, { index: 3, name: "bike", price: [40, 50, 60] }, { index: 2, name: "scooter", price: [10, 20, 60] }, { index: 2, name: "scooter", price: [70] }],
    keys = ['index', 'name'],
    grouped = Object.values(data.reduce(
        (r, { price, ...o }) => {
            const key = keys.map(k => o[k]).join('|');
            (r[key] ??= { ...o, price: [] }).price.push(...price);
            return r;
        },
        Object.create(null)
    ));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I wrote a quick solution that converts your data into an 'associative array'.

It assumes that the index/name pair will always be unique, i.e. index 4 will always be 'car'.

const data = [
    {
        index: 4,
        name: "car",
        price: [10, 20, 30]
    },
    {
        index: 4,
        name: "car",
        price: [40, 50, 60]
    },
    {
        index: 3,
        name: "bike",
        price: [40, 50, 60]
    },
    {
        index: 2,
        name: "scooter",
        price: [10, 20, 60]
    },
    {
        index: 2,
        name: "scooter",
        price: [70]
    },
]

let indexed = {};

data.forEach((data) => {
    indexed[data.index] ??= {};
    indexed[data.index].index = data.index;
    indexed[data.index].name = data.name;
    indexed[data.index].price ??= [];
    indexed[data.index].price.push(...data.price);
});

console.log( indexed);



/* Output:
{
    "2": {
        "index": 2,
        "name": "scooter",
        "price": [
            10,
            20,
            60,
            70
        ]
    },
    "3": {
        "index": 3,
        "name": "bike",
        "price": [
            40,
            50,
            60
        ]
    },
    "4": {
        "index": 4,
        "name": "car",
        "price": [
            10,
            20,
            30,
            40,
            50,
            60
        ]
    }
}
*/
Dabrowski
  • 310
  • 1
  • 8