我想过滤掉我的具有null或""值的对象数组中的对象。
let data = [{
"name": "Product 2",
"link": "/stock/product2",
"category": "234",
"description": ""
}, {
"name": "Product 1",
"link": "/stock/product1",
"category": "1231",
"description": ""
}, {
"name": "",
"link": null,
"ticker": "",
"description": ""
}]
data = data.filter(cv => (cv.name === "" && cv.link === null));
console.log(JSON.stringify(data))Run Code Online (Sandbox Code Playgroud)
如您在上面所看到的,我目前得到了假对象。我想回来:
{
"name": "Product 2",
"link": "/stock/product2",
"category": "234",
"description": ""
}, {
"name": "Product 1",
"link": "/stock/product1",
"category": "1231",
"description": ""
}
Run Code Online (Sandbox Code Playgroud)
有什么建议我做错了吗?
因为filter按照您的想法无法正常工作,所以它保留了满足条件的元素,因此反转条件,它应该可以按预期工作:
let data = [{
"name": "Product 2",
"link": "/stock/product2",
"category": "234",
"description": ""
}, {
"name": "Product 1",
"link": "/stock/product1",
"category": "1231",
"description": ""
}, {
"name": "",
"link": null,
"ticker": "",
"description": ""
}]
data = data.filter(cv => !(cv.name === "" || cv.link === null));
console.log(JSON.stringify(data))Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |