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

105
Visualizações
Function with array and object

I have an array and an object and i would like a function samePrice(arr,obj) like this:

const arr = [0,1,2];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

// samePrice(arr,obj) => true

const arr = [0,3];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

// samePrice(arr,obj) => false

Here is my current function...

const samePrice = (arr,obj) => {
    let result = true;
  let it = obj[arr[0]];
  arr.forEach(item => {
    if (obj[item] !== it){
      result = false;
    }
  })  
  return result;
}

I am sure there is a better solution.

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

0

Basically, what you're doing is checking that every element is the same as the first:

const samePrice = (arr,obj) => arr.every(x => obj[x] == obj[arr[0]])

const arr = [0,1,2];
const obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr,obj)); // => true

const arr2 = [0,3];
const obj2 = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr2,obj2)); // => false

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.every to test if something is true for all array elements. So get the object value for the first array element, then test if every element is equal to that.

function samePrice(items, prices) {
  if (items.length == 0) {
    return true;
  }
  let price = prices[items[0]];
  return items.every(item => prices[item] == price);
}

const arr1 = [0, 1, 2];
const obj1 = {
  0: 10,
  1: 10,
  2: 10,
  3: 12,
};

console.log(samePrice(arr1, obj1));

const arr2 = [0, 3];
const obj2 = {
  0: 10,
  1: 10,
  2: 10,
  3: 12,
};

console.log(samePrice(arr2, obj2));

about 4 years ago · Juan Pablo Isaza Relatório

0

I came up with this:

function samePrice(arr, obj) {
  if(arr.length < 1)
    return false;
  i = 1;
  while(i < arr.length) {
    if(obj[arr[i]]!=obj[arr[i-1]])
      return false;
    i++;
  };
  return true;
}

var arr = [0,1,2];
var obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr, obj));

var arr = [0,3];
var obj = {
    0: 10,
    1: 10,
    2: 10,
    3: 12,
};

console.log(samePrice(arr, obj));

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