How to combine multiple key values ​​in a JSON object?

I have this sample JSON object

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

I want to change something like this

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

It should combine both key values ​​and return a new key value that combines both.

JSON goes dynamically with a key label, and the value is not static, it can be anything. For example, [{"name":"srinivas","lastname":"pai"}]

+6
source share
6 answers

You can use this card:

EDIT

To process shared keys, you can use Object.keys(d)[0] for the first key Object.keys(d)[1] for the second key

 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) 

Hope this helps!

+4
source

You can use Array#map() , Object.keys() , Array#join() .

In ES6, you can use the 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>'; 

In ES5 you can use the same code with anonymous functions

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

Limitations due to dynamic keys:

  • Key order in object is not supported
  • This will combine all the available keys in the object (in case you just want to join fewer)
+3
source

 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> 
+2
source

You can use Array.prototype.forEach() for in situ changes.

The forEach() method executes the provided function once for an array element.

Edit: using dynamic keys stored in the array, due to order.

 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>'); 
+1
source

If there are more, use $. expand

 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 also useful if you already have more objects and you want to combine both , for example .

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

and you want to add more objects

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

then it will be done

 $.extend(base,extra); 

Output :

  { "label":"one", "value":1, "new1":"value1", "new2":"value2", "new3":"value3", "new4":"value4", } 
0
source
 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); 
0
source

All Articles