0

I have an array of object. One object has an array inside.

var array = [{ a:'ai', b:'bi', c:['first','second','third']}]

I need to have above array to be like this:

[
[{ a:'ai', b:'bi', c:['first','second','third'], cSplit:'first'}],
[{ a:'ai', b:'bi', c:['first','second','third'], cSplit:'second'}],
[{ a:'ai', b:'bi', c:['first','second','third'], cSplit:'third'}]
]

I tried this code:

var array = [{ a:'ai', b:'bi', c:['first','second','third']}]
var newArr = [];
for( var i=0; i<array[0]['c'].length; i++ ){
 var innerArr = array;
 innerArr[0]['cSplit'] = array[0]['c'][i];
 newArr.push(innerArr);
}
console.log('newArr', newArr)

But it shows:

[
[{a: "ai", b: "bi", c: ['first','second','third'] , cSplit: "third"}],
[{a: "ai", b: "bi", c: ['first','second','third'] , cSplit: "third"}],
[{a: "ai", b: "bi", c: ['first','second','third'] , cSplit: "third"}]
]

Why cSplit doesn't get the first and second value?

Sven.hig
4,5292 gold badges10 silver badges18 bronze badges
asked Jul 28, 2020 at 18:53
2
  • 2
    The problem is that you're mutating the same object, which is array[0]. Commented Jul 28, 2020 at 19:05
  • @Axnyff You are right. thanks Commented Jul 28, 2020 at 19:08

4 Answers 4

1

let array = [{ a:'ai', b:'bi', c:['first','second','third']}]
let newArr = [];
for(let i = 0; i < array[0]['c'].length; i++ ) {
 newArr.push({ ...array[0], cSplit: array[0].c[i] })
}
console.log(newArr)

answered Jul 28, 2020 at 19:02
Sign up to request clarification or add additional context in comments.

Comments

1

Try with this code

var array = [{ a:'ai', b:'bi', c:['first','second','third']}]
let result = array[0].c.reduce((acc, current) => {
 return acc.concat({...array[0], csplit: current})
}, [])
console.log(result);

answered Jul 28, 2020 at 19:04

6 Comments

It looks like you're using reduce to do a map.
I use reduce to create a completely new array instead of updating the existing one
map does not mutate either
map will return a new array of course, as the array on which the map should be perform is the value of array[0].c to have access to all the value. if You perform map on the value of array directly you'll have access to one item which is an Object and not first, second and first
array[0].c.map(current => ({...array[0], csplit: current}) )
|
1

A different approach using map

var array = [{ a:'ai', b:'bi', c:['first','second','third']}]
 
var res = array.map((o)=>{return Object.values(o)[2].map(e => {
var newob = {...o}
newob.cSplit = e
 return newob
 }) 
})
 console.log(res)

answered Jul 28, 2020 at 19:11

Comments

1

This can be succinctly done with Array.prototype.map:

const array = [{ a:'ai', b:'bi', c:['first','second','third']}]
const result = array[0].c.map(c => [{...array[0], cSplit: c}])
console.log(result)

answered Jul 28, 2020 at 19:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.