【问题标题】:How to get only non-null value of object property from object array? [duplicate]如何从对象数组中仅获取对象属性的非空值? [复制]
【发布时间】:2026-01-18 16:15:01
【问题描述】:

在从服务器接收到的对象数组中,我想只处理和检索对象中属性的非空值。如何在我的函数中更改它??

const arr = [{ 
text01 : 'name', 
text02 : 'email@gmail.com', 
text03 : '010-1234-5678', 
text04 : 'adress1', 
text05 : 'adress2', 
text06 : null, 
text07 : null, 
text08 : null, 
}, 
{ text01 : 'name1', 
text02 : 'email2@gmail.com', text03 : '010-1255-5148', 
text04 : 'adress3', 
text05 : 'adress4', 
text06 : null, 
text07 : null, 
text08 : null, 
}] 

getDataArr(arr) { 
  arr.forEach(item => { 
    const aaa = []; 
    for (let key in item) { 
      if (item[key] !== null) { 
        const value = item[key]; 
        aaa.push({ key, value }); 
      }
    } 
    console.log(aaa); }); 

获取值为

const arr = [{ text01 : 'name', 
text02 : 'email@gmail.com', 
text03 : '010-1234-5678', 
text04 : 'adress1', 
text05 : 'adress2'
},
{ 
text01 : 'name1', 
text02 : 'email2@gmail.com', text03 : '010-1255-5148', 
text04 : 'adress3', 
text05 : 'adress4', 
}]

【问题讨论】:

标签: javascript arrays object


【解决方案1】:

删除空值的一种简洁方法是过滤对象条目,从剩余的条目中创建一个对象。这样,只需映射输入数组...

function nonNullValues(obj) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key, value]) => value !== null)
  )
}
const result = theArray().map(nonNullValues);
console.log(result)

function theArray() {
  return [{
      text01: 'name',
      text02: 'email@gmail.com',
      text03: '010-1234-5678',
      text04: 'adress1',
      text05: 'adress2',
      text06: null,
      text07: null,
      text08: null,
    },
    {
      text01: 'name1',
      text02: 'email2@gmail.com',
      text03: '010-1255-5148',
      text04: 'adress3',
      text05: 'adress4',
      text06: null,
      text07: null,
      text08: null,
    }
  ]
}

【讨论】:

    【解决方案2】:

    Lodash 如果你不介意的话。 使用:ominByisNull

    const arr = [{ text01 : 'name', text02 : 'email@gmail.com', text03 : '010-1234-5678', text04 : 'adress1', text05 : 'adress2', text06 : null, text07 : null, text08 : null} , { text01 : 'name1', text02 : 'email2@gmail.com', text03 : '010-1255-5148', text04 : 'adress3', text05 : 'adress4', text06 : null, text07 : null, text08 : null} ] 
    
    const result = arr.map(obj => _.omitBy(obj, _.isNull));
    
    console.log(result);
    .as-console-wrapper{min-height: 100%!important; top: 0}
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

    【讨论】:

      【解决方案3】:

      const arr = [
        {
          text01: "name",
          text02: "email@gmail.com",
          text03: "010-1234-5678",
          text04: "adress1",
          text05: "adress2",
          text06: null,
          text07: null,
          text08: null,
        },
        {
          text01: "name1",
          text02: "email2@gmail.com",
          text03: "010-1255-5148",
          text04: "adress3",
          text05: "adress4",
          text06: null,
          text07: null,
          text08: null,
        },
      ];
      const nonNull = []
      for(const item of arr) {
          const obj = {}
          for(const key in item) {
              if(item[key]) obj[key] = item[key]
          }
          nonNull.push(obj)
      }
      console.log(nonNull)

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案4】:

      使用您当前的逻辑,您只需几步即可首先创建此函数(或者您可以更改为方法):

      const getDataArr = (arr) => {
          const newArr = [];
          for (const item of arr) {
              const currObject = {};
              for (const key in item) {
                  if (item[key] !== null) currObject[key] = item[key];
              }
              newArr.push(currObject);
          }
          return newArr;
      };
      

      然后您可以使用以下命令打印其结果:

      console.log(getDataArr(arr));
      

      【讨论】:

        【解决方案5】:

        这边

        const arr = 
          [ { text01 : 'name'
            , text02 : 'email@gmail.com'
            , text03 : '010-1234-5678'
            , text04 : 'adress1'
            , text05 : 'adress2'
            , text06 : null
            , text07 : null
            , text08 : null
            } 
          , { text01 : 'name1'
            , text02 : 'email2@gmail.com'
            , text03 : '010-1255-5148'
            , text04 : 'adress3'
            , text05 : 'adress4'
            , text06 : null
            , text07 : null
            , text08 : null
            } 
          ] 
        arr.forEach(obj=>
          {
          Object.keys(obj).forEach(key =>
            {
            if (obj[key] === null) delete obj[key] 
            })
          })
        
        console.log( arr)
        .as-console-wrapper {max-height: 100%!important;top:0 }

        【讨论】: