Warning: session_start(): open(/tmp/sess_a8ec2f5a743bef30cae52fe3ede6ff42, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Add A Key/Value Pair To All Objects In Array In JavaScript - LearnShareIT

How To Add A Key/Value Pair To All Objects In Array In JavaScript

add a keyvalue pair to all objects in array in JavaScript

Javascript supports many methods of manipulating arrays, such as concat(), forEach(), map(),…v. This tutorial will use loop and methods to add a key/value pair to all objects in array in JavaScript.

How To Add A Key/Value Pair To All Objects In Array In JavaScript?

Method 1: Use For loop

This is an effortless, easy-to-implement way to add key/value to objects in an array. Almost all methods of manipulating arrays in Javascript can use loops.

However, to use the loop, we can find a condition for the for loop to stop and perform the assignment of the remaining values ​​through a new variable.

For example, I have an array myArray, and I want to insert a key/value pair into each Object. I will do the following:

var myArray = [{ name: 'Alma'},
		 { name: 'Doris'},
 	 	 { name: 'Lani'}];

for (let i = 0; i < myArray.length; i++) {
    myArray[i].age = i+18
}  

console.log(myArray)

Output:

0: {name: 'Alma', age: 18}
1: {name: 'Doris', age: 19}
2: {name: 'Lani', age: 20}

Depending on your rules, you can manipulate strings in many different ways. I highly recommend this way because it helps to understand how the built-in methods work.

Method 2: Use Array.map()

This is a built-in method in Javascript. It is often used for convenience and doesn’t mutate the old string. Below I will give the definition, usage, syntax, parameters, and some examples for you to understand and use.

Definition and Usage

map() creates a new array by calling a function for every array element.

map() calls a function once for each element in an array.

map() does not execute the function for empty elements.

map() does not change the original Array.

Syntax

array.map(function(currentValue, index, arr), thisValue)

Parameters – Description

  • function(): Indispensable because this function is traversed by all Array elements once.
  • currentValue:  Indispensable because it is the value of the Array executing the function.
  • index: Unnecessary. This is the address of the element array that is executing the function.
  • arr: Unnecessary. This is the Array of the element array that is executing the function.
  • thisValue: Unnecessary.Default value undefined. A value passed to the function is to be used as this value.

Get back the myArray Array in method one and insert a key/value pair into each Object using map(). I do the following:

var myArray = [{ name: 'Alma'},
		 { name: 'Doris'},
 	 	 { name: 'Lani'}];

var newArray = myArray.map(function(currentArray,index)
{
  currentArray.index = index;
  return currentArray;
});

console.log(newArray);

Output:

0: {name: 'Alma', index: 0}
1: {name: 'Doris', index: 1}
2: {name: 'Lani', index: 2}

In this example, I create a new array and add key indexOf. The value is the Object’s address in the Array.

Summary

There are many ways to add a key/value pair to all objects in array in JavaScript. In this article, we have introduced you to the Array.map() method and using a for loop, please see the guide carefully.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *