0

How can I check an array to test that all the data in the array are not equal to a variable's value:

var values = ["1", "2", "3"];

var value = "0";
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • You need to use a loop to compare values. When the first time you get "equal" - exit the loop. If you get all the way through - no matches. Or, if you are allowed, use [`includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) – PM 77-1 Apr 21 '20 at 19:29
  • Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – Abhishek Duppati Apr 21 '20 at 20:23
  • Welcome to SO! Please see "[ask]", "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648)" and "[MCVE](https://stackoverflow.com/help/minimal-reproducible-example)" and all their linked pages. Your question is poorly asked. We expect to see evidence of your effort to solve the problem. You gave us a requirement and two variable assignments but nothing showing what you did. Did you research this? If so, why didn't it help? Did you try writing code? If so, why didn't it work? Without that sort of information it looks like you didn't try. – the Tin Man Apr 22 '20 at 16:11

5 Answers5

2

You can use .some().

From the documentation:

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns a Boolean value.

Try this:

const values = ["1", "2", "3"];
const value = "0";

const hasValue = values.some(e => e === value);

console.log(hasValue);
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
norbitrial
  • 14,716
  • 7
  • 32
  • 59
2

You can use the every() method:

let items = [2, 5,9];
let x =  items.every((item)=>{ return item!=0; });
console.log(x);
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Cristian V
  • 157
  • 5
1
var values = ["1", "2", "3"];

var value = "0"

const isExist = !!values.find(_value => _value === value);
  • Please explain why this is useful and should be the selected answer. The goal is to educate, not just toss out code. – the Tin Man Apr 21 '20 at 22:41
  • You can solve your issue in many ways. There is nothing special in my solution, it's just one of the many ways. The main approach is using such algorithm: iterate through array until you find match. What methods will be used for that, it doesn't matter. – Николай Гольцев Apr 22 '20 at 10:46
0

There are many ways to check whether all the values in an array are not equal to a given value.

Most common methods are .every() and .some()

If don't want to use predefined methods then looping is good option or else first get distinct value in array and then use the index of() to check if index of given value is greater than zero then array all values are nit equal to given value.

0

This is pretty simple, and there are multiple answers, however, I'd recommend using a for loop as the simplest way to do this.

I like using these since, as long as you have some knowledge of JavaScript, this code is easy to read, making it versatile. A for loop would look like this:

var values = ["1", "2", "3"];
var value = "0";
var newArray = [];
for (let i = 0; i < values.length; i++) {
    if (values[i] != value) {
        newArray.push(values[i]);
    };
};
User 10
  • 177
  • 3
  • 10