0

I've this array of objects:

    var buArray = [{'31': {'1':'VN', '2':'AC'}},
                   {'33': {'1':'VN', '2':'AC'}},
                   {'51': {'1':'VN', '2':'AC', '3':'SR', '5':'WIN'}},
                   {'52': {'1':'VN', '2':'AC', '3':'SR', '4':'JU'}},
                   {'53': {'1':'VN', '2':'AC', '3':'SR', '5':'WIN'}},
                   {'54': {'1':'VN', '2':'AC', '3':'SR', '5':'WIN'}},
                   {'55': {'1':'VN', '2':'AC', '3':'SR', '6':'PP'}}]

How can I access for example to this specific object(with id 31) for example: "{'31': {'1':'VN', '2':'AC'}}" ?

Best Regards,

4 Answers 4

1

You can use .filter to find matching elements in the array:

function findEntry(a, key) {
    return a.filter(function(e) {
        var k = Object.keys(e);
        return k.length === 1 && k[0] === key;
    });
}

The result will still be an array, but it'll only contain elements matching the predicate.

If it's possible that the inner object might contain multiple keys then replace the return line with:

return k.indexOf('31') >= 0;

NB: Object.keys, .filter and .indexOf are ES5 functions. Shims are readily available for older browsers.

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

Comments

0

If you are using underscore.js, you could use the _.where(list, properties) function which it has rather than re-inventing the mechanism. You can read about it here: http://underscorejs.org/#where

Comments

0

In standard javascript you have to loop over each item and check the keys.

function find_object_with_key(arr, key) {
    for(var i = 0;i < arr.length;i++) {   // For each index in the array
        var item = arr[i];                // Retrieve the item
        for(k in item) {                  // For each key in the object
            if(!item.hasOwnProperty(k)) { // Check if the item is a proper member of the object
                continue;
            }
            if(k == key) {                // Check if the key matches what we are searching for
                return item;              // Return the item
            }
        }
    }
    return false;                         // In case of failure return false
}

1 Comment

do NOT use for ... in on arrays!
0

That's a very confusing data structure. At the moment none of your objects has an id property, so you can't do a lookup on that - the way it is written you have objects with properties of 31, 33, 51 etc. You'd have to write a custom function to search for any object.

If you can I'd suggest reworking each object in the array to be something like

{
    id: 31,
    data: {
        '1':'VN',
        '2':'AC'
    }
}

But if not, with the data as written, a function like the following (uses simple iteration, there are more efficient ways)

function findObj(objID, array){
    var i, j, len1, len2;
    for(i = 0, len1 = array.length; i < len1; i++){
        var keys = Object.keys(array[i]);
        for(j = 0, len2 = keys.length; j < len2; j++){
            if(keys[j] === objID) {
                return array[i];
            }
        }
    }
    // object hasn't been found
    return null;
}

and call it thus

findObj("31", buArray);

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.