i have a array like this
{
"id": 1,
"name": "this is book",
}
{
"id": 2,
"name": "this is a test book",
}
{
"id": 3,
"name": "this is a desk",
}
when filter the array i want to return the array that in example contain book
i have try
let test = this.pro.filter((s: { name: any; })=>s.name===book);
not working in searching and try
let test = this.pro.filter((s: { name: any; })=>s.name===this is book);
working but just return id 1 it should return id 1 & 2
any solution plz thanx ;
CodePudding user response:
The below code will work as you expected. This checks the word 'Book' is present in the Array of object and return the particular object.
const pro = [
{
"id": 1,
"name": "this is book",
},
{
"id": 2,
"name": "this is a test book",
},
{
"id": 3,
"name": "this is a desk",
}]
let newArr = pro.filter(item=>{
if(item.name.indexOf('book') > -1){
return item;
}
})
console.log(newArr);
CodePudding user response:
Try this let test = b.filter((s)=>s.name.includes('book'));