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

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

programmeradmin7浏览0评论

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

评论列表(0)

  1. 暂无评论