最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to check if an array contains any undefined value in javascript - Stack Overflow

programmeradmin3浏览0评论

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
 |  Show 1 more ment

3 Answers 3

Reset to default 5

arr.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"]
发布评论

评论列表(0)

  1. 暂无评论