Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!