最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Parsing object to array using underscore js or lodash - Stack Overflow

programmeradmin1浏览0评论

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'
}]

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'
}]
Share Improve this question edited Nov 24, 2015 at 6:01 Tushar 87.2k21 gold badges163 silver badges181 bronze badges asked Nov 24, 2015 at 5:57 DibishDibish 9,29322 gold badges68 silver badges107 bronze badges 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. – user663031 Commented Nov 28, 2015 at 4:15
Add a comment  | 

4 Answers 4

Reset to default 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>

You can go like this in Underscore:

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

See it in action

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

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; 
}
发布评论

评论列表(0)

  1. 暂无评论