I am trying to map key dynamically while constructing the object it is not creating ID as key.
main.js
const data = [
{
"ID":"Bellaire",
"Name":"Bellaire",
"Address": "1st Ave"
},
{
"ID":"Bellaire",
"Name":"Bellaire",
"Address": "1st Ave"
},
{
"ID":"Champions Forest",
"Name":"Champions Forest",
"Address": "2nd Ave"
}
]
function test(data,id){
const filterData = data.filter(e => {
if(e.ID === id) {
return true;
}
});
const finalResponse = {
filterData[0].ID: ["other Details"]
}
return finalResponse;
}
console.log(test(data,"Bellaire"));
expected output
[ { "Bellaire": ["other Details"] } ]