-3

How do I filter the word "mango" in an array from an object that contains, please? Example "mango"

var myIteams= [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

]
2
  • what is your expected output? Commented Dec 10, 2018 at 10:39
  • Have you tried anything? What was the problem? Commented Dec 10, 2018 at 10:39

5 Answers 5

0

Just use Array.prototype.filter to filter the array with callback. Then use Array.prototype.includes to match the right item.

var myItems = [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

];

const match = myItems.filter(item => {
  return item.tag.includes('mango');
});
console.log(match);

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

1 Comment

got it perfect. Thank you!!
0

You can use Array.filter for this. It takes a function which should return something either 'truthy' or 'falsey'. If it returns something truthy, it keeps the item, otherwise it is filtered out.

var myIteams= [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

]

const result = myIteams.filter(item => item.tag.indexOf('mango') > -1)

console.dir(result)

Comments

0

You can use filter to filter the array and some to find a tag list, which contains your desired value.

var myItems = [{
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple"]
  },

]


const filtered = myItems.filter(o => o.tag.some(t => t === 'mango'));
console.log(filtered);

Comments

0

You can use filter and includes to get elements which contains tag 'Mango' in it

Filter: Filter provides new array where we get elements from existing array with some condition.

includes: It is used to determines wheter array includes certain element or not. It returns true or false

var myIteams= [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },

];

var result = myIteams.filter(x => x.tag.includes("mango"));
console.log(result);

Comments

0

You can use the filter method with an includes method in it. Here's an example of creating a function that lets you pass in what item you want to find.

var myItems = [
  {   
    title: "Title One",
    section: 1,
    tag: ["orange", "apple", "banana", "mango"]
  },
  {   
    title: "Title Two",
    section: 15,
    tag: ["mango"]
  },
  {   
    title: "Title Three",
    section: 21,
    tag: ["orange", "apple" ]
  },
];

function findItem(itemToFind) {
  return myItems.filter(item => item.tag.includes(itemToFind));
}

console.log(findItem('mango'))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.