By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,576 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,576 IT Pros & Developers. It's quick & easy.

array of objects, lookup

P: n/a
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

Nov 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
VK
On Nov 10, 8:28 pm, "thibault.langlois" <thibault.langl...@gmail.com>
wrote:
Hello,
I am in my first day of javascript programming.
Welcome to the club! :-)
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 "";

}
In here you are iterating through the properties of an object to see
if the object has some given property with some given value. An
object with its properties - programmatically - is a set of key/value
pairs, other words an "associative array" or "hash". There is no
"position" in this structure as such. You can only add, remove and
check for existence particular key/value pairs. For the last task
there is no need to go through all pair. If you need to check is
SomeObject has property SomeProperty and this property equals to
SomeValue then all you have to do is (w/o regular simplification to be
more visual):
if ((typeof SomeObject[SomeProperty] != 'undefined') &&
(SomeObject[SomeProperty] == SomeValue)) {
// positive match
}
Here you have to use SomeObject[SomeProperty] syntax because with
SomeObject.SomeProperty the system will look for "SomeProperty"
property - so using SomeProperty as a literal instead of the value
stored in this variable.

If you want to do what you wrote: so you have an Array of objects and
you want to see if any object has a given property with a given value
and you want to know then the position (index) of such object in your
array:

// Let's create an array with three objects in it:
var MyArray = [
{'foo':'not a bar'},
{'foo':'still not a bar'},
{'foo':'bar'},
];

// Search function:
function elementPosition(arrayReference, propertyName, soughtValue) {
// The match may happen on the first element in the array
// (index 0) so we are using negative value
// for "no match found" like in indexOf / lastIndexOf
// methods:
var position = -1;
for (var i=0; i<arrayReference.length; i++) {
if (arrayReference[i][propertyName] == soughtValue) {
position = i;
break;
}
}
return position;
}

alert(elementPosition(MyArray, 'foo', 'bar'));
// alerts 2 - the 3rd element in the array

Nov 10 '07 #2

P: n/a
thibault.langlois wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>And if you want the index, there really is no point in using for..in:

function elementPosition(element, array, attribute)
{
for (var i = array.length; i--;)
{
if (element === array[i][attribute])

in this case array is a bi-dimensional array and attribute must be an
integer ?
Not at all. From whoever/whatever you got the notion that the bracket
property accessor syntax required an Array object (probably a [bad] book),
forget about them/it regarding programming in ECMAScript implementations.

The value of `i' here is the name of a property of the Array object referred
to by `array'. It only happens that the properties of Array objects that
refer to elements of the encapsulated array data structure have an unsigned
32-bit Integer representation (see the ECMAScript Language Specification Ed.
3 Final, section 15.4.)

So `array[i]' refers to the value of an array element. If that value is a
reference to an object (or a primitive value that can be converted into an
object), that object has properties as well. Therefore, array[i][attribute]
refers to the value of the property of the object array[i] refers to that
has the name that is stored in `attribute'. And that name may have, but
does not have to have, a numeric representation.
Please trim your quotes as explained and recommended in the FAQ (Notes).
http://www.jibbering.com/faq/faq_notes/clj_posts.html
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 11 '07 #3

P: n/a
On Nov 11, 2:13 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
thibault.langlois wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
[...]
in this case array is a bi-dimensional array and attribute must be an
integer ?

Not at all. From whoever/whatever you got the notion that the bracket
property accessor syntax required an Array object (probably a [bad] book),
forget about them/it regarding programming in ECMAScript implementations.
Right. I have no book yet :-(
The value of `i' here is the name of a property of the Array object referred
to by `array'. It only happens that the properties of Array objects that
[...]
So `array[i]' refers to the value of an array element. If that value is a
reference to an object (or a primitive value that can be converted into an
object), that object has properties as well. Therefore, array[i][attribute]
refers to the value of the property of the object array[i] refers to that
has the name that is stored in `attribute'. And that name may have, but
does not have to have, a numeric representation.
I see. I have send another post before reading yours. You answered
here. thanks.
Please trim your quotes as explained and recommended in the FAQ
I'll do my best.

(Notes).http://www.jibbering.com/faq/faq_notes/clj_posts.html
>
PointedEars
--
Thibault

Nov 11 '07 #4

P: n/a
VK
On Nov 11, 2:58 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>
:-))
After the recent lost of the English soccer team to Russians, a new
British joke at the next morning was: "It is already 9am and Steve
Mclaren is still the coach. What a hey delay?".
By using this template: "VK is already two days as back to clj and
Thomas is still posting in here. What is he still doing here?" You had
your fun during my absence since this May, but now the fun is over,
thank you for your time.
:-))
:-|
I will not argue with you as have nothing interesting to say - to me
at least.
:-|
Nov 11 '07 #5

P: n/a
thibault.langlois wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>thibault.langlois wrote:
>>[...] Thomas 'PointedEars' Lahn [...] wrote:
[...]
>>in this case array is a bi-dimensional array and attribute must be an
integer ?
Not at all. From whoever/whatever you got the notion that the bracket
property accessor syntax required an Array object (probably a [bad] book),
forget about them/it regarding programming in ECMAScript implementations.

Right. I have no book yet :-(
Don't bother. I can recommend none, and there are several I can recommend
against. You are best off with this group, it's FAQ, FAQ Notes, and the
reference material pointed to therein.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 12 '07 #6

P: n/a
thibault.langlois wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
>thibault.langlois wrote:
>>[...] Thomas 'PointedEars' Lahn [...] wrote:
[...]
>>in this case array is a bi-dimensional array and attribute must be an
integer ?
Not at all. From whoever/whatever you got the notion that the bracket
property accessor syntax required an Array object (probably a [bad] book),
forget about them/it regarding programming in ECMAScript implementations.

Right. I have no book yet :-(
Don't bother. I can recommend none, and there are several I can recommend
against. You are best off with this group, its FAQ, FAQ Notes, and the
reference material pointed to therein.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 12 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.