i have this JSON:
var projects_array = new Array(
{name:"myName1", id:"myid1", index:1},
{name:"myName2", id:"myid2", index:2},
{name:"myName3", id:"myid3", index:3},
);
I need to get the "index" value of the object matching an specific "id" value. So if my "id" is "myid1" y would get "1".
here is part of my code:
var myid = $(this).attr('id'); //this is the id value
projects_array.map(function (proj) {
if (proj.id == myid) {
return proj // returns Undefined
}
});
Finally, I need to assign that value in a variable to use it later, THANKS :)
i have this JSON:
var projects_array = new Array(
{name:"myName1", id:"myid1", index:1},
{name:"myName2", id:"myid2", index:2},
{name:"myName3", id:"myid3", index:3},
);
I need to get the "index" value of the object matching an specific "id" value. So if my "id" is "myid1" y would get "1".
here is part of my code:
var myid = $(this).attr('id'); //this is the id value
projects_array.map(function (proj) {
if (proj.id == myid) {
return proj // returns Undefined
}
});
Finally, I need to assign that value in a variable to use it later, THANKS :)
Share Improve this question asked Apr 8, 2012 at 0:26 lilymzlilymz 3872 gold badges7 silver badges16 bronze badges 3-
2
Code tip: Never use
new Array()
. Instead, use array literals:[1, 2, 3]
– Ry- ♦ Commented Apr 8, 2012 at 0:31 - @minitech, I got this advise before from SO but why, can you give some idea ? – The Alpha Commented Apr 8, 2012 at 0:36
-
1
@SheikhHeera: It's shorter, it's a little more efficient, it's standard, and people can replace
Array
to mean whatever they want it to mean. Plus, there's the fact thatnew Array(5)
won't give you[5]
but rather[,,,,]
. – Ry- ♦ Commented Apr 8, 2012 at 0:39
1 Answer
Reset to default 3I think you're selecting the index successfully, but when you return the value, it goes into a new array ("maps" there if you will). Try something like this:
var myproj;
var myindex;
projects_array.map(function (proj) {
if (proj.id == myid) {
myproj = proj;
myindex = proj.index;
}
});