Dibish
Dibish

Reputation: 9293

Parsing object to array using underscore js or lodash

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

Tried using usersocre, but its not getting expected result. Sorry am new to underscore js. Your help is much 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'
}]

Upvotes: 7

Views: 12630

Answers (4)

Tushar
Tushar

Reputation: 87203

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

  1. Get all the keys of the object
  2. Iterate over the keys array using forEach
  3. Push 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 be converted to use Lodash/Underscore's 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>

Upvotes: 7

Quy
Quy

Reputation: 1373

If you're talking about functional, I usually don't want to alter the original object in any way, so I use extend to create a new object. I overwrite the connector property in the next line and return the object.

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

Upvotes: 4

Malk
Malk

Reputation: 11983

Grab the object from the first value in each key's array and assign the key to the connector property. No library needed.

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

Upvotes: 1

Manwal
Manwal

Reputation: 23816

You can go like this in Underscore:

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

See it in action

Upvotes: 4

Related Questions