Is there a way to populate an array with if condition inside?
example
cars = [
{id:1,color:'red'},
{id:2,color:'blue'},
{id:3,color:'blue'}
]
I need to create a new array that will get the value of ID and a new field which is type.
if color = red, type = 1, else type = 0.
I tried this one but it will not work for javascript.
var newArray = [
for(var car in cars){
newId: car.id,
if(car.color == 'red'){
type: '1',
else
type: '0'
}
]
the return value should be
newArray = [
{newId:1,type:'1'},
{newId:2,type:'0'},
{newId:3,type:'0'}
]
Help please
{id:1,color:'red'}typeproperty be the number1or0, or the string"1"or"0"? Your question shows it both ways. "dont mind the syntax this is just a representation" - Why would you not use standard JS object literal syntax as shown in all of the answers?