javascript named array code example

Example 1: javascript init associative array

var arr = { "one": 1, "two": 2, "three": 3 };

Example 2: javascript array

//create an array like so:
var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}

Example 3: javascript array read object value in array

var events = [
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 1
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 2
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 3
  }
];
console.log(events[0].place);

Example 4: javascript associative array

/*
Associative array in javascript
There are multiple programming supports array and arrays is having different types i.e
1. Index Array
2. Associative Array
3. Multidimentional Array

But as question asked javascript does not support associative array with named indexes.
Javascript only use numbered indexes in arrays.
*/

Example
var studentDetail = [];
studentDetail[0] = "Code";
studentDetail[1] = "Grepper";
studentDetail[2] = 24;
studentDetail[3] = 'Mumbai';
var x = studentDetail.length;     // studentDetail.length will return 4
var y = studentDetail[1];         // studentDetail[0] will return "Grepper"

/*
I hope it will help you.
Namaste
Stay Safe Stay Home
*/