How to add multiple properties to object in javascript

Questions : adding multiple properties to object in a loop

2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00


I think my question is easy but I dont anycodings_node.js understand why my solution doesnt work anycodings_node.js :( I'm trying to add to a new object some anycodings_node.js long properties using foreach loop but I'm anycodings_node.js getting the whole time error. Could someone anycodings_node.js please help me?

let i = 0;
const obj = {};
for (const item of read) {
  obj['hits']['hits'] ='whyDosntWork';
  if (item.imageURL) {
    obj['hits']['hits'][i]['_source.ActiveChannelReleases'][0]['ImageExports'][0]['Resolutions'][0]['Url'] =getServerURL()+item.imageURL;
  } else {
    obj['hits']['hits'][i]['_source.ActiveChannelReleases'][0]['ImageExports'][0]['Resolutions'][0]['Url'] ='';

I have an response and I want to mock it anycodings_node.js with my data

I wish to have for example an object that I anycodings_node.js can fill with data:

class ResponseController {
  constructor() {
    this.response = {
      'hits': {
        'hits': [{'_source.ActiveChannelReleases': [{'ImageExports': ['Resolutions']}],

module.exports = ResponseController;

Will it work if I write

obj = new ResponseController();

and then I can easily add variables from the anycodings_node.js looo?

Total Answers 2


Answers 1 : of adding multiple properties to object in a loop

First this is a madness :).

Why is not working?

const obj = {};

you cannot do this obj['hits']['hits'] anycodings_node.js ='whyDosntWork'; due to obj['hist'] does anycodings_node.js not exists.

You need to do:

obj['hits'] = {}

and then obj['hits']['hits'] anycodings_node.js ='whyDosntWork';

And the same for the rest...

I cannot understand what do you want to anycodings_node.js do here:


But follow what I said before, you need anycodings_node.js to create each step the value you want. anycodings_node.js I can assume that you want an array in anycodings_node.js ´hits`...


2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00Answer Link



Answers 2 : of adding multiple properties to object in a loop

The issue is that you're defining obj to anycodings_node.js be an object, and then are trying to add anycodings_node.js stuff into obj.hits without defining anycodings_node.js that as an object

const obj = {};
obj['hits'] = {}
obj['hits']['hits'] ='whyDosntWork';


2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00Answer Link


Start of main content

How to add multiple properties to object in javascript

Adding a key-value pair to a JavaScript object is straightforward, yet there are multiple ways available to do so. While mostly similar, these approaches have some differences that you should be aware of.

Dot notation

The most common and straightforward way to add a key-value pair to an object is to use the dot notation. You have probably already used this in the past, and it's sufficient in most situations you will encounter.

const obj = { a: 1 };
obj.b = 2;
obj.c = 3;

Square bracket notation

Similar to dot notation, square bracket notation comes in handy when dealing with dynamic keys, but can also work with static keys. Apart from that, it's exactly the same as dot notation both in functionality and performance.

const obj = { a: 1 };
const bKey = 'b';
obj[bKey] = 2;
obj['c'] = 3;


Object.assign() is slightly different than the previous two options. It can be used to add multiple properties to an object at once and it can also shallow merge two or more objects. It is not as performant, however, so it should only be used when necessary.

const obj = { a: 1 };
Object.assign(obj, { b: 2 }, { c: 3 });


Another, less-common, way to add a key-value pair to an object is to use Object.defineProperty(). This is the lest performant way to add a key-value pair to an object, but it allows the new property to be precisely defined. This function accepts either a data or accessor descriptor as its second argument, allowing the behavior of the new property to be customized as desired. Bear in mind that you can add multiple properties at once, using Object.defineProperties().

const obj = { a: 1 };
Object.defineProperty(obj, 'b', {
  value: 2,
  enumerable: true,
  configurable: true,
  writable: true
Object.defineProperty(obj, 'c', {
  value: 3,
  enumerable: true,
  configurable: true,
  writable: true

Object spread operator

Last but not least, there's the object spread operator (...). Contrary to previous methods, this one doesn't mutate the original object, but instead returns a new object with the added properties. As expected, the performance of this approach is significantly worse than previous ones, due to the need to create a new object.

const obj = { a: 1 };
const newObj = { ...obj, b: 2, c: 3 };

  • Inverts the key-value pairs of an object, without mutating it.

  • Omits the key-value pairs corresponding to the keys of the object for which the given function returns falsy.

  • Creates an object from the given key-value pairs.

Can you set multiple attributes in JavaScript?

To set multiple attributes for an element at once with JavaScript, we can call the element's setAttribute method.

How many properties can a JavaScript object have?

JavaScript objects have two types of properties: data properties and accessor properties.

How do you set multiple attributes to an element?

To set multiple attributes at once on an element:.
Add the attribute names and values to an object..
Use the Object. keys() method to get an array of the object's keys..
Use the forEach() method to iterate over the array..
Use the setAttribute() method to set each attribute on the element..

How do you add multiple objects in JavaScript?

Push multiple Values to an Array in JavaScript.
Use the Array. push() method to push multiple values to an array, e.g. arr. push('b', 'c', 'd'); . ... .
Use the spread syntax to push multiple values to an array, e.g. arr = [... arr, 'b', 'c', 'd']; . ... .
Use the Array. splice() method to push multiple values to an array, e.g. arr..