I have the following data array that I wanna filter by company name/names.
data = [
{
"company":{
"name":"Company 1",
"symbol":"one"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 2",
"symbol":"two"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 3",
"symbol":"three"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 1",
"symbol":"four"
},
"description":"testrr",
"status":"Pending"
}
]
filterBy = ["Company 1", "Company 3"]
Expected Result
filteredData = [
{
"companyName":"Company 1",
"matchingData":[
{
"company":{
"name":"Company 1",
"symbol":"one"
},
"description":"test",
"status":"Pending"
},
{
"company":{
"name":"Company 1",
"symbol":"four"
},
"description":"testrr",
"status":"Pending"
}
]
},
{
"companyName":"Company 3",
"matchingData":[
{
"company":{
"name":"Company 3",
"symbol":"three"
},
"description":"test",
"status":"Pending"
}
]
}
]
Attempted
using the the following attempt I am getting all the matching objects instead of each match individually
const result = data.filter((obj) => filterBy.includes(obj.company.name));
let expectedResult = []
filterBy.forEach((e, i )=> {
let d = {companyName:e[i], matchingData: result}
expectedResult .push(d)
});
...................................................................................................................................................................
{"Company 1": [{}, {}], "Company 3": [{}]}the code necessary to create such an output would be a nobrainer.