7

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'
}]
1
  • There's no "parsing" going on there. You're just accessing data in one object and transforming into another object. Anyway, in cases like this where you're stuck I'd recommend sitting down and writing out what you think you need to in native language. For example, "Create an array. Each element of the array will be an object, ....". That could help you get started. Commented Nov 28, 2015 at 4:15

4 Answers 4

7

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>

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't actually produce the expected output.
With ES6, var arr = []; Object.keys(obj).forEach((e) => arr.push(obj[e][0]));
Still doesn't populate the connector property with the key.
4

You can go like this in Underscore:

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

See it in action

Comments

4

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;
});

1 Comment

Best solution for because it can be use in a _.chain
1

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; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.