I am faced with a challenge involving an array that contains objects with both source
and target
properties. My goal is to identify a value that never appears as a target
.
My current approach seems convoluted. I separate the elements of the array into two new arrays, all
containing all elements and targets
containing just target elements. Then, I apply a filter to find the desired value.
const a = [
{ source: '2', target: '3' },
{ source: '1', target: '2' },
{ source: '3', target: '4' },
{ source: '4', target: '5' }
];
const all = ['1', '2', '3', '4', '5'];
const targets = ['3', '2', '4', '5'];
console.log(all.filter(e => !targets.includes(e))[0]);
I am seeking a more efficient solution that does not require creating these intermediate arrays, as I know the returned element will be unique. Thus, I prefer to receive a single value instead of an array as the answer.