I have an array:
[
{
meta:'abc'
data:
{
name:'asd',
title: 'bbb'
}
},
{
meta:'abc'
data:
{
name:'asd',
title: 'bbb'
},
{
meta:'abc'
data:
{
name:'asd',
title: 'bbb'
}
}
]
I want to convert it to new array, which will be:
[
{
name:'asd',
title: 'bbb'
},
{
name:'asd',
title: 'bbb'
{
name:'asd',
title: 'bbb'
}
]
I want to take only the data elements, and to create from them a new array. How can I do it the fastest way? And how can I do it with lodash?
Thanks!
You can use _.map
var array = [{
meta: 'abc',
data: {
name: 'asd',
title: 'bbb'
}
}, {
meta: 'abc',
data: {
name: 'asd',
title: 'bbb'
}
}, {
meta: 'abc',
data: {
name: 'asd',
title: 'bbb'
}
}];
console.log(_.map(array, 'data'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.2.0/lodash.js"></script>
Just you need is the map() native javascript method to arrays:
//considere that 'someArray' is the same as your.
var newArray = someArray.map(function(item){
return {name: item.data.name, title: item.data.title};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With