0

I have an array of object which looks like:

const orderp = [{id: "1", items:[{id:"1", Name: "Peter"}, {id:"2", Name: "John"}]}, {id: "2", items:[{id:"1", Name: "Teresa"}, {id:"2", Name: "stephan"}]}, {id: "3", items:[{id:"1", Name: "stuart"}, {id:"2", Name: "Jeny"}]}]

Now, For this I am trying to create an combine an array which will have only items object values only.

Desired output would be :

const names = [{id:"1", Name: "Peter"}, {id:"2", Name: "John"},{id:"1", Name: "Teresa"}, {id:"2", Name: "stephan"}, {id:"1", Name: "stuart"}, {id:"2", Name: "Jeny"}]

For this I tried:

let names = []
for (order of orderp) {
  for(name of order['items']) {
    names.push(name)
}

}

One more thing thing that would be using iterator of number:

for(let i =0; i <= orderp.length-1; i ++){

}

like this.

But is there any way to do this?

Thanks.

1
  • The first snippet should work. Do you have any errors? Commented Sep 15, 2020 at 13:14

3 Answers 3

1

You can use reduce and inside callback use concat to concat the items array with accumulator array

let data = [{
  id: "1",
  items: [{
    id: "1",
    Name: "Peter"
  }, {
    id: "2",
    Name: "John"
  }]
}, {
  id: "2",
  items: [{
    id: "1",
    Name: "Teresa"
  }, {
    id: "2",
    Name: "stephan"
  }]
}, {
  id: "3",
  items: [{
    id: "1",
    Name: "stuart"
  }, {
    id: "2",
    Name: "Jeny"
  }]
}];


const newData = data.reduce((acc, curr) => {
 return acc.concat(curr.items)
}, []);
console.log(newData)

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

Comments

0

You can use .flatMap() for this purpose, see from the documentation:

The flatMap() method returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1, but slightly more efficient than calling those two methods separately.

Try as the following:

const orderp = [{id: "1", items:[{id:"1", Name: "Peter"}, {id:"2", Name: "John"}]}, {id: "2", items:[{id:"1", Name: "Teresa"}, {id:"2", Name: "stephan"}]}, {id: "3", items:[{id:"1", Name: "stuart"}, {id:"2", Name: "Jeny"}]}]
const names = orderp.flatMap(e => e.items)
console.log(names)

Comments

0

It can be done through array map function as well

const orderp = [
    { id: '1', items: [ { id: '1', Name: 'Peter' }, { id: '2', Name: 'John' }]},{ id: '2', items: [ { id: '1', Name: 'Teresa' }, { id: '2', Name: 'stephan' } ] },{ id: '3', items: [ { id: '1', Name: 'stuart' }, { id: '2', Name: 'Jeny' } ] }];
        let arr = [];
        orderp.map((item) => {
            if (item['items']) {
                if (Object.entries(item['items'].length > 1)) {
                    Object.entries(item['items']).map((item) => {
                        arr.push(item[1]);
                    });
                } else {
                    arr.push(item[1]);
                }
            }
        });
        console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.