如何在JSON对象中组合多个键值?

时间:2016-01-19 11:25:23

标签: javascript jquery json

我有这个示例JSON对象

var sample = [{
    "label": "one",
    "value": 1
}, {
    "label": "two",
    "value": 2
}, {
    "label": "three",
    "value": 3
}, {
    "label": "four",
    "value": 4
}, {
    "label": "five",
    "value": 5
}];

我想改变一些像这样的事情

var sample = [{
    "label": "one",
    "value": 1,
    "newKeyValue": "one|1"
}, {
    "label": "two",
    "value": 2,
    "newKeyValue": "two|2"
}, {
    "label": "three",
    "value": 3,
    "newKeyValue": "three|3"
},
...
];

它应结合两个键值并返回组合两者的新键值。

JSON是动态的关键标签,值不是静态的,它可以是任何东西。例如[{"name":"srinivas","lastname":"pai"}]

6 个答案:

答案 0 :(得分:4)

你可以使用这样的地图:

修改

用于处理您可以使用的通用键 第一个密钥Object.keys(d)[0] 第二个键Object.keys(d)[1]



var sample = [

     {
      "label":"one",
      "value":1
     },

     {
      "label":"two",
      "value":2
     },
     {
      "label":"three",
      "value":3
     },
     { 
      "label":"four",
      "value":4
     },
     { 
      "label":"five",
      "value":5
      }

     ];
    var data = sample.map(function(d){
     return {label: Object.keys(d)[0], value: Object.keys(d)[1], newKeyValue:  Object.keys(d)[0] +"|" + Object.keys(d)[1]}
    }) 
    console.log(data)




希望这有帮助!

答案 1 :(得分:3)

您可以使用Array#map()Object.keys()Array#join()

在ES6中,您可以使用Arrow functions

sample = sample.map(obj => {
    var keys = Object.keys(obj);
    obj.newKeyValue = keys.map(key => obj[key]).join('|');
    return obj;
});



var sample = [{
    "label": "one",
    "value": 1
}, {
    "name": "two",
    "age": 2
}, {
    "five": "three",
    "six": 3
}, {
    "company": "four",
    "organization": 4
}, {
    "label": "five",
    "value": 5
}];

sample = sample.map(function (x) {
    var keys = Object.keys(x);
    x.newKeyValue = keys.map(key => x[key]).join('|');
    return x;
});
console.log(sample);
document.body.innerHTML = '<pre>' + JSON.stringify(sample, 0, 4) + '</pre>';
&#13;
&#13;
&#13;

在ES5中,您可以使用与匿名函数相同的代码

sample = sample.map(function (obj) {
    var keys = Object.keys(obj);
    obj.newKeyValue = keys.map(function (key) {
        return obj[key]
    }).join('|');
    return obj;
});

由动态键引起的限制:

  1. 无法维护对象中键的顺序
  2. 这将加入对象中的所有可用键(如果您只想加入更少的

答案 2 :(得分:2)

var sample = [

         {
          "label":"one",
          "value":1
         },

         {
          "label":"two",
          "value":2,
          "optionalValue":2
         },
         {
          "label":"three",
          "value":3,
          "remarks":"free text"
         },
         { 
          "label":"four",
          "value":4
         },
         { 
          "label":"five",
          "value":5
          }

         ];

    for (var key in sample) {
      var newValue = [];
      for (var piece in sample[key]){
        newValue.push(sample[key][piece])
      }
      sample[key]["newKeyValue"] = newValue.join('|');
    }

    $('pre').html(JSON.stringify(sample,null,4));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre></pre>

答案 3 :(得分:1)

您可以使用Array.prototype.forEach()进行原位更改。

  

forEach()方法每个数组元素执行一次提供的函数。

编辑:由于订单的原因,使用动态密钥存储在数组中。

&#13;
&#13;
var sample = [{ "label": "one", "value": 1 }, { "label": "two", "value": 2 }, { "label": "three", "value": 3 }, { "label": "four", "value": 4 }, { "label": "five", "value": 5 }];

sample.forEach(function (a) {
    a.newKeyValue = ['label', 'value'].map(function (k) { return a[k]; }).join('|');
});

document.write('<pre>' + JSON.stringify(sample, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 4 :(得分:0)

如果他们有更多元素,那么请使用$.extend

var sample = [

         {
          "label":"one",
          "value":1
         },

         {
          "label":"two",
          "value":2
         },
         {
          "label":"three",
          "value":3
         },
         { 
          "label":"four",
          "value":4
         },
         { 
          "label":"five",
          "value":5
          }

         ];
        $(sample).each(function(i,item){
            var keyes = Object.keys(item);
            sample[i] =  $.extend(item,{newKeyValue: item[keyes[0]] +"|" +item[keyes[1]]});
        });
       
        console.log(sample)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

当您拥有更多对象并且想要合并两者时,

$.extend也很有用 的例如即可。

  var base  = {
          "label":"one",
          "value":1
         }

并且您想要添加更多对象

 var extra  =  {
          "new1":"value1",
          "new2":"value2",
          "new3":"value3",
          "new4":"value4",
       }

然后它将由

完成
$.extend(base,extra);

输出

        {
              "label":"one",
              "value":1,
              "new1":"value1",
              "new2":"value2",
              "new3":"value3",
              "new4":"value4",
         }

答案 5 :(得分:0)

var sample = [{"name":"srinivas","lastname":"pai"}];    

sample.map(function(item) {
  item.newKeyValue = Object.getOwnPropertyNames(item).map(function(d) {return item[d];}).join("|");
})

    console.log(sample);