1

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));

4 Answers 4

4

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) }

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

1

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; }

Comments

0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.