1

This might seems silly, but I'm trying it from long time.

var parentArray = [
  [0.49988811188811194, 0.17249417249417248],
  [0.8181398601398602, null],
  [0.8181398601398602, 0.47785547785547783],
  [0.49988811188811194, 0.47785547785547783]
];

function containsNull(arr) {
  return (function() {
    return arr.filter(e => {
      if(e.some(el => el == null)) return true
    })
  })();
}

console.log("-->", containsNull(parentArray))

here I'm getting output as

> "-->" Array [Array [0.8181398601398602, null]]

but I don't need the array as return value. I just want true or false that's all.

i.e, 

"-->" true

How to achieve this? even using load-ash library is good to go!

1
  • 1
    why don't you: return array.includes(null) === true) Commented Oct 28, 2021 at 9:23

3 Answers 3

2

You can use some with includes

var parentArray = [
  [0.49988811188811194, 0.17249417249417248],
  [0.8181398601398602, null],
  [0.8181398601398602, 0.47785547785547783],
  [0.49988811188811194, 0.47785547785547783],
];

const result = parentArray.some((a) => a.includes(null));
console.log(result);

Sign up to request clarification or add additional context in comments.

4 Comments

This is good logically, Some will return boolean by iterating, therefore one must use a method which return boolean inside it. i.e includes which returns boolean.
yup, @VikasAcharya that is correct
But there is one problem, When there no null value it crashes. it is not returning false.
It does return false...
2

It seems that you are using a multidimensional array, so you could just flatten it with Array.prototype.flat and use Array.prototype.includes to check if the flattened array contains null.

let parentArray = [
  [0.49988811188811194, 0.17249417249417248],
  [0.8181398601398602, null],
  [0.8181398601398602, 0.47785547785547783],
  [0.49988811188811194, 0.47785547785547783]
];

const containsNull = (arr) => arr.flat().includes(null)

console.log(containsNull(parentArray))

The following would flatten arrays of n-dimensions and check if it contains null:

function flatten(arr) {
  return arr.reduce(function (flat, toFlatten) {
    return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
  }, []);
}

let parentArray = [
  [[[[0.49988811188811194, 0.17249417249417248]]],
  [0.8181398601398602, null],
  [0.8181398601398602, 0.47785547785547783]],
  [0.49988811188811194, 0.47785547785547783]
];

const containsNull = (arr) => flatten(arr).includes(null)

console.log(containsNull(parentArray))

Comments

0

Use Array.some() method on both parent and child array.

var parentArray = [
  [0.49988811188811194, 0.17249417249417248],
  [0.8181398601398602, null],
  [0.8181398601398602, 0.47785547785547783],
  [0.49988811188811194, 0.47785547785547783]
];

function containsNull(arr) {
  return arr.some(list => list.some(ele => !ele))
}

console.log(containsNull(parentArray));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.