从属性和值集中动态删除基于数组/对象

时间:2018-08-03 18:11:28

标签: javascript arrays angular typescript

假设我有一个对象数组,可以说数据

[ 
  0: { Name: "foo", X1 : "1", X2 : "1", Other : "Test1" },
  1: { Name: bar, X1 : "2", X2 : "2", Other : "Test2" },
  2: { Name: test, X1 : "2", X2 : "3", Other : "Test3" }
]

我还有另一个对象数组,例如字段

[
 0: { 
       rows: [ 
             { checked: true, value: "1", field: "X1"} ,
             {  checked: false, value: "2", field: "X1"} 
             ]
     }
 1: { 
       rows: [
             {  checked: false, value: "1", field: "X2"},
             {  checked: false, value: "2", field: "X2"},
             {  checked: false, value: "3", field: "X2"}
             ]
     }
]

如何删除属性名称中具有“假”值的数据。字段和值都应该是动态的,并应根据示例输出:

[ 
  0: { Name: "foo", X1 : "1", X2 : "1", Other : "Test1" }
]

因为它的X1 = 2

我尝试了以下

FilterData( fields, data): object{
    let $result = datafile.filter(function (o) {
      return Object.keys(toRemove).every(function(k){
        return toRemove[k].some(function (f){
          return o[k] !== f;
        })
      })
    })
    return $result;
  }

但出现错误“ toRemove [k] .some不是一个函数”,我正在使用角度和打字稿

1 个答案:

答案 0 :(得分:0)

我只是想这就是你想要的。

const data = [{Name: "foo", X1: "1", X2: "1", Other: "Test1"},{Name: "bar",
X1: "2",X2: "2",Other: "Test2"},{Name: "test",X1: "2",X2: "3",Other: "Test3"}
];
const fields = [{rows: {checked: true,value: "1",field: "X1"}
}, {rows: {checked: false,value: "2",field: "X1"}
}, {rows: {checked: true,value: "1",field: "X2"}
}, {rows: {checked: true,value: "2",field: "X2"}
}, {rows: {checked: true,value: "3",field: "X2"}
}];


const res = data.filter(d => !fields.find(f => d[f.rows.field] === f.rows.value && !f.rows.checked));

console.log(res);