-2
How do I check the internal contents of an array?
For example:
var teste = [1, 2, 3, -4, 0]
How do I check that the internal content is less than 0 within the array?
-2
How do I check the internal contents of an array?
For example:
var teste = [1, 2, 3, -4, 0]
How do I check that the internal content is less than 0 within the array?
0
face to check if the array returns empty, you can use the method length
,
the question is unclear. However, you can do the following.
var teste = [1, 2, 3, -4, 0]
if(teste.length > 0){
console.log('O array não é vazio')}
else{
console.log('Array Vazio')
}
0
You can use the method filter()
, in which case it will go through the array and delete the positive numbers, the ones that will remain in the array are only the negative or zeroed.
const array = [-1, 0, 1, 2];
const qntNegativos = array.filter(nr => nr <= 0).length;
console.log(`Existe(m) ${qntNegativos} numero(s) negativo(s) ou zerado(s).`)
Reference: filter()
Browser other questions tagged javascript array
You are not signed in. Login or sign up in order to post.
Do you need to check in all positions? If so, study the
filter
of arrays javascript.– Woss
I think the question is unclear. I thought he wanted to know if the array has more than 0 elements ("internal contents of the array")...
– Luiz Felipe
You want to know the internal sum of the array is less than or equal to 0 or if there is any value less than 0?
– leofalmeida
Sorry for the somewhat clear question, but I need to check the array items and if it is less than or equal to zero, if these numbers are greater than 2 do an action.
– Xcat
So if it is
menor ou igual à 0
should an action be taken and if it ismaior que 2
one must make another?– leofalmeida
this, if it is less or equal returns true and otherwise returns false
– Xcat
Ask the question how you want the output to be.
– leofalmeida
"...I need to check the array items and if it is less than or equal to zero..." the syntax and semantic errors suggest a conflicting question, the "I need to check the array items" part suggests that you have to iterate and compare the items. The "if it is less than or equal to zero" part suggests that you do not need to iterate but use the array length.
– Augusto Vasques