1

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'].

4
  • Can you please show the filter you attempted? Commented Sep 15, 2021 at 15:29
  • yes here it is: Commented Sep 15, 2021 at 15:30
  • console.log(state.carData.map(({badge}) => (badge.filter(i => i==="outreach"))).find(badge=>badge.length>0)); Commented Sep 15, 2021 at 15:30
  • it gives ['outreach'] Commented Sep 15, 2021 at 15:31

3 Answers 3

1

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")))

Sign up to request clarification or add additional context in comments.

Comments

0
carData.filter(x=>x.badge.includes("outreach"))

1 Comment

thanks, it worked for me .... i am new to stack over flow so i can not tickmark it. btw thanks
0

Try this

carData.filter((car) => {
  car.badge.includes("outreach")
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.