Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

120
Visualizações
How to determine if Javascript array contains an object with an attribute that is equal to each other

I have an array like

selectedData = [{
   Name = 'Michelle', 
   LRN = '100011'
 },
 {
   Name = 'Micheal', 
   LRN = '100011'
 },
 {
   Name = 'Mick', 
   LRN = '100012'
 } // so on....
]

I am sending this array through a function and that simply checks if the LRN attribute of each object is same if not it throws error. I have written the function as -

createSet(selectedData) {
 this.selectedData.forEach (element => {
 //the condition
 }
 else
 {
  this.openSnackBar ("LRN mismatching");
 }
}

How do I check if the LRN attribute of each object is same? I don't want loop unless I have to. I'm working with more than thousand records. I appreciate all the help. :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could take Array#every and check the first item to each other. The method stops the iteration if the condition is false and returns then false, if not then true.

The callback's parameter can have three parts, one for the item, the next for the index and the third one has the reference to the array.

In this case only the item is used with a destructuring of LRN and the array. The unneeded index is denoted by an underscore, which is a valid variable name in Javascript.

if (!array.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
    this.openSnackBar ("LRN mismatching");
}

Code with example of all property LRN having the same value.

const
    selectedData = [{ Name: 'Michelle', LRN: '100011' }, { Name: 'Micheal', LRN: '100011'}, { Name: 'Mick', LRN: '100011' }];

if (!selectedData.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
    console.log("LRN mismatching");
} else {
    // just to show
    console.log('OK');
}

Mismatching

const
    selectedData = [{ Name: 'Michelle', LRN: '100011' }, { Name: 'Micheal', LRN: '100011'}, { Name: 'Mick', LRN: '100012' }];

if (!selectedData.every(({ LRN }, _, a) => LRN === a[0].LRN)) {
    console.log("LRN mismatching");
} else {
    // just to show
    console.log('OK');
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Sorry but there's no other way, you will need to loop through them to compare the value, once it's an array of objects, it would be different if it was a LRN array.

On this case I would use it this way:

const sameLrnArrPosition = [];

checkLrn(arr, valueToBeCompared) {
 arr.forEach((element, index)=>{
  if(element.lnr === valueToBeCompared){
   sameLrnArrPosition.push(*here you can get the element or the index*);
  }
 })
}

This way you can have the objects that have the same value or the index, to acces them on the array and change a value or something.

about 4 years ago · Juan Pablo Isaza Relatório

0

An easy beginner-friendly solution would be:

function allLRNMatch(data) {
  if (data.length < 2) {
    return true;
  }
  const firstLRN = data[0].LRN;
  for (const element of data) {
    if (element.LRN !== firstLRN) {
      return false;
    }
  }
  return true;
}

You iterate through the array and check each element's LRN attribute with the first one. As soon you find a non-matching one, you can immediately return.

For a more functional approach, you could also use Array.prototype.some instead of the for loop to achieve this.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda