Languages

JavaScript - push to array

3 points
Asked by:
Cassia
1265

How can I push new values to the following array?

I've tried:

But it doesn't work.

1 comments
Root-ssh
2y
Hint: in your case, object naming as `json` is incorrect. JSON term is used to describe way of objects creation (JavaScript Object Notation). You can call as JSON, text/string that stores object description.
Add comment
1 answer
3 points
Answered by:
Admin Dirask Community
4380

This is not an array.

It's an object and what you are trying to do is to add a key/value pair to the object.

You can do it in two ways:

or:

You can also do it as an array but it requires a different syntax:

 

References

  1. Property accessors - JavaScript | MDN
  2. Array.prototype.push() - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join