1

What I am trying to achieve

I am trying to dynamically add an array to an object. That is, starting with

 {colour: "white", type: "electric"};

and ending with

{colour: "white", type: "electric", owners: ["person1", "person2", "person3"] };

Consider the following object below

let car = {colour: "white", type: "electric"};

If I want to add another property to this object, I can check if it exist. Then add to it as follows.

if( typeof car.status === "undefined")
    car.status = "sold";

What i have tried

Using the above logic I tried the following

let car = {
  colour: "white",
  type: "electric"
};

if (typeof car.owners === "undefined")
  car.owners.push("person1");

but this doesn't work.

The question

How do I add an array to an object?

  • By adding it: `car.owners = []` (instead of using a non-existing one) – Andreas Jan 11 '20 at 11:07
  • follow this link https://stackoverflow.com/questions/59693812/how-to-dynamically-add-an-array-to-an-object/59694475#59694475 you can get your answer – Soura Ghosh Jan 11 '20 at 12:49

2 Answers2

3

To add an array to an object, you create the array and assign it to a property:

let car = {
  colour: "white",
  type: "electric"
};

if (typeof car.owners === "undefined") {
  //           vvvvvvvvvvv----- creates the array
  car.owners = ["person1"];
  // ^^^^^^^^^--- assigns it to the property
}

console.log(car);

If you want to add to that array later, you can use push. But the array has to exist first. E.g., something like:

// `person` has the person to add...
if (typeof car.owners === "undefined") {
  car.owners = [person];
} else {
  car.owners.push(person);
}

That said, it's best to avoid changing the shape of objects unnecessarily. I'd just start with an empty array and do away with the if:

let car = {
  colour: "white",
  type: "electric",
  owners: [] // <=== Empty array
};

car.owners.push("person1");

console.log(car);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

If the status property exists in the object then u have to follow the below way

let car = {colour: "white", type: "electric",status:"somethings!"};
if(car.status){
  car.status = "sold";
}
console.log(car)
// o/p { colour: 'white', type: 'electric', status: 'sold' }

If the status property does not exist in the object

let car = {colour: "white", type: "electric"};
if(car.status){
  car.status = "sold";
}
console.log(car)
// o/p { colour: 'white', type: 'electric' }

If the Array property exists inside the object then you should follow the below sample code

let car = {colour: "white", type: "electric"};
if(car.owners && car.owners.length > 0){
  // when the owners property exist and it's not an empty array 
  car.owners.push("person1");
} else {
  // when the owners property does not exist or it's an empty array
  car.owners = [];
  car.owners.push("person2");
}
console.log(car)

// o/p { colour: 'white',type: 'electric',owners: [ 'person2' ] }
Soura Ghosh
  • 879
  • 1
  • 9
  • 16
  • It's too much smart sample code rather than other sample code. It follows the ES6 version – Soura Ghosh Jan 11 '20 at 12:49
  • what if `status` was assigned to `false` initially in the object? wouldn't `if(car.status){ car.status = "sold"; }` not execute? – Personal Information Jan 11 '20 at 14:47
  • 1
    If the status is false or undefined it is not executed and output should be { colour: 'white', type: 'electric' }. My answer is most sophisticated than other answers. So, please accept my answer – Soura Ghosh Jan 11 '20 at 14:50