-3

I need to make addition using NodeJS in an array JSON and then return the altered JSON like this, I need also check if key exist or not to avoid exceptions:

JSON:

{
    "cmd": [
        {
            "sales": [
                {
                    "qte1": "2",
                    "qte2": "3", 
                    "someString": "test 
                },
                {
                    "qte1": "66",
                    "someAttribute": "test " 
                },
                {
                    "qte2": "77",
                    "toto": "tata"
                }
            ]
        }
    ]
} 

target JSON:

{
    "cmd": [
        {
            "sales": [
                {
                    "qte1": "2",
                    "qte2": "3",
                    "somme": "5"
                },
                {
                    "qte1": "66",
                    "somme": "66"

                },
                {
                    "qte2": "77",
                    "somme": "77"
                }
            ]
        }
    ]
}

I need to add the two key qte1 et qte2 would you have any propositions ?

Best regards

Amine Hatim
  • 237
  • 2
  • 7
  • 19

3 Answers3

3

Seems like you just want the sum of the existing keys, should be pretty simple using a couple .map commands and a .reduce:

return json.cmd.map(function(salesList) {
    return salesList.sales.map(function(sale) {
        var keysToAdd = ["qte1", "qte2"];

        sale.somme = Object.keys(sale).reduce(function(total, key) {
            return total += keysToAdd.indexOf(key) > -1 ? +sale[key] : 0;
        }, 0);

        return sale;
    });
});

Demo: https://jsfiddle.net/x294yt1h/

tymeJV
  • 103,943
  • 14
  • 161
  • 157
1
const data = 
{
    "cmd": [
        {
            "sales": [
                {
                    "qte1": "2",
                    "qte2": "3"
                },
                {
                    "qte1": "66"
                },
                {
                    "qte2": "77"
                }
            ]
        }
    ]
};

function sum(dataObj) {
    const hasCmd = dataObj && dataObj.cmd && dataObj.cmd.length > 0;
    const hasSales = hasCmd && dataObj.cmd[0].sales && dataObj.cmd[0].sales.length > 0;
    if (hasCmd && hasSales) {
        const clonedArray = dataObj.cmd[0].sales.slice();
        const summedArray = clonedArray.map(function(group) {
            const groupKeys = Object.keys(group);
            const sum = groupKeys.reduce(function (total, key) {
                return total + parseInt(group[key], 10 /* Decimal Radix */);
            }, 0);
            return Object.assign({}, group, { 'somme': sum.toString() });
        });
        // build a new object to return to include new summed array
        return { "cmd": [{ "sales": summedArray }] };
    }
    return dataObj;
}

console.log('data', JSON.stringify(data));
const newData = sum(data);
console.log('newData', JSON.stringify(newData));
Rob Brander
  • 3,702
  • 1
  • 20
  • 33
0

Maybe you can first and formost refector your tree.

Like that :

{
  command: [
    {
      sales: [
        {
          quantity: [1, 2, 4],
          sum: 7
        }
      ]
    }
  ]
}
I don't know if your structure is open to any refectoring, but you must do something about it !

However, try to use the correct type of value in your row, and try to keep in mind to never mutate and object, use functor like map and reduce :

const cmd = [
    {
    sales: [
        {
          quantity: [1, 2, 3],
          sum: 0
        },
        {
          quantity: [67, 2, 3],
          sum: 0
        }
    ]
  }
];

const newObjectWithSum = cmd.map(sales => {
    return sales.sales.map((sale) => {
    sale.sum = sale.quantity.reduce((valueIn, next) => { 
        return valueIn + next;
    },0);
    return sale;
    });
})
Mech45
  • 321
  • 1
  • 9