Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.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值,在添加动态键和动态值的同时持久化JSON_Javascript_Jquery_Json_Set - Fatal编程技术网

Javascript 通过检查旧的JSON值,在添加动态键和动态值的同时持久化JSON

Javascript 通过检查旧的JSON值,在添加动态键和动态值的同时持久化JSON,javascript,jquery,json,set,Javascript,Jquery,Json,Set,我有一个类似这样的json数据,包含重复的年份值。我必须将数据按年份与country:value进行分组(按年份唯一,意味着同一年不会有重复的国家,即在2003年,欧洲国家将不会再次出现) 下面是将动态生成的示例数据 { "data": [ { "year": 2003, "europe": 2.5 },{ "year": 2003

我有一个类似这样的json数据,包含重复的年份值。我必须将数据按年份与country:value进行分组(按年份唯一,意味着同一年不会有重复的国家,即在2003年,欧洲国家将不会再次出现)

下面是将动态生成的示例数据

 {
      "data": [ {
                    "year": 2003,
                    "europe": 2.5
                },{
                    "year": 2003,
                    "namerica": 2.5
                },{
                    "year": 2003,
                    "asia": 2.1
                },{
                    "year": 2003,
                    "lamerica": 0.3
                },{
                    "year": 2003,
                    "meast": 0.2
                },{
                    "year": 2003,
                    "africa": 0.1
                }, {
                    "year": 2004,
                    "europe": 2.6
                },{
                    "year": 2004,
                    "namerica": 2.7
                },{
                    "year": 2004,
                    "asia": 2.2
                },{
                    "year": 2004,
                    "lamerica": 0.3
                },{
                    "year": 2004,
                    "meast": 0.3
                },{
                    "year": 2004,
                    "africa": 0.1
                }, {
                    "year": 2005,
                    "europe": 2.8
                  },{
                    "year": 2005,
                    "namerica": 2.9
                  },{
                    "year": 2005,
                    "asia": 2.4
                  },{
                    "year": 2005,
                    "lamerica": 0.3
                  },{
                    "year": 2005,
                    "meast": 0.3
                  },{
                    "year": 2005,
                    "africa": 0.1
        }]
    }
以下是所需的输出:

{
            "year": 2003,
            "europe": 2.5,
            "namerica": 2.5,
            "asia": 2.1,
            "lamerica": 0.3,
            "meast": 0.2,
            "africa": 0.1
        }, {
            "year": 2004,
            "europe": 2.6,
            "namerica": 2.7,
            "asia": 2.2,
            "lamerica": 0.3,
            "meast": 0.3,
            "africa": 0.1
        }, {
            "year": 2005,
            "europe": 2.8,
            "namerica": 2.9,
            "asia": 2.4,
            "lamerica": 0.3,
            "meast": 0.3,
            "africa": 0.1
        }

动态数据是非常有问题的,因为这些国家似乎是随机的

这将使针对这一点进行编程变得更加容易

{ "year": 2003, "country": "europe", "index": 2.3}
但我不知道你是否有这样做的可能性


你要做的就是这样

let output = {};

for(let i=0;i<data.length;i++){
    const dataIndex = data[i];
    const yearData = dataIndex.year;
    if(output[yearData] === undefined){
       output[yearData] = {"year":yearData};
    }
    const keys = Object.keys(dataIndex);
    for(let j=0;j<keys.length;j++){
       const key = keys[j];
       if(key !== "year"){
          const countryIndex = dataIndex[key];
          output[yearData][key] = countryIndex;
       }
    }
}
所以现在你可以说

console.log(output["2003"]["africa"]); // 0.1
var-ar={
“数据”:[{
“年份”:2003年,
“欧洲”:2.5
}, {
“年份”:2003年,
“namerica”:2.5
}, {
“年份”:2003年,
“亚洲”:2.1
}, {
“年份”:2003年,
“拉美国家”:0.3
}, {
“年份”:2003年,
“米斯特”:0.2
}, {
“年份”:2003年,
“非洲”:0.1
}, {
“年份”:2004年,
“欧洲”:2.6
}, {
“年份”:2004年,
“namerica”:2.7
}, {
“年份”:2004年,
“亚洲”:2.2
}, {
“年份”:2004年,
“拉美国家”:0.3
}, {
“年份”:2004年,
“米斯特”:0.3
}, {
“年份”:2004年,
“非洲”:0.1
}, {
“年份”:2005年,
“欧洲”:2.8
}, {
“年份”:2005年,
“namerica”:2.9
}, {
“年份”:2005年,
“亚洲”:2.4
}, {
“年份”:2005年,
“拉美国家”:0.3
}, {
“年份”:2005年,
“米斯特”:0.3
}, {
“年份”:2005年,
“非洲”:0.1
}]
};
//提取不同年份
风险值年数=[];
对于(var i=0;i
console.log(output["2003"]["africa"]); // 0.1