删除没有“category”字段的数组
req:想摆脱obj [0]&obj [2],因为他们没有hav类别字段......
例如:
js数据:
var obj=[
{"email":"[email protected]","event":"open"},
{"ip":"24.38.43.233","email":"[email protected]","category":["webinar"]},
{"email":"[email protected]","event":"open"},
{"ip":"24.98.43.230","email":"[email protected]","category":["webinar"]},
{"ip":"24.77.55.931","email":"[email protected]","category":["webinar"]},
{"ip":"44.67.85.456","email":"[email protected]","category":["webinar"]}
];
预期o / p:
var obj = [
{"ip":"24.38.43.233","email":"[email protected]","category":["webinar"]},
{"ip":"24.98.43.230","email":"[email protected]","category":["webinar"]},
{"ip":"24.77.55.931","email":"[email protected]","category":["webinar"]},
{"ip":"44.67.85.456","email":"[email protected]","category":["webinar"]}
];
问题被编辑...为了简单的理解,提前感谢....
我不确定我是否完全理解这个问题,但我假设您想要摆脱所有没有category
属性的对象。这个片段会做到这一点。
var obj=[
{"email":"[email protected]","event":"open"},
{"ip":"24.38.43.233","email":"[email protected]","category":["webinar"]},
{"email":"[email protected]","event":"open"},
{"ip":"24.38.43.230","email":"[email protected]","category":["webinar"]}
];
var result = obj.filter(participation => participation.hasOwnProperty('category'));
console.log(result);