T
thibault.langlois
Hello,
I am in my first day of javascript programming.
I would like to find the position of an object in an array such that
one of its attribute has a certain value. Is it possible to pass the
attribute name as a parameter ?
function elementPosition (element, array, attribute) {
var i = 0;
for (object in array) {
if (element == object.attribute)
return i;
i = i + 1;
}
return "";
}
If not, is it correct to pass a function name:
function elementPosition (element, array, test) {
var i = 0;
for (object in array) {
if (test (element, object))
return i;
i = i + 1;
}
return "";
}
function test1 (value, object) {
return (object.someSlot == value);
}
Are there better ways to do this in javascript ?
Thanks
Thibault Langlois
I am in my first day of javascript programming.
I would like to find the position of an object in an array such that
one of its attribute has a certain value. Is it possible to pass the
attribute name as a parameter ?
function elementPosition (element, array, attribute) {
var i = 0;
for (object in array) {
if (element == object.attribute)
return i;
i = i + 1;
}
return "";
}
If not, is it correct to pass a function name:
function elementPosition (element, array, test) {
var i = 0;
for (object in array) {
if (test (element, object))
return i;
i = i + 1;
}
return "";
}
function test1 (value, object) {
return (object.someSlot == value);
}
Are there better ways to do this in javascript ?
Thanks
Thibault Langlois