-2

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?

Karla Jensen
  • 57
  • 1
  • 4

3 Answers3

1

This function will return the objects for which either firstName or lastName match the passed argument (search string) 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>
Alexander Presber
  • 6,429
  • 2
  • 37
  • 66
0

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:

function isPartOfArray(person, name) {
    return person.some(entry => entry.firstName === name || entry.lastName === name);
}
T. Short
  • 3,481
  • 14
  • 30
0
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);

Rajat Sharma
  • 121
  • 7