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?
car.owners = [](instead of using a non-existing one)