Skip to content

All topics  /  jQuery

JQuery - How To Push Specific Key And Value In Array?

jQuery ·

When “JQuery - How To Push Specific Key And Value In Array?” moves through design, development and browser testing, practical pay group can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.

For API changes, browser support and current usage patterns, compare the snippet with the official jQuery website before adopting it in production.

JQuery - How To Push Specific Key And Value In Array?

Hi Dev,

Now let's see example of how to push specific key and value in array using jquery. We will talk about insert specific key and value from array in jquery. This blog will give you simple and easy way to push key and value in array using jquery.

In this example, i will let you know how to push specific key with value as array 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.

Here i will give you two example for how to push specific key and value in array using jquery. So let's see the bellow example:

Example 1 :


<!DOCTYPE html>
<html>
<head>
    <title>JQuery - How to push specific key and value in array? - NiceSnippets.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" : "Rahul", "lastName" : "Shah" }, 
            { "id" : "2", "firstName" : "Pratik", "lastName" : "Gupta" }, 
            { "id" : "3", "firstName" : "Jayesh", "lastName" : "Pandya" }, 
            { "id" : "4", "firstName" : "Raghav", "lastName" : "Patel" }
        ];

        
        var myobj = {};
        $.each(myArray,function(i,value){
            myobj[value.id] = value.firstName;
        });
        console.log(myobj);
    </script>
</body>
</html>

Example 2 :

<!DOCTYPE html>
<html>
<head>
    <title>JQuery - How to push specific key and value in array? - NiceSnippets.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" : "Rahul", "lastName" : "Shah" }, 
            { "id" : "2", "firstName" : "Pratik", "lastName" : "Gupta" }, 
            { "id" : "3", "firstName" : "Jayesh", "lastName" : "Pandya" }, 
            { "id" : "4", "firstName" : "Raghav", "lastName" : "Patel" }
        ];

        
        var myobj = [];
        $.each(myArray, function (i, value) {
            myobj.push({firstName: value.firstName, lastName: value.lastName});
        });

       
        console.log(myobj);
    </script>
</body>
</html>

It will help you....