JS去除对象数组中指定字段为空的数据

发布时间 2023-07-13 09:36:14作者: Nancy*

 去掉为空字段

const filteredArr = this.arouselList.filter((obj) => {
                      return !(Object.prototype.hasOwnProperty.call(obj, 'pic') && (obj.pic === null || obj.pic === undefined));
                    });

去掉不为空的字段

const filteredArr = arr.filter((obj) => {
  return Object.prototype.hasOwnProperty.call(obj, 'age') && (obj.age === null || obj.age === undefined);
});
console.log(filteredArr); // [{id: 2, name: 'bar', age: null}, {id: 3, name: 'baz', age: undefined}, {id: 4, name: 'qux'}]