简体   繁体   English

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

[英]Creating a function that can search for an element in an array of objects?

I have this array of objects:我有这个对象数组:

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"}
];

And I have to create a function where you write down a name, and the function can tell you if it's a part of the array of objects or not.我必须创建一个函数,您可以在其中写下一个名称,该函数可以告诉您它是否是对象数组的一部分。 Can somebody help me?有人可以帮助我吗?

This function will return the objects for which either firstName or lastName match the passed argument (search string) input :此函数将返回firstNamelastName匹配传递的参数(搜索字符串) 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;
};

An empty array means: No persons first or last name matches the input string.空数组表示:没有人名或姓氏与输入字符串匹配。

The function is used in this filtering solution that you can run:该函数用于此过滤解决方案,您可以运行:

 // your input array const 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"} ]; // the function you are looking for const filterPersons = function(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; }; // this shows the filtering in action window.onload = function(){ const input = document.getElementById('val'); const output = document.getElementById('out'); input.addEventListener("keyup", function (ev) { const val = ev.target.value; // here we are calling the filter function const results = filterPersons(val); if (results.length > 0) { output.innerHTML = 'Yes! ' + JSON.stringify(results); } else { output.innerHTML ='No!'; } }); }
 <input id="val" /> <h3>included?</h3> <div id="out"> </div>

One approach:一种方法:

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

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

    return found;
}

And if you only want it to check the firstName and lastName values:如果您只想检查firstNamelastName值:

function isPartOfArray(person, name) {
    return person.some(entry => entry.firstName === name || entry.lastName === name);
}
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM