I want to know whether my array contains any undefined value. My array is like this,
How to find it? I tried this method but it doesn't read 'undefined'
let newValues = []
if(values.length === selectedCertifiedList.length){
values && values.filter(v => v === (undefined || 0 || null)).map(val => {
newValues.push(val);
})
}
I want to know whether my array contains any undefined value. My array is like this,
How to find it? I tried this method but it doesn't read 'undefined'
let newValues = []
if(values.length === selectedCertifiedList.length){
values && values.filter(v => v === (undefined || 0 || null)).map(val => {
newValues.push(val);
})
}
Share
Improve this question
edited Feb 26, 2020 at 13:49
Krzysztof Krzeszewski
6,7982 gold badges21 silver badges32 bronze badges
asked Feb 20, 2020 at 8:41
Lex VLex V
1,4474 gold badges22 silver badges37 bronze badges
6
- 1 What are your exact requirements? Do you want value or index? – Ankit Kumar Gupta Commented Feb 20, 2020 at 8:49
- @ankitgupta, say correct what you want exactly, values or check contain undefined values available or not? – R.G.Krish Commented Feb 20, 2020 at 8:55
- @Leya Have you got output or not? – R.G.Krish Commented Feb 20, 2020 at 9:06
- I want to check if the array contains any undefined value, if it contains I want to stop a function. – Lex V Commented Feb 20, 2020 at 9:17
- arr.some(x=> x===undefined) – Deee Commented Feb 20, 2020 at 9:27
3 Answers
Reset to default 5arr.some(item => item === undefined)
returns true
if any item in the array is undefined
let arr = ['a', undefined, 'b']
arr.indexOf(undefined) !== -1 //true
It will return true if undefined present in the array.
Try this,
const data = ['one', 0, 'two', undefined, 'three'];
const checker = [null, undefined, 0]; //filtering conditions
const x = data.filter((item) => !checker.includes(item) )
console.log(data)
console.log(x)
//Output looks ["one","two","three"]