0

I have created an array like this

contacts = [bob,ryan,468]

Assume that bob and ryan are objects having more than one properties. I know I can call an array by its position, say, contacts[1] or contacts[0].

Is it possible to call the bob object by its name? Is there any default method to do this in javascript or should I write my own solution?

PS: I am totally new to Javascript.

4
  • 1
    You can still access bob as long as it's in scope. Commented May 21, 2015 at 8:56
  • 1
    With array - no. Use object instead of array Commented May 21, 2015 at 8:57
  • @Anonymous Platypus What do you mean by name of an object? The variable it's stored in? Please clarify. Commented May 21, 2015 at 9:07
  • @tim-we I meant the variable that holds the object. Say bob in the question. I don't know whether I have a misconception about this as I am new to JS. :( Commented May 21, 2015 at 9:11

5 Answers 5

1

Well to be able to do this:

contacts = [bob,ryan,468]

bob and ryan have to be defined somewhere before that line. Something like this:

var bob = {
      name: 'Bob',
      age: 42
    };

var ryan = { name: 'Ryan' };

var contacts = [bob,ryan,468];

Instead of using contacts[0] and contacts[1] you could also just use bob and ryan to access them.

You are asking for 'call the bob object by its name'. Objects in JavaScript don't have a name. You can have a variable that contains the object (or at least a reference to it) like var bob = {};. Or what do you mean by it's name?

Hope this helps :)

Sign up to request clarification or add additional context in comments.

5 Comments

I wanted to call them from array. When I try console.log(contacts[bob]) it returns undefined. I mean by the variable name.
There are no associative arrays in JS, so all you can do is contacts[0], contacts[1], and so on. You can use plain objects though,have a look at this link: less-broken.com/blog/2010/12/…
I hope this comment solved my doubt :) I am accepting this answer. Thank you
Well thanks for accepting my answer. Were JavaScript Objects (key-value-pairs) the thing you've been looking for?
Not really. Associative arrays was the one I was looking for. Since it doesn't exist for JS my problem is solved :)
1

Example:

var as = { "bob": bob, "ryan":ryan, "468":468 };

//as["bob"] -> bob object

1 Comment

This is actually an object, not an associative array. There is no such thing in JS. However this might still be what @Anonymous Platypus was asking for. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
0

what you might want here is a hash...

var contacts = {
  first: 'bob',
  last: 'ryan',
  number: 468
};

alert(contacts['last']);

1 Comment

I think u called a property from an object. I am looking to call an object from an array with its variable name. I am not sure whether I went wrong
0

You can use filter function from array prototype to query your array.

var vals = [
    {
        "name":"bob",
        "age":50
    },
    {
        "name":"brayan",
        "age":51
    },
    567
];

var query = vals.filter(function(obj){
    if (typeof(obj) === 'object' && 'name' in obj &&     typeof(obj.name) === 'string') {
        if(obj.name === 'bob')
            return true;
    }
    return false;
});

alert(query[0].age);

You can modify your query operation with suitable condition.

Here is a working fiddle.

Comments

0

In javascript object we generally keep values in Key: Value pairs.

You can use like:

var contacts = {"obj_ryan":{"fname":"ryan","lname":"bob","phone":468},
                "obj_mark":{"fname":"mark","lname":"filip","phone":467}
               };

//use them like
alert(contacts.obj_ryan.fname);
alert(contacts.obj_mark.phone);

Hope it helps

2 Comments

This is just an object rite? Not an array. Correct me if I am wrong
Yes, This is an Object. In JS Array is a type of object only. They both work on concept of Key : Value pair. The difference is only that in objects we explicitly specify both Key and Value(like var x = {"fname":"mark"}), but in Arrays JS Automatically indexes key with numbers starting with 0( like var x= ["mark"] ). We would access them like x.fname || x["fname"] and x[0] resp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.