Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 合并JSON对象_Javascript_Jquery_Json - Fatal编程技术网

Javascript 合并JSON对象

Javascript 合并JSON对象,javascript,jquery,json,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,如下所示: { "tot

这是我的输入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"
}]}
i、 e.添加具有相同名称ID的对象的金额值

从注释编辑代码。另外,这篇文章看起来主要是代码,我必须添加更多细节才能编辑它

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); 
到目前为止我已经试过了

您可以用更少的代码来实现这一点,但这里有一个快速的解决方案

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);
使用Numbervalue确保该值是一个有效的数字,并测试它是否是一个带有number.isInteger的整数

您可以使用:

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.reducea,c=>{ const id=c.currency+c.NameID; c、 金额=+c.金额; !a[id]?a[id]=c:a[id]。金额+=c.金额; 返回a; }, {}; const result=Object.keystemp.mapk=>temp[k];
console.logresult;这是一个JavaScript问题,与jQuery无关。另外,请展示一些努力并分享你的尝试。到目前为止你做了什么?@GayathriGanesan,请不要将代码作为评论发布。将编辑我的问题。非常感谢你的朋友。
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`);
    }
});