Navigate back to the homepage

how to set value in json object in javascript?

Infinitbility
JavaScript
July 30th, 2022 · 1 min read
how to set value in json object in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To set value in JSON object in javascript, we have multiple ways

  1. Set the value at the time of creating JSON object
  2. Set value using the key in JSON object
  3. Set value using dynamic key in JSON object

Let’s take a short example of all the above methods.

1// Set value at the time of creating JSON object
2let obj = {
3 id: 1
4}
5
6// Set value using key in json object
7obj.name = "infinitbility";
8
9// Set value using dynamic key in JSON object
10let key = "status";
11obj[key] = true;
12
13console.log(obj)
14// 👇️ Output
15// { id: 1, name: 'infinitbility', status: true }

Today, I’m going to show you How do I set value in a JSON object in javascript, as above mentioned, I’m going to use all the above-mentioned methods.

Let’s start today’s tutorial how do you assign value in a JSON object in javascript?

Set value at the time of creating JSON object

To set value at the time of the creation of an object, we have colon syntax to write key and value.

In this example, I’m creating an object with id key-value pair.

1let obj = {
2 id: 1
3}
4
5console.log(obj)
6// 👇️ Output
7// { id: 1 }

Set value using the key in a JSON object

To set the value using the key in javascript, javascript provide dot syntax, using this syntax we can create key-value pair in exiting JSON object.

In this below example, we have used the above existing object and added new key-value pair using dot syntax.

1let obj = {
2 id: 1
3}
4
5obj.name = "infinitbility";
6
7// 👇️ Output
8// { id: 1, name: 'infinitbility' }

Set value using the dynamic key in a JSON object

To value using the dynamic key in a JSON object, javascript provides bracket syntax where we have to mention the key in the bracket and equal to value.

In this below example, we have created a key variable and used it for creating key-value pair in JSON object.

1let obj = {
2 id: 1
3}
4
5let key = "status";
6obj[key] = true;
7
8console.log(obj)

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to check if a string contains only whitespace in typescript?

how to check if a string contains only whitespace in typescript?

TypeScript, check if a string contains only whitespace example with a step-by-step guide.

July 28th, 2022 · 1 min read
how to get length of numbers in typescript?

how to get length of numbers in typescript?

TypeScript, get the length of numbers example with a step-by-step guide.

July 28th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]