I have an object to be filtered and it should return a key that has a specific id. Id is Unique. Need an efficient logic to return this expected output.The Object to be filtered.
{
"a":[{"id":"1123","value":"test1"}],
"b":[{"id":"1124","value":"test2"}],
"c":[{"id":"1125","value":"test3"}]
}
Input Id : "1124"
Expected Output : 'b'
let data = { "a":[{"id":"1123","value":"test1"}], "b":[{"id":"1124","value":"test2"}], "c":[{"id":"1125","value":"test3"}] }; let input = "1124"; let result = Object.entries(data).filter(([k,v]) => v[0].id === input)[0][0]; console.log(result);
Efficiencies here:
o = { "a":[{"id":"1123","value":"test1"}], "b":[{"id":"1124","value":"test2"}], "c":[{"id":"1125","value":"test3"}] } for (key in o) { if (o[key].some(x => x.id === '1124')) { console.log(key); break; } }
const input = "1124" const obj = { "a":[{"id":"1123","value":"test1"}], "b":[{"id":"1124","value":"test2"}], "c":[{"id":"1125","value":"test3"}] } Object.values(obj).filter((ob, i)=>{if(ob[0].id === input){console.log(Object.keys(obj)[i])}})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.