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

91
Visualizações
Find the difference between 2 values inside array

I want to find the numeric difference between 2 values inside an object.

My objects are done like this:

{ today: 9, lastMonth: 6 }

I'm inside an every() method:

const validation = Object.values(myObj).every(item => console.log(item))

The every method is inside a reduce() method, because i have an array of object like the one i write up here.

Right now item log is printing value without any connection to the previous object, but one by one, so seems that i can't do a difference between the value like so:

function difference(a, b) {
  return Math.abs(a - b);
}

What i expect:

(item => difference(item))

to find the difference between the 2 values present in my object

What is happening:

Difference is returning NaN because item are not taken together

Edit: I fixed the main problem, now i have a simple array like so:

[[0,2],[4,9],[5,7]]...

seems that Math.abs(a-b) doesn't work with array, there is a valid solution to this?

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

0

If you use the spread syntax (...) as you call the difference function the result should be as expected.

I'd suggest creating a wrapper getDifferences() function to implement this:

function difference(a, b) {
  return Math.abs(a - b);
}
   
function getDifferences(arr) {
  return arr.map(el => difference(...Object.values(el)));
}

console.log(getDifferences([{ today: 9, lastMonth: 6 }]));
console.log(getDifferences([[0,2],[4,9],[5,7]]));
console.log(getDifferences([{ a: 1, b: 2 }, { x: 1, y: 3 }]));
.as-console-wrapper { max-height: 100% !important; }

You could also use function.apply, this will also allow you to pass an array of arguments to a function:

function difference(a, b) {
  return Math.abs(a - b);
}
   
function getDifferences(arr) {
  return arr.map(el => difference.apply(null, Object.values(el)));
}

console.log(getDifferences([{ today: 9, lastMonth: 6 }]));
console.log(getDifferences([[0,2],[4,9],[5,7]]));
console.log(getDifferences([{ a: 1, b: 2 }, { x: 1, y: 3 }]));
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Try

function difference(array) {
  var result = [];
  for(var i=0;i<array.length;i++){
   result.push(Math.abs(array[i][0]-array[i][1]));
}
return result
}
var arr = [[0,2],[4,9],[5,7]]
console.log(difference(arr))

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