合并JSON对象

时间:2018-06-20 09:53:58

标签: javascript jquery json

这是我输入的JSON:

{
"totalAmount": [
{
  "currency": "INR",
  "amount": "2",
  "NameID": "test-1"
},
{
  "currency": "INR",
  "amount": "8",
  "NameID": "test-1"
},
{
  "currency": "SGD",
  "amount": "4",
  "NameID": "mytest-1"
}]}

我需要这样的输出JSON:

{
 "totalAmount": [
{
  "currency": "INR",
  "amount": "10",
  "NameID": "test-5"
},
{
  "currency": "SGD",
  "amount": "4",
  "NameID": "mytest-1"
}]}

即要添加具有相同“ NameID”的对象的金额值。

编辑注释中的代码。而且,看起来这篇文章主要是代码,我必须添加更多详细信息才能对其进行编辑...

var nwarr = [];

for(i in obj['totalAmount']){
    var item = obj['totalAmount'][i];
    var newObj = {};

    if(newObj[item.NameID] === undefined){
        newObj[item.NameID] = 0;
    }

    newObj[item.NameID] += parseInt(item.amount);
    nwarr.push[newObj];
}

printLog("nwarr ="+nwarr);

var result = {};
result.totalAmount = [];

for(i in newObj){
    result.totalAmount.push({'NameID':i,'amount':newObj[i]});
}

console.log(result); 

到目前为止,我已经尝试过了 Please click here

3 个答案:

答案 0 :(得分:0)

您可以使用较少的代码来完成此操作,但这是一个快速的解决方案。

var json = '{"totalAmount":[{"currency":"INR","amount":"2400000","nickNameID":"shopify-5"},{"currency":"SGD","amount":"49204","nickNameID":"lazada-1"},{"currency":"SGD","amount":"49258","nickNameID":"lazada-2"},{"currency":"SGD","amount":"400","nickNameID":"etsy-1"},{"currency":"SGD","amount":"10","nickNameID":"lazada-1"},{"currency":"INR","amount":"800000","nickNameID":"snapdeal-1"},{"currency":"SGD","amount":"46600","nickNameID":"magento-1"},{"currency":"USD","amount":"257569","nickNameID":"amazon-1"},{"currency":"INR","amount":"6702314","nickNameID":"shopify-3"},{"currency":"INR","amount":"2100000","nickNameID":"shopify-2"},{"currency":"USD","amount":"1904126200","nickNameID":"eBay-7"},{"currency":"SGD","amount":"11093000","nickNameID":"sellInAll-1"},{"currency":"SGD","amount":"1203","nickNameID":"qoo10-1"},{"currency":"SGD","amount":"525207","nickNameID":"shopee-1"},{"currency":"INR","amount":"1020800","nickNameID":"shopclues-1"},{"currency":"SGD","amount":"75","nickNameID":"qoo10-1"},{"currency":"SGD","amount":"75","nickNameID":"shopify-1"},{"currency":"SGD","amount":"75","nickNameID":"eBay-1"},{"currency":"SGD","amount":"372740","nickNameID":"shopee-6"}],"totalUSDAmount":{"currency":"USD","amount":256467}}';

const obj = JSON.parse(json);
const newObj = {};

for(let key in obj.totalAmount){
    const item = obj.totalAmount[key];

    if(newObj[item.nickNameID] === undefined){
        newObj[item.nickNameID] = {
            amount: 0,
            currency: undefined
        };
    }

    newObj[item.nickNameID].amount += parseInt(item.amount);    
    newObj[item.nickNameID].currency = item.currency; 
}

const result = [];

for (let key in newObj) {
    const item = newObj[key];

    result.push({
        nickNameID: key,
        currency: item.currency,
        amount: item.amount
    });
}

console.log(result);

答案 1 :(得分:0)

使用Number(value)来确保值是一个有效数字,并使用Number.isInteger测试它是否为整数

const testValues = [
    // OK
    1529490236830,
    '1529490236830',
    '0',
    '',
    1529490236830.0,
    '1529490236830.000',
    // NOT OK
    1529490236830.123,
    'abc',
    '1970/00/12',
];

testValues.forEach(value => {
    if (Number.isInteger(Number(value))) {
        console.log(Date(value));
    } else {
        console.log(`${value} not a valid timestamp`);
    }
});

答案 2 :(得分:0)

您可以使用Array.prototype.reduce()

const data = {"totalAmount": [{"currency": "INR","amount": "2","NameID": "test-1"},{"currency": "INR","amount": "8","NameID": "test-1"},{"currency": "SGD","amount": "4","NameID": "mytest-1"}]};
const temp = data.totalAmount.reduce((a, c) => {
    const id = c.currency + c.NameID;
    c.amount = +c.amount;
    !a[id] ? a[id] = c : a[id].amount += c.amount;
    return a;
  }, {});
const result = Object.keys(temp).map(k => temp[k]);

console.log(result);