I have an array of objects as below, I want to get the whole object where badge has outreach word. I tried to filter and find but it only give me a single word in the array, not the full object. Please help me sort this out, thanks.
carData: [
{
flowName: "Cars",
badge: ["content", "outreach"],
image: icons.car,
},
{
flowName: "linkedin",
badge: ["content"],
image: icons.linkedin,
},
{
flowName: "facebook",
badge: ["content"],
image: icons.facebook,
},
]
This is what I've tried:
console.log(state.carData.map(({badge}) => (badge.filter(i => i==="outreach"))).find(badge=>badge.length>0));
It gives the result ['outreach']
.
CodePudding user response:
const carData = [{
flowName: "Cars",
badge: ["content", "outreach"],
image: "icons.car",
},
{
flowName: "linkedin",
badge: ["content"],
image: "icons.linkedin",
},
{
flowName: "facebook",
badge: ["content"],
image: "icons.facebook",
},
]
console.log(carData.filter(car => car.badge.includes("outreach")))
CodePudding user response:
carData.filter(x=>x.badge.includes("outreach"))
CodePudding user response:
Try this
carData.filter((car) => {
car.badge.includes("outreach")
})