473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array of objects, lookup

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
6 1793
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
if (is_array($_POST)) { foreach ($this->getAssocSectionsObjArray($key, $dbAP) as $obj) { print_r($obj); print_r(" in array? "); print_r(in_array($obj, $result)); print_r("<P>"); if...
2
by: Brian | last post by:
I have an application that uses an array of values as a "lookup" table and smarty as a template engine. $lookup = "label"; $lookup = "label2"; $lookup = "label"; $lookup = "label2"; In my...
3
by: John MacIntyre | last post by:
Hi, Can anybody give me a hint as to how to convert a javascript array into a vbscript array? BTW-it only needs to work in IE5 & 6 Thanks in advance, John MacIntyre VC++ / VB / ASP /...
3
by: Julius Mong | last post by:
Hi all, I'm doing this: // Test char code wchar_t lookup = {0x8364, 0x5543, 0x3432, 0xabcd, 0xef01}; for (int x=0; x<5; x++) { wchar_t * string = (wchar_t*) malloc(sizeof(wchar_t)); string =...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
9
by: GiantCranesInDublin | last post by:
Hi, I am looking for the best performing solution for modifying and iterating an object graph in JavaScript. I have outlined below a simplified example of the object model and examples of how I...
4
by: Kev | last post by:
Hello, I have an Access 2003 database running on an XP network. I have a datasheet subform containing a 28 day roster - shift1 to shift28. Each record has 1 RosterEmpID, 1 EmployeeNumber, 28...
7
by: d d | last post by:
I have an array of objects that start out looking like this: var ra=; I want to be able to access the array by index number from some code, and by the name property from other code, as if it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.