ganesh kaspate
ganesh kaspate

Reputation: 2695

Create a new array of object by pushing elements in the array JavaScript

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.

Upvotes: 0

Views: 58

Answers (3)

Ahmad Suddle
Ahmad Suddle

Reputation: 190

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

Upvotes: 0

brk
brk

Reputation: 50346

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)

Upvotes: 1

norbitrial
norbitrial

Reputation: 15176

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)

Upvotes: 0

Related Questions