将JSON对象与相同的"键"合并并添加他们的"值"使用JavaScript

时间:2015-06-14 18:43:24

标签: javascript json merge key

我的JSON看起来像这样

{"rows":[
    {"key":["zeit.de"],"value":98},
    {"key":["google.com"],"value":49},
    {"key":["spiegel.de"],"value":20},
    {"key":["spiegel.de"],"value":12},
    {"key":["spiegel.de"],"value":20},
    {"key":["spiegel.de"],"value":12},
    {"key":["netmng.com"],"value":49},
    {"key":["zeit.de"],"value":300}
]}

我正在寻找合并所有相同键的解决方案,并将合并键的值加在一起,以获得类似的内容。

{"rows":[
    {"key":["zeit.de"],"value":398},
    {"key":["google.com"],"value":49},
    {"key":["spiegel.de"],"value":54},
    {"key":["netmng.com"],"value":49}
]}

感谢您的回答。

5 个答案:

答案 0 :(得分:3)

var json = '{"rows":[{"key":["zeit.de"],"value":98},{"key":["google.com"],"value":49},{"key":["spiegel.de"],"value":20},{"key":["spiegel.de"],"value":12},{"key":["spiegel.de"],"value":20},{"key":["spiegel.de"],"value":12},{"key":["netmng.com"],"value":49},{"key":["zeit.de"],"value":300}]}';
var obj = JSON.parse(json);

var newObj = {};
for(i in obj['rows']){
 var item = obj['rows'][i];
    if(newObj[item.key[0]] === undefined){
        newObj[item.key[0]] = 0;
    }
    newObj[item.key[0]] += item.value;
}

var result = {};
result.rows = [];
for(i in newObj){
    result.rows.push({'key':i,'value':newObj[i]});
}
console.log(result);

https://jsfiddle.net/s8rp84qc/

答案 1 :(得分:3)

以下是使用ECMA脚本Array函数forEachsomefilter的替代解决方案:

var data = {
  "rows": [{
    "key": ["zeit.de"],
    "value": 98
  }, {
    "key": ["google.com"],
    "value": 49
  }, {
    "key": ["spiegel.de"],
    "value": 20
  }, {
    "key": ["spiegel.de"],
    "value": 12
  }, {
    "key": ["spiegel.de"],
    "value": 20
  }, {
    "key": ["spiegel.de"],
    "value": 12
  }, {
    "key": ["netmng.com"],
    "value": 49
  }, {
    "key": ["zeit.de"],
    "value": 300
  }]
};

var merged = {
  rows: []
};

data.rows.forEach(function(sourceRow) {
  debugger;
  if(!merged.rows.some(function(row) { return row.key[0] == sourceRow.key[0]; })) {
    merged.rows.push({ key: [sourceRow.key[0]], value: sourceRow.value });
  } else {
    var targetRow = merged.rows.filter(function(targetRow) { return targetRow.key[0] == sourceRow.key[0] })[0];
    
    targetRow.value += sourceRow.value;
  }
});

document.getElementById("result").textContent = JSON.stringify(merged);
<div id="result"></div>

答案 2 :(得分:0)

您可以尝试这样的事情:

var newData = {rows: []},
    index;

for (var i = 0; i < data.rows.length;i++){
    index = newDataContains(data.rows[i].key);
    if (index == -1){
        newData.rows.push({key: data.rows[i].key, value: data.rows[i].value});
    } else {
        newData.rows[index].value += data.rows[i].value;
    }
}

function newDataContains(key){
    for (var i=0; i < newData.rows.length; i++){
        if (JSON.stringify(newData.rows[i].key) == JSON.stringify(key)) {
            return i;
            break;
        }
    }
    return -1;
}

的jsfiddle: https://jsfiddle.net/9p81g5j6/

答案 3 :(得分:0)

你可以使用lodash

protected void GetViewOnClientClick(object sender, EventArgs e)
{
    LinkButton lb = (LinkButton)sender;
    GridViewRow row = (GridViewRow)lb.NamingContainer;
    if (row != null)
    {
        int index = row.RowIndex;

        LinkButton link = (LinkButton)row.FindControl("lnkbtnView");
        link.Enabled = false;
    }
}

答案 4 :(得分:0)

JSON如下所示:2场景 在传递角色-id

时,网址很常见
  1. role-id / name有admin:

    [{"Key":"value1"},{"key":"value2"},{"key":"value3"}]
    
  2. role-id / name有Tester

    [{"Key":"value1"},{"key":"value3"}]
    
  3. 没有键:值2适用于Tester

    例如:

    全局变量字符串a,b;

    想要获取键值对中的所有值,如果它具有来自JSON的value2和value3

    即:管理员  去找一个=&#34;两个&#34;

    检查Tester 如果它只有value3那么  B =&#34;单&#34;