多维数组判断每一项是否有值

一个数组对象里面套数组,判断这些某个数组是否有值(length > 0)
思路:(适用表单)
1.先把selected这些数组拿出来(用得map循环)
2.分别去获取每个selected数组,看其长度是否 > 0,若是有值给个标识true (用reduce实现)
3.每个selected数组中若是无值得话会一直是空数组来回替换
4.最后定义一个值去获取reduce得结果,用if判断即可,若是true得话就说明某个数组是有值得,else 那就是没值
5.reduce()用着真香,快去看看,另外一篇文章有讲用ruduce进行数组去重或合并数据

getTest() {
    
    
      const array = [
        {
    
    
          title: 'ID',
          key: '1',
          selected: []
        },
        {
    
    
          title: '姓名',
          key: '2',
          selected: []
        },
        {
    
    
          title: '昵称',
          key: '3',
          selected: []
        },
        {
    
    
          title: '年龄',
          key: '4',
          selected: []
        },
        {
    
    
          title: '爱好',
          key: '5',
          selected: []
        },
        {
    
    
          title: '电话',
          key: '6',
          selected: []
        }
      ]
      const dataList = array.map(({
    
     selected }) => {
    
    
        return ({
    
     selected })
      })
      const flage = dataList.reduce((first, item) => {
    
    
        console.log(first, item)
        return item.selected.length > 0 || first
      }, [])
      console.log(flage, 'flage-flage')
      // flage是true的时候,数组里面某个length > 0
      // 其它情况是所有数组的length <= 0
    }

猜你喜欢

转载自blog.csdn.net/weixin_45324044/article/details/115210169