So I am dealing with an object that looks a bit like this:
{
examples: [
{ key: 'value1' },
{ key: 'value1' },
{ key: 'value1' },
{ key: 'value2' },
{ key: 'value2' },
{ key: 'value2' }
]
}
As you can see the values are ordered. I'm trying to get where the value is value2 and I'm sure there is a more efficient way than what I am doing currently, as the actual object is significantly larger and so it takes some time to reach value2. This is the function I created:
function getValue2 (obj, num) {
if (obj.examples[num] = "value2"){
console.log(obj.examples[num]);
}
else {
getValue2(obj, num + 1);
};
};
var x = JSON.parse(obj);
getValue2(x, 0);
Thank you in advance! :)