Omar
Omar

Reputation: 3040

combine multiple array together from the same array of objects

I have this array of objects

const line_items = [
    {
        id: 1,
        components: [
            { name: "nadia" },
            { name: "tim" },
            { name: "mark" },
            { name: "alex" }
        ]
    }, {
        id: 1,
        components: [ 
            { name: "ramsey" } 
        ]
    }
];

I want to combine all components into one array. the result would be

[
    { name: "nadia" },
    { name: "tim" },
    { name: "mark" },
    { name: "alex" },
    { name: "ramsey" }
]

my attempt

function pretty(obj) { return JSON.stringify(obj, null, 2) }

const line_items = [
    {
        id: 1,
        components: [
            { name: "nadia" },
            { name: "tim" },
            { name: "mark" },
            { name: "alex" }
        ]
    }, {
        id: 1,
        components: [ 
            { name: "ramsey" } 
        ]
    }
];

const all_components = line_items;

console.log(pretty(all_components));

Upvotes: 1

Views: 45

Answers (4)

Saurabh Yadav
Saurabh Yadav

Reputation: 3386

you can use flatMap for this.

const line_items = [
    {
        id: 1,
        components: [
            { name: "nadia" },
            { name: "tim" },
            { name: "mark" },
            { name: "alex" }
        ]
    }, {
        id: 1,
        components: [ 
            { name: "ramsey" } 
        ]
    }
];

var result = line_items.flatMap((i)=>{ return i['components'];});

console.log(result);

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386604

You could use reduce the items.

const
    line_items = [{ id: 1, components: [{ name: "nadia" }, { name: "tim" }, { name: "mark" }, { name: "alex" }] }, { id: 1, components: [{ name: "ramsey" }] }],
    result = line_items.reduce((r, { components }) => [...r, ...components], []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Maheer Ali
Maheer Ali

Reputation: 36574

You can use reduce() and pass components to concat() using Rest Parameters.

const line_items = [
    {
        id: 1,
        components: [
            { name: "nadia" },
            { name: "tim" },
            { name: "mark" },
            { name: "alex" }
        ]
    }, {
        id: 1,
        components: [ 
            { name: "ramsey" } 
        ]
    }
];

let allcomps = line_items.reduce((ac,a) => ac.concat(...a.components),[])
console.log(allcomps)

Upvotes: 1

Ori Drori
Ori Drori

Reputation: 191976

You can use Array.flatMap() (not supported by IE/Edge):

const line_items = [{"id":1,"components":[{"name":"nadia"},{"name":"tim"},{"name":"mark"},{"name":"alex"}]},{"id":1,"components":[{"name":"ramsey"}]}];

const all_components = line_items.flatMap(o => o.components);

console.log(pretty(all_components));

function pretty(obj) { return JSON.stringify(obj, null, 2) }

And if flatMap is not supported you can use Array.map() with spread and Array.concat():

const line_items = [{"id":1,"components":[{"name":"nadia"},{"name":"tim"},{"name":"mark"},{"name":"alex"}]},{"id":1,"components":[{"name":"ramsey"}]}];

const all_components = [].concat(...line_items.map(o => o.components));

console.log(pretty(all_components));

function pretty(obj) { return JSON.stringify(obj, null, 2) }

Upvotes: 4

Related Questions