I have a test array
and a test object
. I want to compare each element of test array
with test object
, but only those elements which are after the test object
in the test array
.
I have done it via the following example. Do we have any elegant way to do the same.
let testArray = [{ id: xx, name: 'C' },
{ id: xy, name: 'B' },
{ id: yz, name: 'C' },
{ id: ab, name: 'D' },
{ id: bc, name: 'E' },
{ id: cd, name: 'C' },
{ id: ce, name: 'E' },
{ id: ef, name: 'C' }];
let testObj = { id: 3, name: 'C' };
for (let i = testArray.indexOf(testObj) + 1; i < testArray.length; i++) {
if (testObj.name === testArray[i].name) {
console.log(testArray[i].id); // expected - object with id : cd and ef
// if I change test object to { id: cd, name: 'C' } then answer should be object with id : ef
}
}