3 回答
TA贡献1848条经验 获得超6个赞
使用数组过滤方法。filter() 方法创建一个数组,其中填充了所有通过测试的数组元素。数组过滤器的语法:array.filter(function(currentValue, index, arr), thisValue)
const data = [
{ 0: { key: "zeroline", door: "zero" } },
{ 1: { key: "oneline", door: "one" } },
{ 2: { key: "twoline", door: "two" } },
{ 3: { key: "threeline", door: "three" } },
];
let ret = data.filter((x, i) => x[i].key !== "oneline" && x[i].key !== "twoline");
console.log(ret);
TA贡献1817条经验 获得超6个赞
一种方法是:
const result = Object.keys(data).reduce((filtered,key,index) => {
//Getting specific object of array
//Example for first: data[0]["0"] === {key: "zeroline",door "zero"}
const object = data[index][key];
//Check if object key is zeroline or twoline
if(object.key === "zeroline" || object.key === "twoline"){
//If it is we push object to array
filtered.push(object)
}
return filtered
},[])
TA贡献1864条经验 获得超6个赞
你可以用过滤掉它Array#filter。
const keywords = ['twoline', 'oneline'], data = [{0: {key: "zeroline", door: "zero"}},{1: {key: "oneline", door: "one"}},{2: {key: "twoline", door: "two"}},{3: {key: "threeline", door: "three"}}];
const res = data.filter((v, i) => keywords.indexOf(v[i].key) === -1);
console.log(res);
添加回答
举报