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

148
Visualizações
Compare items of an object array in JS

I am working on an array which is like this:

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

Now i want to compare the floor items here. If all the floor value is same, then it should return true and if there are multiple values like '1' & '2' in this array, it should return false.

Here's what i have tried so far

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

for (let i=0; i< arr.length; i++){
    for (let j=1; j< arr.length; j++){
       if( arr[i].floor !== arr[j].floor){
            console.log('found');
       }else{
            console.log('Not found');
       }
    }
}

I know this is nowhere close to the solution. But i'm stuck.

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

0

An initial naive approach.

  • convert the check into a function so it can be reused
  • extract the floor property of the first item using Destructuring assignment and rename it to firstFloor
  • Use Array.prototype.every() to check if all the items meet a certain criteria

const allSameFloors = (array = []) => {
  const [{
    floor: firstFloor
  }] = array;
  return array.every(({
    floor
  }) => floor === firstFloor);
};

console.log(
  allSameFloors(
    [{
        floor: '1',
        id: '10165',
        label: 'Elutuba/Köök'
      },
      {
        floor: '1',
        id: '10166',
        label: 'Tuba 1'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      },
      {
        floor: '2',
        id: '10167',
        label: 'Vannituba'
      }
    ]
  )
);

console.log(
  allSameFloors(
    [{
        floor: '1',
        id: '10165',
        label: 'Elutuba/Köök'
      },
      {
        floor: '1',
        id: '10166',
        label: 'Tuba 1'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      },
      {
        floor: '1',
        id: '10167',
        label: 'Vannituba'
      }
    ]
  )
);

about 4 years ago · Juan Pablo Isaza Relatório

0

There more than likely is a better way of doing this but here is a quick solution.

function FloorSameValue(arr){
  values = [];
  for (obj of arr){
    if (!values.includes(obj.floor)){
      values.push(obj.floor);
    }
  }
  return values.length === 1;
}

var arr = [
  {floor: '1', id: '10165', label: 'Elutuba/Köök'},
  {floor: '1', id: '10166', label: 'Tuba 1'},
  {floor: '1', id: '10167', label: 'Vannituba'},
  {floor: '2', id: '10167', label: 'Vannituba'}
];

console.log(FloorSameValue(arr))
//false

about 4 years ago · Juan Pablo Isaza Relatório

0

for( var i = 0 ; i < arr.length;i++)
{
    var myval = arr[i].floor;
    for (var j = i+1 ; j < arr.length ; j++)
    {
        if (arr[i].floor !== arr[j].floor)
        {          
            alert("false");
        }
    }
}

    enter code here
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