Count of “Defined” Array Elements

后端 未结 7 1591
无人及你
无人及你 2020-12-03 04:30

Given following array:

var arr = [undefined, undefined, 2, 5, undefined, undefined];

I\'d like to get the count of elements which are

7条回答
  •  悲&欢浪女
    2020-12-03 05:11

    Remove the values then check (remove null check here if you want)

    const x = A.filter(item => item !== undefined || item !== null).length
    

    With Lodash

    const x = _.size(_.filter(A, item => !_.isNil(item)))
    

提交回复
热议问题