-2

So I have array with object inside. That object has a property call name that is a string of values

How can I return object that has "apples" in the name properties

fruits = [
{
name: 'apples, lemon',
quantity: 2
},
{
name: 'bananas, pearl',
quantity: 0
},
{
name: 'cherries,
pineapple',
quantity: 5
}
];
0

2 Answers 2

5

You can use Array.prototype.filter() with String.prototype.includes()

var fruits = [
{
name: 'apples, lemon',
quantity: 2
},
{
name: 'bananas, pearl',
quantity: 0
},
{
name: 'cherries, pineapple',
quantity: 5
}
];

const filteredFruits = fruits.filter(item => item.name.includes("apples"));
console.log(filteredFruits);

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

2 Comments

Thanks, this is what I was looking for !
If its helpful please mark it as answer. Thank you
0

let fruits=[{name:'apples, lemon',quantity:2},{name:'bananas, pearl',quantity:0},{name:'cherries, pineapple',quantity:5}];

let filteredFruits = fruits.filter(fruit => {
  return fruit.name.indexOf("apples") !== -1;
});

console.log(filteredFruits);

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.