Pushing an array into an object with an undefined/non-existing property If I want to &quot;push&quot; an array into my object inside of a property, I must first assure that this property exists - it works the same way for arrays too. However, what is the smoothest way of doing this?
Currently, I am first assigning the property it's first initial value so it gets made: then, since the property exists, I can push data into it.
const result = {};
const data = [{
    name: &quot;John Doe&quot;
},
{
    name: &quot;Doe John&quot;
}];

const key = &quot;helloworld&quot;;

if(result.hasOwnProperty(key)) {
  result[key].push(data);
} else {
  Object.assign(result, {
    [key]: data
  });
}

console.log(result);

Which gives me the following object.
{
    helloworld: [
        {
            name: &quot;John Doe&quot;
        }, 
        {
            name: &quot;Doe John&quot;
        }
    ]
}

What is the smoothest/better way to do this, if there is such a way?
The overall approach looks fine - check if the property exists on the object, and then either create the collection or append the data to the existing collection. While there are other methods than yours that achieve the same thing, none of them is clearly smoother or more semantically appropriate - the tools to do this more concisely just don't exist in vanilla JS yet (though there are libraries like Lodash and Underscore that implement the same sort of logic under the hood).
But, your current code has a bug: if the property already exists on the object, the data will be pushed to the existing array as a subarray, rather than pushing all elements of data to the existing array:


const result = {
  helloworld: [{
    alreadyExistingProp: 'alreadyExistingVal'
  }]
};
const data = [{
    name: "John Doe"
},
{
    name: "Doe John"
}];

const key = "helloworld";

if(result.hasOwnProperty(key)) {
  result[key].push(data);
} else {
  Object.assign(result, {
    [key]: data
  });
}

console.log(result);



You want a flat structure instead. Change
result[key].push(data);

to
result[key].push(...data);

A couple other pointers:
hasOwnProperty? Since it sounds like the values of the object will always be truthy, you can consider making a truthy test instead of using hasOwnProperty:
if (result[key]) {

Object.assign is unnecessary when setting just a single property This:
Object.assign(result, {
  [key]: data
});

can be
result[key] = data;

Mutation worries? Note that the approach used so far will mutate the result object and the data array. Mutation may cause unexpected behavior and bugs elsewhere if other parts of the script have a reference to the object that gets modified. If this is something to worry about, either clone the data before assigning it, or immutably update the result variable (like how state is updated in React).
For a short contrived example of how accidental mutation might result in a bug:


const result = {};
const addData = (data) =&gt; {
  const key = "helloworld";
  if(result.hasOwnProperty(key)) {
    result[key].push(...data);
  } else {
    result[key] = data;
  }
};

const dataFromUser = [{
    name: "John Doe"
},
{
    name: "Doe John"
}];
addData(dataFromUser);
addData([{ name: 'Default Name' }]);


// now the `result` object contains all the data
// but the dataFromUser now contains an extra object, which may be a mistake:
for (const item of dataFromUser) {
  console.log('Name from dataFromUser:', item.name);
}



This may well not be a problem for your situation, but variable contents changing unexpectedly due to mutation by code elsewhere is quite a common source of bugs in my experience.
Of course, if you understand well how references to objects work, and have no problem with (or like) the mutation that will occur, feel free to use it.