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

169
Visualizações
how to get boolean value from an array which contains array and objects as elements

I need to get a boolean value from an array of array and object like below

points = [
  [1,2,3,4],
  {a:1, b:2}
]

now, I get some array or object from server,

let newVal = [1,2,3,4]
or sometimes 
let newVal = {a: 2, b: 5}  etc

And I need to check whether it's present inside points or not and push inside it if absent.

 if(!_.some(points, val => _.isEqual(val,newVal))){
    points.push(newVal)
 } else return;

expected result
if newVal is present inside points then I should get true else false

But the above _some always returns true if newVal is array.

Can anyone help me on this.

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

0

If you are relying on isEqual from lodash to determine equality, you can use this:

points.some(val => isEqual(val, newVal))

Example snippet below:

<script type="module">

import {isEqual} from 'https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js';

// returns `true` if value was added, `false` otherwise
function addNewValueIfNotEqual (arr, value) {
  if (arr.some(v => isEqual(v, value))) return false;
  arr.push(value);
  return true;
}

const points = [
  [1,2,3,4],
  {a:1, b:2},
];

const newValues = [
  [1, 2, 3, 4],
  {a: 2, b: 5},
];

for (const value of newValues) {
  const valueAdded = addNewValueIfNotEqual(points, value);
  console.log({value, valueAdded});
}

console.log(points);

</script>

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