1

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'
            }];

4 Answers 4

3
(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 || {}.

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

2 Comments

Your answer is great but I just realized I made a mistake. Basically I have a selection using Select2 library and it returns the number 0-13 based on the selected item. Because my ID matches the array numbers, I mistaken it. So in reality I need to take a returned number and simply match it to the array item and then get the ID for that array/object item. Could you help with that as well please?
Then it is trivial: data[id].full_name.
1

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

2 Comments

I just realized this as my question was backwards and the array key number is actually what my selection was returning to me but since it matched my ID I didnt realize it. IN real production the ID will be different but the code I am using is returning the array key from selected item on a change event so this is actually what I need!
then amadan's answer will be the right for you. @torazaburo thanks for the edit. One of my first answers in good English now. :-)
1

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.

2 Comments

I'm just in reading progress for node js. Can I transform your code via babel too node directly?
You can use babel-node.
0
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
}

Comments

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.