Skip to main content
added 424 characters in body
Source Link
Kai Hao
  • 196
  • 3

I am assuming you have the correct target to transform (the input array is filtered).

If you are using es6:

const input = [
    {
        "setId": "setA",
        "prodId": "A",
        "price": 5,
        "deliveryCost": 1,
        "name": "Set_Prod01"
    },
    {
        "setId": "setA",
        "prodId": "B",
        "price": 5,
        "deliveryCost": 2,
        "name": "Set_Prod01"
    }
];

const output = {
    "setId": "setA",
    "prodId": "A,B",
    "price": 5,
    "deliveryCost": 3,
    "name": "Set_Prod01"
};

const groupBy = (input) => (
    Object.assign(
        input[0],
        {
            prodId: input.map(set => set.prodId).join(','),
            deliveryCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
        }
    )
);

console.log(output);
console.log(groupBy(input));

If you are using es6 and stage-2 object spread operator, change the function to:

const groupBy = (input) => (
    {
        ...input[0],
        prodId: input.map(set => set.prodId).join(','),
        delivertCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
    }
);

If you are not using es6, simplest method is use it, and use babel to convert it into es5 compatible code. It's less code, more readable and easier to maintain.

EDIT: The function above simply take an array of objects and assign them into another object. Note that in Object.assign, the latter's value will overwrite the former's value with the same key, so you can easily implement the overwriting rule you mentioned. The value are simply some functional programming, one join the array of string, and the other sum up the values.

I am assuming you have the correct target to transform (the input array is filtered).

If you are using es6:

const input = [
    {
        "setId": "setA",
        "prodId": "A",
        "price": 5,
        "deliveryCost": 1,
        "name": "Set_Prod01"
    },
    {
        "setId": "setA",
        "prodId": "B",
        "price": 5,
        "deliveryCost": 2,
        "name": "Set_Prod01"
    }
];

const output = {
    "setId": "setA",
    "prodId": "A,B",
    "price": 5,
    "deliveryCost": 3,
    "name": "Set_Prod01"
};

const groupBy = (input) => (
    Object.assign(
        input[0],
        {
            prodId: input.map(set => set.prodId).join(','),
            deliveryCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
        }
    )
);

console.log(output);
console.log(groupBy(input));

If you are using es6 and stage-2 object spread operator, change the function to:

const groupBy = (input) => (
    {
        ...input[0],
        prodId: input.map(set => set.prodId).join(','),
        delivertCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
    }
);

If you are not using es6, simplest method is use it, and use babel to convert it into es5 compatible code.

I am assuming you have the correct target to transform (the input array is filtered).

If you are using es6:

const input = [
    {
        "setId": "setA",
        "prodId": "A",
        "price": 5,
        "deliveryCost": 1,
        "name": "Set_Prod01"
    },
    {
        "setId": "setA",
        "prodId": "B",
        "price": 5,
        "deliveryCost": 2,
        "name": "Set_Prod01"
    }
];

const output = {
    "setId": "setA",
    "prodId": "A,B",
    "price": 5,
    "deliveryCost": 3,
    "name": "Set_Prod01"
};

const groupBy = (input) => (
    Object.assign(
        input[0],
        {
            prodId: input.map(set => set.prodId).join(','),
            deliveryCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
        }
    )
);

console.log(output);
console.log(groupBy(input));

If you are using es6 and stage-2 object spread operator, change the function to:

const groupBy = (input) => (
    {
        ...input[0],
        prodId: input.map(set => set.prodId).join(','),
        delivertCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
    }
);

If you are not using es6, simplest method is use it, and use babel to convert it into es5 compatible code. It's less code, more readable and easier to maintain.

EDIT: The function above simply take an array of objects and assign them into another object. Note that in Object.assign, the latter's value will overwrite the former's value with the same key, so you can easily implement the overwriting rule you mentioned. The value are simply some functional programming, one join the array of string, and the other sum up the values.

Source Link
Kai Hao
  • 196
  • 3

I am assuming you have the correct target to transform (the input array is filtered).

If you are using es6:

const input = [
    {
        "setId": "setA",
        "prodId": "A",
        "price": 5,
        "deliveryCost": 1,
        "name": "Set_Prod01"
    },
    {
        "setId": "setA",
        "prodId": "B",
        "price": 5,
        "deliveryCost": 2,
        "name": "Set_Prod01"
    }
];

const output = {
    "setId": "setA",
    "prodId": "A,B",
    "price": 5,
    "deliveryCost": 3,
    "name": "Set_Prod01"
};

const groupBy = (input) => (
    Object.assign(
        input[0],
        {
            prodId: input.map(set => set.prodId).join(','),
            deliveryCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
        }
    )
);

console.log(output);
console.log(groupBy(input));

If you are using es6 and stage-2 object spread operator, change the function to:

const groupBy = (input) => (
    {
        ...input[0],
        prodId: input.map(set => set.prodId).join(','),
        delivertCost: input.map(set => set.deliveryCost).reduce( (cur, next) => cur + next)
    }
);

If you are not using es6, simplest method is use it, and use babel to convert it into es5 compatible code.