Push associative array into array in JavaScript

Push associative array into array in JavaScript

·

3 min read

let myArray = [];

function insert(key, value) {
  let obj = {};
  obj[key] = value;
  myArray.push(obj);
}

insert("name", "John");
insert("age", 30);
insert("city", "New York");

console.log(myArray);

In this example, we declare an empty array called myArray and a function called insert that takes two parameters: key and value. Inside the insert function, we create an empty object called obj. We then use square bracket notation to set a property on obj with the name specified by the key parameter and the value specified by the value parameter.

For example, when we call insert("name", "John"), it sets the property name on obj with the value "John". Similarly, when we call insert("age", 30), it sets the property age on obj with the value 30.

Finally, we push obj into the myArray array, which now contains a list of objects with dynamically named properties.

Note that you can use any variable as the key parameter in the insert function, as long as it is a string or can be converted to a string. This allows you to dynamically set property names based on user input, API responses, or any other source of data.


let myArray = [
  { name: "John", age: 30 },
  { name: "Jane", age: 25 },
  { name: "Bob", age: 40 }
];

myArray.forEach(function(element, index) {
  console.log("Element at index " + index + ":");
  Object.keys(element).forEach(function(key) {
    console.log("  " + key + ": " + element[key]);
  });
});

In this example, we declare an array called myArray that contains three objects, each with a name and age property. We then use the forEach() method to iterate over each element in myArray.

For each element, we use the Object.keys() method to get an array of all the keys (property names) in the object. We then use another forEach() method to iterate over each key and log both the key and value to the console.

The element parameter in the outer forEach() callback function represents the current element being processed, and the index parameter represents the index of the current element in the array. The inner forEach() callback function iterates over the keys of the current element, and the key parameter represents the current key being processed.

So for each element in the array, this code logs each key-value pair to the console like this:

Element at index 0:
  name: John
  age: 30
Element at index 1:
  name: Jane
  age: 25
Element at index 2:
  name: Bob
  age: 40

You may Write the foreach in jquery instead of vanilla JavaScript

In this example, we declare an array called myArray that contains three objects, each with a name and age property. We then use the $.each() function to iterate over each element in myArray.

For each element, we use the $.each() function again to iterate over each key-value pair in the object and log both the key and value to the console.

The index parameter in the outer $.each() callback function represents the index of the current element in the array, and the element parameter represents the current element being processed. The inner $.each() callback function iterates over the key-value pairs of the current element, and the key and value parameters represent the current key-value pair being processed.

So for each element in the array, this code logs each key-value pair to the console like this:

$.each(myArray, function(index, element) {
  console.log("Element at index " + index + ":");
  $.each(element, function(key, value) {
    console.log("  " + key + ": " + value);
  });
});
Element at index 0:
  name: John
  age: 30
Element at index 1:
  name: Jane
  age: 25
Element at index 2:
  name: Bob
  age: 40

You can modify this code to suit your specific use case, such as performing some operation on each key-value pair instead of logging it to the console.