使用lodash在集合中的数组中搜索和删除空值

时间:2017-05-12 14:24:16

标签: javascript lodash

我们说我有这样的对象:

[{
  title: 'first',
  someField: 'someValue',
  someArr: [1,2,null,null,5,null],
  someNestedObj: {
    title: 'firstNested',
    someField: 'someNestedValue',
    someNestedArr: [null,null,3],
      someNestedNestedObj: {
      title: 'firstNestedNested',
      someField: 'someNestedNestedValue',
      someNestedNestedArr: [1,2,null]
    }
  }
},
{
  title: 'second',
  someField: 'someValue',
  someArr: [1,2,null,null,5,null],
  someNestedObj: {
    title: 'firstNested',
    someField: 'someNestedValue',
    someNestedArr: [null,null,3],
      someNestedNestedObj: {
      title: 'firstNestedNested',
      someField: 'someNestedNestedValue',
      someNestedNestedArr: [1,2,null]
    }
  }
}]

在嵌套属性中从数组中删除null的最快方法是什么?

我试图用_.omit& _.omitBy但这不是一个好主意...... 也许有人有一个很好的解决方案?

3 个答案:

答案 0 :(得分:1)

如果value是数组,则使用递归来循环数据并过滤掉空值。



var data = [{"title":"first","someField":"someValue","someArr":[1,2,null,null,5,null],"someNestedObj":{"title":"firstNested","someField":"someNestedValue","someNestedArr":[null,null,3],"someNestedNestedObj":{"title":"firstNestedNested","someField":"someNestedNestedValue","someNestedNestedArr":[1,2,null]}}},{"title":"second","someField":"someValue","someArr":[1,2,null,null,5,null],"someNestedObj":{"title":"firstNested","someField":"someNestedValue","someNestedArr":[null,null,3],"someNestedNestedObj":{"title":"firstNestedNested","someField":"someNestedNestedValue","someNestedNestedArr":[1,2,null]}}}]

function deleteNull(data) {
  for(var i in data) {
    if(Array.isArray(data[i])) data[i] = data[i].filter(e => e != null)
    if(typeof data[i] == 'object') deleteNull(data[i])
  }
}

deleteNull(data)
console.log(data)




答案 1 :(得分:0)

一种可能的方法(但是,你必须小心这里的变量名),所以只应用领域知识:

 Route::get('/login',[
    'uses' => 'userController@user',
    'as' => 'login'
]);
 Route::post('/signin',[
    'uses' => 'userController@postSignIn',
    'as' => 'signin',
    'middleware'=>'auth',
]);

其中src是您的源对象。结果是你的对象没有空值。

基本上你用字符串操作删除空值,所以如果'null'是属性名称或字符串值的一部分,它将不起作用。

答案 2 :(得分:0)

您可以使用lodash#cloneDeepWithlodash#reject使用lodash#isNull删除所有null值。请注意,lodash#cloneDeepWith递归遍历数组和对象,只要您不返回所需的结果即可。要继续遍历空过滤的数组,我们必须再次使用lodash#maplodash#cloneDeepWith

var result = _.cloneDeepWith(data, function cb(v) {
  if(_.isArray(v)) {
    return _(v).reject(_.isNull)
      .map(_.partial(_.cloneDeepWith, _, cb)).value();
  }
});

var data = [{
  title: 'first',
  someField: 'someValue',
  someArr: [1,2,null,null,5,null],
  someNestedObj: {
    title: 'firstNested',
    someField: 'someNestedValue',
    someNestedArr: [null,null,3],
      someNestedNestedObj: {
      title: 'firstNestedNested',
      someField: 'someNestedNestedValue',
      someNestedNestedArr: [1,2,null]
    }
  }
},
{
  title: 'second',
  someField: 'someValue',
  someArr: [1,2,null,null,5,null],
  someNestedObj: {
    title: 'firstNested',
    someField: 'someNestedValue',
    someNestedArr: [null,null,3],
      someNestedNestedObj: {
      title: 'firstNestedNested',
      someField: 'someNestedNestedValue',
      someNestedNestedArr: [1,2,null]
    }
  }
}];

var result = _.cloneDeepWith(data, function cb(v) {
  if(_.isArray(v)) {
    return _(v).reject(_.isNull)
      .map(_.partial(_.cloneDeepWith, _, cb)).value();
  }
});

console.log(result);
body > div { min-height: 100%; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>