在javascript中检查键是否有价值

时间:2018-12-06 11:45:02

标签: javascript

const sampleObj = {
    yearOne: [],
    yearTwo:[{name:"test1", age: "26"}],
    yearThree:[],
    yearFour:[{name:"test3", age: "23"}]
}

以上是我的示例对象,我需要从该对象中删除所有空键值。我尝试过这段代码,来自互联网

const filterObj = Object.keys(sampleObj).forEach(key =>
    (sampleObj[key] && typeof sampleObj[key] === 'object') && delete sampleObj[key]);

但不为我工作。我得到的是undefined如何解决这个问题。

我需要打印filterObj,就像这样

{
    yearTwo:[{name:"test1", age: "26"}],
    yearFour:[{name:"test3", age: "23"}]
}

4 个答案:

答案 0 :(得分:2)

您可以使用reduce方法并检查数组中是否包含任何元素。

const sampleObj = {yearOne: [],yearTwo:[{name:"test1", age: "26"}],yearThree:[],yearFour:[{name:"test3", age: "23"}]}

const filterObj = Object.entries(sampleObj)
  .reduce((r, [k, v]) => {
    if (v.length) r[k] = v;
    return r;
  }, {})

console.log(filterObj)

答案 1 :(得分:1)

  

检查键是否在javascript中具有值

我应该注意,示例对象中的全部属性具有值:它们都包含对数组的引用。这些数组中有几个是空的,我想是要删除/排除的数组。

  

我正在获取未解决的fliterObj。

forEach始终返回undefined

由于您的代码正在修改对象,因此您无需使用该返回值。但是逻辑有点过头(它会删除具有真实值的任何属性),而您最好创建一个新对象(因为delete对对象进行了优化)。

简单的方法是使用循环,也许使用Object.entries(必要时可以很容易地填充到较旧的环境中):

const filteredObj = {};
for (const [key, value] of Object.entries(sampleObj)) {
    if (value && (!Array.isArray(value) || value.length)) {
        filteredObj[key] = value;
    }
}

实时示例:

const sampleObj = {
    yearOne: [],
    yearTwo:[{name:"test1", age: "26"}],
    yearThree:[],
    yearFour:[{name:"test3", age: "23"}]
};
const filteredObj = {};
for (const [key, value] of Object.entries(sampleObj)) {
    if (value && (!Array.isArray(value) || value.length)) {
        filteredObj[key] = value;
    }
}
console.log(filteredObj);
.as-console-wrapper {
  max-height: 100% !important;
}

但是,如果愿意,可以将它塞入reduce调用中(因为任何数组操作都可以塞入reduce调用中):

const sampleObj = {
    yearOne: [],
    yearTwo:[{name:"test1", age: "26"}],
    yearThree:[],
    yearFour:[{name:"test3", age: "23"}]
};
const filteredObj = Object.keys(sampleObj).reduce((obj, key) => {
    const value = sampleObj[key];
    if (value && (!Array.isArray(value) || value.length)) {
        obj[key] = value;
    }
    return obj;
}, {});

实时示例:

const sampleObj = {
    yearOne: [],
    yearTwo:[{name:"test1", age: "26"}],
    yearThree:[],
    yearFour:[{name:"test3", age: "23"}]
};
const filteredObj = Object.keys(sampleObj).reduce((obj, key) => {
    const value = sampleObj[key];
    if (value && (!Array.isArray(value) || value.length)) {
        obj[key] = value;
    }
    return obj;
}, {});
console.log(filteredObj);
.as-console-wrapper {
  max-height: 100% !important;
}

尽管它什么都没买。

答案 2 :(得分:1)

您可以尝试

const sampleObj = {
    yearOne: [],
    yearTwo:[{name:"test1", age: "26"}],
    yearThree:[],
    yearFour:[{name:"test3", age: "23"}]
}

Object.keys(sampleObj).map(e=>{
  if(sampleObj[e] && sampleObj[e].length === 0){
    delete sampleObj[e]
  }
})

console.log(sampleObj)

答案 3 :(得分:1)

forEach不返回结果。迭代原始实例。我修复了您的代码,如下所示。

 const sampleObj = {
        yearOne: [],
        yearTwo:[{name:"test1", age: "26"}],
        yearThree:[],
        yearFour:[{name:"test3", age: "23"}]
    }

    Object.keys(sampleObj).forEach(key => {
       if (sampleObj[key].length == 0) 
           delete sampleObj[key];
    });
    
    
    console.log(sampleObj);