JQuery – How to push specific key and value in array?

JQuery – How to push specific key and value in array?

In this post we will give you information about JQuery – How to push specific key and value in array?. Hear we will give you detail about JQuery – How to push specific key and value in array?And how to use it also give you demo for it if it is necessary.

In this example, i will let you know how to push specific key with value as array in jquery array. we can add dynamically push key value pair in jquery array.

As we know if we use push method into an array then you can not specify key for value. it will create automatically 0 1 2 3 etc, but if you want to push both key and value then you can not specify key, but in this example i will show you how to create array with specific key value.

So, here i am going to share simple example, so you can check it:

Example 1:

<!DOCTYPE html>

<html>

<head>

<title>JQuery - How to push specific key and value in array? - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>

<script type="text/javascript">

var myArray = [

{ "id" : "1", "firstName" : "Hardik", "lastName" : "Savani" },

{ "id" : "2", "firstName" : "Vimal", "lastName" : "Kashiyani" },

{ "id" : "3", "firstName" : "Harshad", "lastName" : "Pathak" },

{ "id" : "4", "firstName" : "Harsukh", "lastName" : "Makawana" }

];

var myObj = {};

$.each(myArray, function (i, value) {

myObj[value.id] = value.firstName;

});

console.log(myObj);

</script>

</body>

</html>

Example 2:

Also see:Remove Duplicate Objects from Array Jquery Example

<!DOCTYPE html>

<html>

<head>

<title>JQuery - How to push specific key and value in array? - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>

<script type="text/javascript">

var myArray = [

{ "id" : "1", "firstName" : "Hardik", "lastName" : "Savani" },

{ "id" : "2", "firstName" : "Vimal", "lastName" : "Kashiyani" },

{ "id" : "3", "firstName" : "Harshad", "lastName" : "Pathak" },

{ "id" : "4", "firstName" : "Harsukh", "lastName" : "Makawana" }

];

var myObj = [];

$.each(myArray, function (i, value) {

myObj.push({firstName: value.firstName, lastName: value.lastName});

});

console.log(myObj);

</script>

</body>

</html>

I hope it can help you…

Hope this code and post will helped you for implement JQuery – How to push specific key and value in array?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

9  +    =  15

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US