0

I am trying to create an Array in Javascript with some key:value pair. My initial array looks likes this:

var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10
}

Now there is a checkbox, upon which upon checking I need to add more data to array. Say I check a box and need to update the array with its value and make the array look like this:

var data = {
isPublic : true,
distance : 500,
noOfPlayer : 10,
typePlay: 'amateur'
}

Can this be done, because I tried creating object with Key:value and pushing it to the array, but it gives and output as below:

var data = 
[
{
  isPublic : true,
  distance : 500,
  noOfPlayer : 10,
},
{
  typePlay: 'amateur'
}
]

Please suggest.

Thanks, Ayush

Aye Jae
  • 63
  • 6

1 Answers1

0

Use object["property"] = value or object.property = value to add a new property with its value

var data = {
"isPublic" : true,
"distance" : 500,
"noOfPlayer" : 10
};

data["typePlay"] = "amateur";
data.typePlay2 = "amateur";

console.log(data);
Weedoze
  • 13,683
  • 1
  • 33
  • 63