javascript - How to create a new array from child object properties - Stack Overflow

I have an array:[{meta:'abc'data:{name:'asd',title: 'bbb'}},{meta:'a

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!

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!

Share Improve this question edited Feb 3, 2016 at 15:40 user663031 asked Feb 3, 2016 at 15:34 user3712353user3712353 4,2194 gold badges22 silver badges35 bronze badges 4
  • Why do you care how fast it is? Are you programming on a Commodore 64? – user663031 Commented Feb 3, 2016 at 15:40
  • I'd assume that the OP means "the cleanest way". – isherwood Commented Feb 3, 2016 at 15:40
  • Where are you stuck? Do you not know how to loop over an array and do something with each of its elements? Do you not know the equivalent lodash utility? Are you stuck writing the logic for what to do with each element, namely extracting its data property? By the way, you tagged this "JSON", but it has nothing to do with JSON, which a string-based format for exchanging information. – user663031 Commented Feb 3, 2016 at 15:44
  • 1 I mean the cleanest way. Got an answer. – user3712353 Commented Feb 3, 2016 at 15:53
Add a ment  | 

2 Answers 2

Reset to default 6

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744096072a4557963.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信