2

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

6
  • any code of your searching? Commented Sep 27, 2016 at 9:26
  • I don't know how to start with that Commented Sep 27, 2016 at 9:27
  • Basically, you want to get the id of the first item in your elements arrays ? Commented Sep 27, 2016 at 9:29
  • there's no built-in way to do this. You need to loop on the array Commented Sep 27, 2016 at 9:29
  • @YassineBadache No, the ID is the value I know. Depending on that id I need to get the index number of its parent object Commented Sep 27, 2016 at 9:31

5 Answers 5

5

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)

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

1 Comment

findIndex might not exist in all browsers.
0

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.

1 Comment

I'm thinking of the first level object. So 456 is in the first object, which should give me a 0
0

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);
    }, {});
}

Comments

0

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');

Comments

0

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

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.