Javascript 创建一个可以在对象数组中搜索元素的函数?

Javascript 创建一个可以在对象数组中搜索元素的函数?,javascript,arrays,function,object,arrayobject,Javascript,Arrays,Function,Object,Arrayobject,我有一组对象: var person = [ {firstName: "Josh", lastName: "Doe", age: 50, eyeColor: "blue"}, {firstName: "Jake", lastName: "Denver", age: 34, eyeColor: "blue"}, {firstName: "Sam", lastName: "Kiszka", age: 20, eyeColor: "brown"} ]; 我必须创建一个函数,在这里你写下一个名字,这个

我有一组对象:

var person = [
{firstName: "Josh", lastName: "Doe", age: 50, eyeColor: "blue"},
{firstName: "Jake", lastName: "Denver", age: 34, eyeColor: "blue"},
{firstName: "Sam", lastName: "Kiszka", age: 20, eyeColor: "brown"}
];
我必须创建一个函数,在这里你写下一个名字,这个函数可以告诉你它是否是对象数组的一部分。有人能帮我吗

一种方法:

function isPartOfArrayOfObjects(person, name) {
    let found = false;

    person.forEach(entry => {
        Object.keys(entry).forEach(key => {
            if (entry[key] === name) {
                found = true;
            }
        });
    });

    return found;
}
如果您只想让它检查
firstName
lastName
值:

函数isPartOfArray(人员、姓名){
返回person.some(entry=>entry.firstName==name | | entry.lastName==name);
}

此函数将返回
firstName
lastName
与传递的参数匹配的对象(搜索字符串)
input

function filterPersons(input) {
  const results = person.filter(function(p){
    if (input.length == 0) return false;
    return (p.firstName+' '+p.lastName).match(new RegExp(input, 'i'));
  });
  return results;
};
空数组表示:没有人的名字或姓氏与输入字符串匹配

此函数用于此筛选解决方案,您可以运行:

//您的输入数组
const person=[
{名字:“乔希”,姓氏:“多伊”,年龄:50岁,眼睛颜色:“蓝色”},
{名字:“杰克”,姓氏:“丹佛”,年龄:34岁,眼睛颜色:“蓝色”},
{姓:“萨姆”,姓:“基什卡”,年龄:20岁,眼睛颜色:“棕色”}
];
//您正在寻找的函数
常量filterPersons=函数(输入){
const results=person.filter(函数(p){
if(input.length==0)返回false;
返回(p.firstName+“”+p.lastName).match(新的RegExp(输入'i'));
});
返回结果;
};
//这显示了正在进行的过滤
window.onload=函数(){
常量输入=document.getElementById('val');
const output=document.getElementById('out');
input.addEventListener(“键控”,功能(ev){
const val=ev.target.value;
//这里我们调用filter函数
const results=filterPersons(val);
如果(results.length>0){
output.innerHTML='Yes!'+JSON.stringify(结果);
}否则{
output.innerHTML='No!';
}
});
}

包括?

key是什么意思?key指的是
firstName
lastName
等。。。你可以给它取任何你喜欢的名字。我还添加了另一个选项,您可能更喜欢@KarlaJensenThank you!条目代表什么?条目是每个“数组条目”。所以一个条目应该是
{firstName:“Josh”,lastName:“Doe”,年龄:50岁,eyeColor:“blue”}
。同样,您可以随意命名它,以便在
firstName
lastName
字段中搜索。如果表述得更清楚,许多问题会更容易解决。“Josh”和某人匹配吗“Doe”匹配吗“Josh Doe”匹配吗?“乔斯”或“乔希”怎么样?或者。所以我可以搜索一个姓,一个名,或者两者都搜索,结果会是真的。对不起,我不知道如何使用jQuery@KarlaJensen如果这个答案解决了您的问题,请随意将其标记为正确答案。
function returnWetherNameMatch(personArr,name){
    let matched =  personArr.filter(person => {
    return person.firstName.toLowerCase() === name.toLowerCase() || person.lastName.toLowerCase() === name.toLowerCase()
  });
  if(matched.length>0) {
  return true;
  } else
    return false;
}

var person = [
{firstName: "Josh", lastName: "Doe", age: 50, eyeColor: "blue"},
{firstName: "Jake", lastName: "Denver", age: 34, eyeColor: "blue"},
{firstName: "Sam", lastName: "Kiszka", age: 20, eyeColor: "brown"}
];

var isNamePresent = returnWetherNameMatch(person,"Josh")

console.log(isNamePresent);