0

I have the following array:

const person = [
      { name: "John", surname: "Doe" },
      { name: "Jane", surname: "Williams" }];

I want to check if a match is equal to just one name from the array. I did the following:

match === person[0].name || match === person[1].name || match === person[3].name ? "Do something" : "Blank"

I'm looking for a better way to iterate through this array, because I don't know array length.

  • You can try and use the find() method for that https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – Craques Nov 01 '21 at 14:37
  • Your question is unclear, do you want to return the first item that matches or only return if it is the only one that matches in the array? `find()` will do the former but not guarantee the latter. – pilchard Nov 01 '21 at 14:45
  • Think one point this question had which is missing in the linked 'already answered here' post ist the 'equal to exactly one'. Using .find() won't tell you this, it just gives back the first result. To check if there is just one use .filter() and check the length of the search result array – Corrl Nov 01 '21 at 15:16

4 Answers4

0
person.forEach(function(item){
if(match === item.name){
// your logic goes here
}
})

this iterates through every object("{}") in your array and executes a function (you could use an arrow function too). The function then checks if your "match" value is equal to the "name" key in every object(referenced by "item") in the array it had ran through.

There are other array methods you can check out.

0

You can use Array.prototype.find to find in an array of objects.

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

As a demonstration, I have written a search persons by name form:

const persons = [
  { name: "John", surname: "Doe" },
  { name: "Jane", surname: "Williams" }
]

function bindEvent() {
  const form = document.forms['find-by-name-form']
  form.addEventListener('submit', onSubmit, true)
}

function onSubmit(event) {
  event.preventDefault()
  const { name } = event.target
  const person = findPersonByName(name.value)
  alert(person ? `${person.name} ${person.surname}` : 'no result')
  person && console.log(person)
}

function findPersonByName(name) {
  return persons.find((item) => item.name === name)  
}

window.onload = bindEvent
<form name="find-by-name-form">
  <label for="name">Find by name:</label>
  <input name="name" />
  <button>Search person</button>
</form>
Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
0

Best solution to find index of array of match value const name = 'sandeep'

const match = persons.findindex

((person) => person.name === name)

0

You can use the find method, the find method takes in a callback function.

So you can do

const checkIfNameExists = (eachPerson) => eachPerson.name === match;

person.find(checkIfNameExists)

If the name exists, it returns the object. If not it returns undefined.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find