JasonDavis
JasonDavis

Reputation: 48933

Get value from a JavaScript array.object using id number

Based on the var data below. How can I get the full_name value if all I have is the number 2? Number 2 corresponds with id: 2 and the full_name would be Eric

var data = [{
                id: 0,
                full_name: 'None',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 1,
                full_name: 'John',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 2,
                full_name: 'Eric',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 3,
                full_name: 'Larry',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 4,
                full_name: 'Rick',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 5,
                full_name: 'John',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 6,
                full_name: 'Eric',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 7,
                full_name: 'Larry',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 8,
                full_name: 'Rick',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 9,
                full_name: 'John',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 10,
                full_name: 'Eric',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 11,
                full_name: 'Larry',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }, {
                id: 12,
                full_name: 'Rick',
                gravatar_url: 'http://gravatar.com/avatar/31b64e4876d603ce78e04102c67d6144?s=80'
            }];

Upvotes: 1

Views: 52

Answers (4)

user663031
user663031

Reputation:

ES6 offers Array#find, to find the first element in an array that meets some condition:

var wanted = data.find(elt => elt.id === 2);

If you don't have this available, there are various polyfills, including one on the page referenced above.

Upvotes: 1

baao
baao

Reputation: 73221

Amadan's answer most probably is better/more failure proof than mine, but if you know that your array of objects will always contain objects from Id 0 to Id infinite, you could also get it with

var wanted = data[number].fullName

Upvotes: 1

carterw485
carterw485

Reputation: 818

function myQuery(array, id){
    for(i in array){
        if(array[i].id === id){
            console.log(data[i].full_name);
            return data[i].full_name;
        }
    }
}

myQuery(data, 2)

If you are keeping your ID equal to the place in the array, you could just do this.

function myQuery(id){
    return array[id].full_name
}

Upvotes: 0

Amadan
Amadan

Reputation: 198324

(data.filter(function(x) { return x.id == 2 })[0] || {}).full_name;
// => "Eric"

data.filter will return only elements that fit the given criterion; in this case, id being 2. Since we only care about one element, we will just take the first found element; but if none exist, .full_name will give an error about full_name not being defined on undefined, so we put in an empty object just in case the search fails using || {}.

Upvotes: 3

Related Questions