Javascript 如何从属性等于Null的数组中删除对象-Lodash
我有一个这样的对象数组:Javascript 如何从属性等于Null的数组中删除对象-Lodash,javascript,lodash,Javascript,Lodash,我有一个这样的对象数组: var a = [ { "ClientSideAction": 1, "CompletedDate": "not null", "ItemDescription": "Step 1" }, { "ClientSideAction": 1, "CompletedDate": null, "ItemDescription": "step 2" }, { "ClientSideAction": 1,
var a = [
{
"ClientSideAction": 1,
"CompletedDate": "not null",
"ItemDescription": "Step 1"
},
{
"ClientSideAction": 1,
"CompletedDate": null,
"ItemDescription": "step 2"
},
{
"ClientSideAction": 1,
"CompletedDate": "not null",
"ItemDescription": "Step 3"
},
{
"ClientSideAction": 1,
"CompletedDate": null,
"ItemDescription": "step 4"
}
];
如何删除CompletedDate==null
的元素
我已经尝试了\u dropWhile
,但是当函数返回false时它就会停止,这不是我想要的。我想遍历所有对象,并删除那些符合该条件的对象。现在,我知道我可以使用常规js来实现这一点,但如果可能的话,我想使用lodash。我是洛达斯的初学者,我正在努力变得更好
这是我使用的.dropWhile:
var a2 = _.dropWhile(a, function(o) { return o.CompletedDate == null; });
不需要洛达斯,只需过滤
var res = a.filter(x => x.CompletedDate !== null);
您可以使用本机
Array.filter()
来筛选项目
var a=[
{
“客户端操作”:1,
“CompletedDate”:“不为空”,
“项目描述”:“步骤1”
},
{
“客户端操作”:1,
“CompletedDate”:空,
“项目描述”:“步骤4”
}
];
变量b=a.过滤器(功能(项目){
return item.CompletedDate!==null;
});
控制台日志(b)代码>您可以使用
请显示您迄今为止尝试过的代码。我很确定OP的意思是每个非空的CompletedDate
都是一个字符串,而不是一个表示“notnull”
的文本字符串。您可能是对的,但在这种情况下,为什么不在声明中只对字段进行OMM操作呢?他们可能不是在声明它,它可以从API中提取。或者它们的实现可能取决于字段的存在、null或其他。
var a = [
{
"ClientSideAction": 1,
"CompletedDate": "not null",
"ItemDescription": "Step 1"
},
{
"ClientSideAction": 1,
"CompletedDate": null,
"ItemDescription": "step 2"
},
{
"ClientSideAction": 1,
"CompletedDate": "not null",
"ItemDescription": "Step 3"
},
{
"ClientSideAction": 1,
"CompletedDate": null,
"ItemDescription": "step 4"
}
];
var a = a.filter(function(v) {
return v.CompletedDate != null;
})
console.log(a)