ItSolutionStuff.com

JQuery - How to Push Specific Key and Value in Array?

By Hardik Savani • July 3, 2023
Javascript jQuery

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:

<!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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove Duplicate Object from Array in JQuery?

Read Now →

How to Remove Duplicate Value from Array in JQuery?

Read Now →

JQuery QR Code Scanner using Instascan JS Example

Read Now →

JQuery Ajax CRUD Operations in PHP MySQL Example

Read Now →

PHP Ajax Drag and Drop Sorting Table Rows Example

Read Now →

Codeigniter JQuery Ajax Image Upload Example

Read Now →

PHP Ajax Infinite Scroll Pagination Example

Read Now →

Bootstrap - Input multiple tags example using Tag Manager Jquery Plugin

Read Now →

JQuery Accordion using JQuery UI Example

Read Now →

PHP Crop Image Before Upload using Croppie JS Example

Read Now →

JQuery Tooltip Example using JQuery UI Plugin

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to Check Undefined, Empty and Null in JQuery?

Read Now →

How to Convert Line Breaks to br in jQuery ?

Read Now →