从另一个对象数组中过滤对象数组

时间:2018-08-21 16:49:14

标签: arrays json object

目前,我正在根据已检查属性值等于true的问题过滤数据。

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 questions = [{rows: {text: "Text 1", checked: true,fields: "1",column: "X1"}
}, {rows: {text: "Text 2", checked: true,fields: "2",column: "X1"}
}, {rows: {text: "Text 3", checked: false,fields: "1",column: "X2"}
}, {rows: {text: "Text 4", checked: false,fields: "2",column: "X2"}
}, {rows: {text: "Text 5", checked: false,fields: "3",column: "X2"}
}];
 console.log(questionArr);
// console.log(dataArr);
const res = data.filter(d => questions.find(f => d[f.rows.column] === f.rows.fields && f.rows.checked));

起作用,但在过滤下面的实际数据时不起作用。我认为下面的问题对象和实际的问题对象之间存在细微的差别。访问此类结构时,我的过滤器代码应该是什么?

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为这就是您要寻找的。我将数据结构与您问题中的图像匹配。让我知道我是否错过任何事情。

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 questions = [
  { rows: [{ text: "Text 1", checked: true, fields: "2", column: "X1" }] },
  { rows: [{ text: "Text 2", checked: true, fields: "2", column: "X1" }] },
  { rows: [{ text: "Text 3", checked: false, fields: "1", column: "X2" }] },
  { rows: [{ text: "Text 4", checked: false, fields: "2", column: "X2" }] },
  { rows: [{ text: "Text 5", checked: false, fields: "3", column: "X2" }] }
];


const result = data.filter(function(item){
  return questions.some(function(question){
    return question.rows.some(function(row){
      return (row.checked && item[row.column] === row.fields);
    });
  });
});

console.log(result);

精简版

const result = data.filter((item) => questions.some((question) => question.rows.some((row) => (row.checked && item[row.column] === row.fields))));

考虑到性能

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 questions = [
  { rows: [{ text: "Text 1", checked: true, fields: "2", column: "X1" }] },
  { rows: [{ text: "Text 2", checked: true, fields: "2", column: "X1" }] },
  { rows: [{ text: "Text 3", checked: false, fields: "1", column: "X2" }] },
  { rows: [{ text: "Text 4", checked: false, fields: "2", column: "X2" }] },
  { rows: [{ text: "Text 5", checked: false, fields: "3", column: "X2" }] }
];

const result = {};

for(let a = 0, b = data.length; a < b; a++){
  const item = data[a];

  for(let c = 0, d = questions.length; c < d; c++){
    const rows = questions[c].rows;

    for(let e = 0, f = rows.length; e < f; e++){
      const row = rows[e];

      if(row.checked && item[row.column] === row.fields){
        result[item.Name] = item;
        break;
      }
    }
  }
}

// this could be replaced with Object.values(result);
const matches = [];

for(let match in result){
  matches.push(result[match]);
} 

// not supported by IE yet
// Object.values(result);

console.log(matches);