Navigate back to the homepage

How to add key and value in array of objects in javascript?

Infinitbility
JavaScript
November 26th, 2021 · 1 min read
How to add key and value in array of objects in javascript?

Hello Friends 👋,

Welcome To Infinitbility! ❤️

JavaScript array of objects, everyone uses to pass and store multiple details in the array.

now, we are going to learn when we have already an array of objects and we want to add one more property in all array of objects or in a single object of the array then how to do.

let start today’s tutorial How to add key and value in an array of objects in javascript?

before jumping to how we can add key and value, let create an example array of objects which we can use in examples.

1let arrOfObj = [
2 { id: 1, name: 'infinit' },
3 { id: 2, name: 'bility' },
4 { id: 3, name: 'infinitbility' },
5];
6
7console.log("arrOfObj", arrOfObj);

First, let take an example of adding the is_active key with value in every array of objects.

1const newArrOfObj = arrOfObj.map(
2 (e) => ({ ...e, is_active: true })
3);
4
5console.log('newArrOfObj', newArrOfObj);

now, try to change any single object with an object index.

1arrOfObj[2].age = "infinity";
2
3console.log('arrOfObj', arrOfObj);

Here, our whole code practice with output.

1let arrOfObj = [
2 { id: 1, name: 'infinit' },
3 { id: 2, name: 'bility' },
4 { id: 3, name: 'infinitbility' },
5];
6
7console.log('arrOfObj', arrOfObj);
8
9// change every objects of array
10const newArrOfObj = arrOfObj.map(
11 (e) => ({ ...e, is_active: true })
12);
13
14console.log('newArrOfObj', newArrOfObj);
15
16
17// change single object of array
18arrOfObj[2].age = "infinity";
19
20console.log('arrOfObj', arrOfObj);

Output

Array of objects, Javascript, Example

Thanks for reading…

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

Tutorials

Categories

More articles from Infinitbility

How to create a ref of the functional component in React?

How to create a ref of the functional component in React?

Example of creating ref of custom functional componant.

November 18th, 2021 · 1 min read
How to encrypt SQLite database?

How to encrypt SQLite database?

Example of SQLite encryption using SQLcipher and SQLite DB browser.

November 17th, 2021 · 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]