如何创建一个函数来查找对象数组的数据,而不给出与对象值匹配的精确搜索值。
例如我的数组是:
const array =
[{
"name": "Max Messi",
"age": 21,
"gender": "male"
},
{
"name": "tina baidya",
"age": 10,
"gender": "female"
},
{
"name": "tina shrestha",
"age": 100,
"gender": "female"
}
]
现在我想要一个函数来返回所有包含“tina”的数据name。
我尝试使用array.filter()方法,但它需要精确的搜索名称。就像我需要打字tina shrestha而不是仅仅tina
这是我尝试过的:
const array =
[{
"name": "Max Messi",
"age": 21,
"gender": "male"
},
{
"name": "Lina baidya",
"age": 10,
"gender": "female"
},
{
"name": "tina shrestha",
"age": 100,
"gender": "female"
}
]
function findData(data, id){
const found = data.filter(element => element.name === id)
return found
}
console.log(findData(array, "tina"))//logs empty array as i need to type full search value
那么我怎样才能创建搜索 json 数据的函数而不输入确切的值。
慕容森
红糖糍粑
相关分类