When given an object structure like the one below:
temp = [
{category: 'category1', branch: [{key: 'key A1', value: 'value A1'}, {key: 'key B1', value: 'value B1'}] },
{category: 'category2', branch: [{key: 'key A2', value: 'value A2'}, {key: 'key B2', value: 'value B2'}] },
{category: 'category3', branch: [{key: 'key A3', value: 'value A3'}, {key: 'key B3', value: 'value B3'}] },
{category: 'category4', branch: [{key: 'key A4', value: 'value A4'}, {key: 'key B', value: 'value B4'}] }
]
The goal is to locate an object similar to
{key: 'key A3', value: 'value A3'}
when a specific value such as certainValue = 'value A3'
matches within the given object.
An attempted solution has been provided in the form of code:
result = temp.map(te => te.branch.find(t => t.value === certainValue))
Unfortunately, this results in obtaining undefined values in the outcome, like so:
[undefined, undefined, {key: 'key A3', value: 'value A3'}, undefined]
Therefore, the question remains - is there a way to directly receive the exact desired result, being
{key: 'key A3', value: 'value A3'}
, without needing to iterate over the result again to remove the undefined
entries?