1

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

4
  • wrong object you have specified : cars =[ [id:1,color:red], [id:2,color:blue], [id:3,color:blue] ]; Commented Jun 22, 2017 at 4:08
  • Your array / object syntax is incorrect. Each car entry should probably look like {id:1,color:'red'} Commented Jun 22, 2017 at 4:12
  • 1
    Should the new object type property be the number 1 or 0, 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? Commented Jun 22, 2017 at 4:13
  • I fixed the syntax to use object literals where appropriate. I hope that's ok Commented Jun 22, 2017 at 4:19

5 Answers 5

4

You can easily map your original array to a new one

const cars = [{"id":1,"color":"red"},{"id":2,"color":"blue"},{"id":3,"color":"blue"}]

const newArray = cars.map(car => ({
  newId: car.id,
  type: car.color === 'red' ? '1' : '0'
}))

console.info(newArray)


If you need to support IE, this will work in v9+

var newArray = cars.map(function(car) {
  return {
    newId: car.id,
    type: car.color === 'red' ? '1' : '0'
  }
})

Versions of IE prior to 9 do not support Array.prototype.map. See this pollyfill if you need to support those.

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

7 Comments

Cool! this one is the best answer and working for me. I will follow this. Thank you so much.
hi phil, will be possible if you can write the if statement as string? As i need to apply this to a string problem. I just used a representation of a small sample to show my problem
@ReyNorbertBesmonte do you mean that the "1" and "0" values should be strings? If so, you were asked to clarify that here
yup.. lets make it if red = "this is red", else "this is not red"
I got it working thank you so much... It seems it has a compatibility issue on IE. It said it has syntax error. I need to find a solution for this one. Working Fine in Edge, Firefox, and Chrome.
|
1

I believe this is what you are looking for:

var newArray = [];
for(var car in cars) {
    var obj = {newId: car.id};
    if(car.color === 'red') {
        obj['type'] = '1';
    } else {
        obj['type'] = '0';
    }
    newArray.push(obj);
}

Comments

1

Given array is wrong, If your array looks like this :

cars =[
  {id:1,color:"red"},
  {id:2,color:"blue"},
  {id:3,color:"blue"}
];

then you should do this :

var newArray = cars.map((car) => { let type; if(car.color === "blue") type = 1; else type = 0; return { id: car.id, color: car.color, type }})

1 Comment

This modifies the original values. Doesn't look like OP wants to do that
0

That's what the Array Map function is for, You can create from another array.

However, your array is wrong and it has been corrected in the code below:

cars =[
    {id:1,color:'red'},
    {id:2,color:'blue'},
    {id:3,color:'blue'}
]
newCarArray=cars.map(function(car){
    if(car.color =='red'){
         return {newId:car.id, type:1}
    }
     return {newId:car.id, type:0}
})
console.log (newCarArray)

Comments

0

Try this instead your code:

     var newArray = [];
      for(var car in cars){
         if(car.color == 'red'){
             newArray.push({newId: car.id,type: '1'});
           } else {
              newArray.push({newId: car.id,type: '0'});
           }
       }

1 Comment

sorry, 2nd should be type = 0

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.