简体   繁体   中英

Get index of parent object in nested object array

I have this array:

[
    { 
        elements: [
            { id: '123', field: 'value' }
            { id: '456', field: 'value' }
        ]
    }
    { 
        elements: [
            { id: '789', field: 'value' }
        ]
    }
]

Now I need to get the index of the first level object searching by an id: searching for id = '456' should give me 0 , id = '789' should give me 1

You can do this with findIndex() and some()

 var arr = [{ elements: [{ id: '123', field: 'value' }, { id: '456', field: 'value' }] }, { elements: [{ id: '789', field: 'value' }] }] var i = arr.findIndex(function(o) { return o.elements.some(function(e) { return e.id == 456; }) }) console.log(i) 

Get the feeling that something could be fixed to rethink this. But who knows. 456 should give u id 1 and so should 789 aswell.

var mapped = whatEverObject[0].elements.map(function(obj){ //0 = first 
 return obj.id;
})

console.log(mapped.indexOf(456)) // will give you 1 since id:123 is id 0 in the first elements array.

You can make a lookup table. It will be much faster if you do several lookups.

Live Example

var table = makeNestedLookupTable(example);
// table[789] would be 1 if the array in the question were "example"

function makeNestedLookupTable(data) {
    return data.reduce(function(lookup, obj, topIndex) {
        return obj.elements.reduce(function(lookup, element) {
            lookup[element.id] = topIndex;
            return lookup;
        }, lookup);
    }, {});
}

You can use this function getIndex which loops through the array and matches the id of the element with the given id and returns the index.This solution will work in all browsers.

var arr = [
    { 
        elements: [
            { id: '123', field: 'value' }
            { id: '456', field: 'value' }
        ]
    }
    { 
        elements: [
            { id: '789', field: 'value' }
        ]
    }
];

function getIndex(arr, id) {
    var i, ii, len, elemlen;
    for (i = 0, len = arr.length; i < len; i++) {
        elements = arr[i].elements;
        for (ii = 0, elemlen = elements.length; ii < elemlen; ii++) {
            if (elements[ii].id === id) {
                return i;
            }
        }
    }
}

var index = getIndex(arr, '456');

Here is a generic code where you can send an array and the lookup attribute:

function giveLevel(a,attr){
for(var i=0;i<a.length;i++){
  var o = a[i];
  var tmp = JSON.stringify(o);
  if(tmp.indexOf(attr)!==-1){
    return i;
  }
}
}

var a = [{elements: [
            { id: '123', field: 'value' },
            { id: '456', field: 'value' }
        ]
    },
    { 
        elements: [
            { id: '789', field: 'value' }
        ]
    }
];

giveLevel(a,'"id":"789"'); // returns 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM