简体   繁体   中英

How to merge multiple array in angular

here's the data:

collection = [{
    date: '2020-12-01',
    data: [{
        id: 'A1',
        name: 'A1',
        date: '2020-12-01'
    },{
        name: 'A2',
        date: '2020-12-01'
    },{
        name: 'A3',
        date: '2020-12-01'
    }]
},{
    date: '2020-10-02',
    data: [{
        name: 'B1',
        date: '2020-10-02'
    },{
        name: 'B2',
        date: '2020-10-02'
    },{
        name: 'B3',
        date: '2020-10-02'
    }]
},{
    date: '2020-10-03',
    data: [{
        name: 'C1',
        date: '2020-10-03'
    },{
        name: 'C2',
        date: '2020-10-03'
    },{
        name: 'C3',
        date: '2020-10-03'
    }]
}];


    collection.map((DATA: any) => {

        _COL = _.cloneDeep(DATA['data']);



    })

how to merge the multiple array in _COL . I'm getting error when I try to use the findIndex and equal it to the inputed id and it doesn't display.

You can use array#flatMap .

 const collection = [{ date: '2020-12-01', data: [{ id: 'A1', name: 'A1', date: '2020-12-01' },{ name: 'A2', date: '2020-12-01' },{ name: 'A3', date: '2020-12-01' }] },{ date: '2020-10-02', data: [{ name: 'B1', date: '2020-10-02' },{ name: 'B2', date: '2020-10-02'},{ name: 'B3', date: '2020-10-02' }] },{ date: '2020-10-03', data: [{ name: 'C1', date: '2020-10-03' },{ name: 'C2', date: '2020-10-03' },{ name: 'C3', date: '2020-10-03' }] }], result = collection.flatMap(({data}) => data); console.log(result);

You can use array#map and array#concat .

 const collection = [{ date: '2020-12-01', data: [{ id: 'A1', name: 'A1', date: '2020-12-01' },{ name: 'A2', date: '2020-12-01' },{ name: 'A3', date: '2020-12-01' }] },{ date: '2020-10-02', data: [{ name: 'B1', date: '2020-10-02' },{ name: 'B2', date: '2020-10-02'},{ name: 'B3', date: '2020-10-02' }] },{ date: '2020-10-03', data: [{ name: 'C1', date: '2020-10-03' },{ name: 'C2', date: '2020-10-03' },{ name: 'C3', date: '2020-10-03' }] }], result = [].concat(...collection.map(({data}) => data)); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM