Reading only the first value in JavaScript

  • 0
  • Pump00:00

I am trying to add the below body to JSON. But I only see one value is being added.

json = { "Myname":"Nate",  }

Adding the below code:

Body = Myproperty: [{
  "vehicle": "car",
  "color": "Red"
}, {
  "name": "van",
  "color": "white"
}, {
  "name": "Truck",
  "color": "Blue"
}]

Here is the code I am using:

for (var i = 0; i < Myproperty.length; i++) {
  json.mycarname = Body.Myproperty[i].name;
  json.mycolor = Body.Myproperty[i].color;
}

The end result should look like this:

{
  "Myname": "Nate",
  mycarname: "car",
  "mycolor": "Red"
},
{
  "Myname": "Nate",
  mycarname: "van",
  "mycolor": "white"
},
{
  "Myname": "Nate",
  mycarname: "Truck",
  "mycolor": "Blue"
}
Default Asked on February 7, 2020 in Programming.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.