Parsing an array object using js or lodash underscore

I have a requirement to parse an object and convert it to an array using Underscore / Lo-Dash

I tried using usersocre, but did not get the expected result. Sorry, new to js underscore. Your help is greatly appreciated.

var arr = _.values(obj) var obj = { '2c13790be20a06a35da629c71e548afexing': [{ connector: '', displayName: 'John', loginName: '', userImage: '2.jpg' }], '493353a4c5f0aa71508d4055483ff979linkedinpage': [{ connector: '', displayName: 'Mak', loginName: '', userImage: '1.jpg' }] } 

expected output array

 array = [{ connector: '2c13790be20a06a35da629c71e548afexing', displayName: 'John', loginName: '', userImage: '2.jpg' }, { connector: '493353a4c5f0aa71508d4055483ff979linkedinpage', displayName: 'Mak', loginName: '', userImage: '1.jpg' }] 
+6
source share
4 answers

Use Object.keys with forEach and add the object to the array.

  • Get all object keys
  • Iterating over an array of keys using forEach
  • Click the first element of the subarray in the result array.

Code:

 var arr = []; Object.keys(obj).forEach(function(e) { // Get the key and assign it to `connector` obj[e][0].connector = e; arr.push(obj[e][0]); }); 

 var obj = { '2c13790be20a06a35da629c71e548afexing': [{ connector: '', displayName: 'John', loginName: '', userImage: '2.jpg' }], '493353a4c5f0aa71508d4055483ff979linkedinpage': [{ connector: '', displayName: 'Mak', loginName: '', userImage: '1.jpg' }] }; var arr = []; Object.keys(obj).forEach(function(e) { obj[e][0].connector = e; arr.push(obj[e][0]); }); console.log(arr); document.getElementById('output').innerHTML = JSON.stringify(arr, 0, 4); 
 <pre id="output"></pre> 

The same code will be converted to use Lodash / Underscore forEach .

 var arr = []; _.forEach(obj, function(e, k) { e[0].connector = k; arr.push(e[0]); }); 

 var obj = { '2c13790be20a06a35da629c71e548afexing': [{ connector: '', displayName: 'John', loginName: '', userImage: '2.jpg' }], '493353a4c5f0aa71508d4055483ff979linkedinpage': [{ connector: '', displayName: 'Mak', loginName: '', userImage: '1.jpg' }] }; var arr = []; _.forEach(obj, function(e, k) { e[0].connector = k; arr.push(e[0]); }); console.log(arr); document.getElementById('output').innerHTML = JSON.stringify(arr, 0, 4); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> <pre id="output"></pre> 
+5
source

This can be done underline :

 _(obj).each(function(elem, key){ arr.push(elem[0]); }); 

Look at the action

+3
source

If you are talking about functionality, I usually do not want to modify the original object in any way, so I use the extension to create a new object. I overwrite the connector property on the next line and return the object.

 _.map(function( arr, key ) { var obj = _.extend({}, arr.pop()); obj.connector = key; return obj; }); 
+3
source

Take an object from the first value in each key array and assign a key to the connector property. No library required.

 var result = []; for (key in obj) { result.push(obj[key][0]); result[result.length-1]["connector"] = key; } 
+1
source

All Articles