Merrin K
Merrin K

Reputation: 1790

How can I create a two dimensional array with index and with key in javaScript?

I want to create a two dimensional array with index and with key in JavaScript, each row has the index and column has the key

myArray[0]=["S.No":"1", "Name":"abcd", "Age":"12"];
myArray[1]=["S.No":"2", "Name":"efgh", "Age":"15"];
myArray[2]=["S.No":"3", "Name":"ijkl", "Age":"20"];

So the output Should be

Array
(
[0] => Array
    (
    [S.No] => 1
    [Name] => abcd
    [Age] => 12
    )

[1] => Array
    (
    [S.No] => 2
    [Name] => efgh
    [Age] => 15
    )

[2] => Array
    (
    [S.No] => 2
    [Name] => ijkl
    [Age] => 20
    )
)

How to do this in JavaScript?

Want an array like this for php from javaScript.

Upvotes: 1

Views: 1237

Answers (4)

A Haworth
A Haworth

Reputation: 36575

JavaScript doesn't have arrays with named indexes so you can't do something like myArray[0]['Name'] You could have a two dimensional array, an array with arrays as its entries, but they would be accessed for example by myArray[0][1]

Luckily in JavaScript there is the concept of object (and in fact arrays are special types of object). You can have an array of objects and it looks as though that is what you want from what you have given which is:

myArray[0]=["S.No":"1", "Name":"abcd", "Age":"12"];
myArray[1]=["S.No":"2", "Name":"efgh", "Age":"15"];
myArray[2]=["S.No":"2", "Name":"ijkl", "Age":"20"];

You can define an object like this obj={ 'S_No': '12', 'Name': 'abcd', 'Age': '20' }

In your case you could do:

<script>
var myArray=[];
myArray[0]={'S_No': '1','Name': 'abcd','Age': '12'};

console.log(myArray);
console.log(myArray[0].Name);
console.log(myArray[0].S_No);
</script>

Note that you cannot have . in S.No so I have replaced it with an underscore.

Upvotes: 1

ghayoornaqvi
ghayoornaqvi

Reputation: 108

You can create multi dimensional arrays just like other languages. The difference is that you need to initialize a new array at index.

let a = [];
for ( let i=0; i<5; i++){
  // Created a new array on index of 1st dimension.
  a[ i ] = [];
  for ( let j=0; j < 2; j++ ) {
    // Here you can add elements to your 2nd dimension.
    a[i][j] = {'a':1};
    // As per your specific requirement, You can do it in following way
    a[i]['Name'] = 'Username';
  }
}
console.log ('a ', a);
console.log('User name', a[0]['Name']);

Upvotes: 1

Hitesh Kumar
Hitesh Kumar

Reputation: 514

The basic code to create such array:

var obj=[];
var array={};
//loop the resulted data OR hard code and push it
for(loop the data){
array.sno = "1"; //data.sno
array.Name = "abcd"; //data.Name
array.Age = "12"; //data.Age
obj.push(array);
}

when you try console.log(obj[0]) it will print the 0th element

Upvotes: 0

michaelT
michaelT

Reputation: 1711

You can not create an array with keys, because this is not the way an array works. But you can nest an object (key-value-pair) into your array.

myArray[0] = {
    "S-No": 1, "Name" : "abcd", "Age": 12
};
myArray[1] = {
    "S-No": 2, "Name" : "efgh", "Age": 12
};
myArray[2] = {
    "S-No": 3, "Name" : "ijkl", "Age": 20
};

When you type myArray[0] JS will return the object {"S-No": 2, "Name" : "efgh", "Age": 12 }.

And you can access the object's properties easily, e.g. myArray[0].name will return abcd.

Also you can iterate through the object's properties. Have a look at my code example.

let myArray = new Array();

myArray[0] = {
    "S-No": 1, "Name" : "abcd", "Age": 12
};
myArray[1] = {
    "S-No": 2, "Name" : "efgh", "Age": 12
};
myArray[2] = {
    "S-No": 3, "Name" : "ijkl", "Age": 20
};

// Acesss objects
console.log('>> 1');
console.log(myArray[0])


// Access object properties
console.log('>> 2');
console.log(myArray[0].Name);

// Iterate through object properties
console.log('>> 3');
let myObject = myArray[0];
for(let key in myObject){
    console.log('Key = ' + key + ' | Value = ' + myObject[key]);
}

Upvotes: 1

Related Questions