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.
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>