根据嵌套值过滤嵌套数组

时间:2018-10-08 23:32:09

标签: javascript ecmascript-6 filtering

如果我拥有以下数据,该如何使用ES6语法过滤每个students中的node数组,因此它仅返回subjects中具有特定值的学生(例如subject ='English')?

数据:

[
  {
    "node": {
      "name": "Miss Jones",
      "students": [
        {
          "name": "John",
          "subjects": ["English", "Maths"]
        },
        {
          "name": "Sarah",
          "subjects": ["Geography"]
        }
      ]
    }
  },
  {
    "node": {
      "name": "Mr Hudson",
      "students": [
        {
          "name": "Joe",
          "subjects": ["Maths", "French"]
        },
        {
          "name": "Samantha",
          "subjects": ["English"]
        }
      ]
    }
  }
]

预期输出:

[
  {
    "node": {
      "name": "Miss Jones",
      "students": [
        {
          "name": "John",
          "subjects": ["English", "Maths"]
        }
      ]
    }
  },
  {
    "node": {
      "name": "Mr Hudson",
      "students": [
        {
          "name": "Samantha",
          "subjects": ["English"]
        }
      ]
    }
  }
]

谢谢!

4 个答案:

答案 0 :(得分:1)

是的,您可以使用mapfilter来做到这一点。

let nodes = [
  {
    "node": {
      "name": "Miss Jones",
      "students": [
        {
          "name": "John",
          "subjects": ["English", "Maths"]
        },
        {
          "name": "Sarah",
          "subjects": ["Geography"]
        }
      ]
    }
  },
  {
    "node": {
      "name": "Mr Hudson",
      "students": [
        {
          "name": "Joe",
          "subjects": ["Maths", "French"]
        },
        {
          "name": "Samantha",
          "subjects": ["English"]
        }
      ]
    }
  }
];

const englishNodes = nodes.map(n => {
  n.node.students = n.node.students.filter(s => s.subjects.includes('English'));
  return n;
});

console.log(englishNodes);

答案 1 :(得分:1)

我将使用.map.filter过滤数据。

const data = [{ "node": { "name": "Miss Jones", "students": [{ "name": "John", "subjects": ["English", "Maths"] }, { "name": "Sarah", "subjects": ["Geography"] }] } }, { "node": { "name": "Mr Hudson", "students": [{ "name": "Joe", "subjects": ["Maths", "French"] }, { "name": "Samantha", "subjects": ["English"] }] } }];

const results = data.map(({ node }) => {
    const students = node.students.filter(student =>
        student.subjects.includes('English'));
    return {
        node: {
            ...node,
            students
        }
    };
});

console.log(results);

答案 2 :(得分:1)

即使处理程序正在更改原始数组,您也不需要像其他答案所建议的功能map

只需使用函数forEach进行循环,并使用函数filter和函数includes来检查数组subjects

const arr = [  {    "node": {      "name": "Miss Jones",      "students": [        {          "name": "John",          "subjects": ["English", "Maths"]        },        {          "name": "Sarah",          "subjects": ["Geography"]        }      ]    }  },  {    "node": {      "name": "Mr Hudson",      "students": [        {          "name": "Joe",          "subjects": ["Maths", "French"]        },        {          "name": "Samantha",          "subjects": ["English"]        }      ]    }  }],
      subject = 'English';
  
arr.forEach(o => o.node.students = o.node.students.filter(s => s.subjects.includes(subject)));

console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 3 :(得分:0)

您想做的是转换数据,而不仅仅是过滤。请尝试以下操作:

data.map((o) => {
  o.students = o.node.students.filter((s) => s.subjects.indexOf("English") >= 0);
  if (o.students.length >= 0) return o;
  return null;
}).filter((o) => o);

对于没有English主题的学生,您必须返回null,然后对null进行过滤以忽略他们