Questions : adding multiple properties to object in a loop
2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00
967
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) { console.log(item.name); console.log(item.imageURL); obj['hits']['hits'] ='whyDosntWork'; console.log(item.name); 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'] ='//cdn3.iconfinder.com/data/icons/kitchen-glyph-black/2048/4834_-_Cookbook-512.png'; } console.log(item.imageURL); i++; }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
27
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:
obj['hits']['hits'][i]['_source.ActiveChannelReleases'][0]['ImageExports'][0]['Resolutions'][0]['Url']
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`...
0
2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00Answer Link
mRahman
4
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';0
2022-09-23T06:39:36+00:00 2022-09-23T06:39:36+00:00Answer Link
jidam
Start of main content
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()
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 });
Object.defineProperty()
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 };
Recommended snippets
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.