user12838586
user12838586

Reputation:

How can I access ID and pID in following array?

Here is the array:

Array(2) [Array(5), Array(5)]
length:2
__proto__:Array(0) [, …]
0:Array(5) [Object, Object, Object, …]
1:Array(5) [Object, Object, Object, …]

first array [Array(5)]

0:Array(5) [Object, Object, Object, …]
length:5
__proto__:Array(0) [, …]
0:Object {pID: "1", pName: "janaka ravindra"}
1:Object {pID: "2", pName: "Darshana disanayaka"}
2:Object {pID: "3", pName: "Azad"}
3:Object {pID: "4", pName: "Hiran perera"}
4:Object {pID: "5", pName: "Shanela liyanage"}
1:Array(5) [Object, Object, Object, …]

Second Array [Array(5)]

1:Array(5) [Object, Object, Object, …]
length:5
__proto__:Array(0) [, …]
0:Object {ID: "1", Name: "janaka", Grade: "A"}
1:Object {ID: "2", Name: "Darshana", Grade: "B"}
2:Object {ID: "3", Name: "Azad", Grade: "C"}
3:Object {ID: "4", Name: "Hiran", Grade: "D"}
4:Object {ID: "5", Name: "Shanela", Grade: "E"}

I want to access ID and pID in above arrays? But how?

Upvotes: 1

Views: 125

Answers (3)

You can also use "filter",

this.firstArray.filter((data1) => {
console.log(data1.pID);
});
​
this.secondArray.filter((data2) => {
console.log(data2.ID);
});

Upvotes: 0

Siva Kondapi Venkata
Siva Kondapi Venkata

Reputation: 11001

1) Use destructure and get first and second arr
2) Use forEach (any other loop) and access the each element.

const [firstArr, secondArr] = [
  [{ pID: "1", pName: "janaka ravindra" }],
  [{ ID: "1", Name: "janaka", Grade: "A" }]
];

firstArr.forEach(obj => console.log(obj.pID));

secondArr.forEach(obj => console.log(obj.ID));

Upvotes: 1

Sundas Riasat
Sundas Riasat

Reputation: 350

Lets say your main array variable name is arr, then for first array the following code will work:

for(let i = 0; i< arr[0].length ; i++){
   console.log(arr[0][i].pID);
}

while for the second array:

for(let i = 0; i< arr[1].length ; i++){
   console.log(arr[1][i].ID);
}

Upvotes: 1

Related Questions