To find array index of complex
<aura:component >
<aura:handler name="render" value="{!this}" action="{!c.myAction}"/>
<aura:attribute name="objs" type="Object[]" />
</aura:component>
({
myAction : function(component, event, helper) {
var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];
console.log('studentsArray'+JSON.stringify(studentsArray[0]));
console.log(studentsArray[0].rollnumber);
console.log(studentsArray[0].name);
console.log(studentsArray[0].subject);
for(var i=0;i<studentsArray.length;i++){
if(studentsArray[i].name === 'amit')
{
console.log(i);
}
}
},
})
o/p
studentsArray{"rollnumber":1,"name":"dj","subject":"physics"}
findArrayElement.js:32 1
findArrayElement.js:33 dj
findArrayElement.js:34 physics
findArrayElement.js:39 2
<aura:handler name="render" value="{!this}" action="{!c.myAction}"/>
<aura:attribute name="objs" type="Object[]" />
</aura:component>
({
myAction : function(component, event, helper) {
var studentsArray =
[
{
"rollnumber": 1,
"name": "dj",
"subject": "physics"
},
{
"rollnumber": 2,
"name": "tanmay",
"subject": "biology"
},
{
"rollnumber": 3,
"name": "amit",
"subject": "chemistry"
},
];
console.log('studentsArray'+JSON.stringify(studentsArray[0]));
console.log(studentsArray[0].rollnumber);
console.log(studentsArray[0].name);
console.log(studentsArray[0].subject);
for(var i=0;i<studentsArray.length;i++){
if(studentsArray[i].name === 'amit')
{
console.log(i);
}
}
},
})
o/p
studentsArray{"rollnumber":1,"name":"dj","subject":"physics"}
findArrayElement.js:32 1
findArrayElement.js:33 dj
findArrayElement.js:34 physics
findArrayElement.js:39 2
Comments
Post a Comment