Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda