• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Adding a function to add new objects and properties in javascript

Resolved: Adding a function to add new objects and properties in javascript

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

Am stuck trying to add a function to add a new object to a const in javascript
I am aware that I can initiate a new object via
But other than that I have trouble understanding how to add properties on that new object.
Normally a simple:
But that approach would only generate the following which is not what I need:
As I need those to be under new objects with the name variable.

Answer:

Since it is objects inside objects, you can use [name][mobile] to access the mobile property of the corresponding name. When creating a new name, you have to use if-else conditions to first check if it exists. Note that [numberType] is used to create a dynamic key (where it creates a key with name as value of numberType).


If you have better answer, please add a comment about this, thank you!

javascript javascript-objects
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: correct policy to get secrets on local go app

22/03/2023

Resolved: Vue 3 watchEffect dependency tracking after asynchronous code

22/03/2023

Resolved: Watching state object in pinia doesn’t fire when object changes

22/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.