嵌套数组的深度过滤

时间:2020-10-08 16:20:01

标签: javascript

我需要像这样的字符串值数组过滤嵌套数组:

const filters = ["Berlin", "Paris"]

这是需要过滤的基本阵列(需要根据location键过滤作业阵列)

const departments = [
  {
    name: "Brand",
    children: [
      {
        name: "Marketing",
        jobs: [
          { location: "Paris", title: "Brand Manager" },
          { location: "Berlin", title: "Brand Designer" },
          { location: "New York", title: "Brand Designer" }
        ],
      },
    ],
  },
  {
    name: "Business",
    children: [
      {
        name: "People",
        jobs: [
          { location: "Paris", title: "Office Manager" },
          { location: "Berlin", title: "Office Manager" },
          { location: "New York", title: "Office Manager" }
        ],
      },
    ],
  },
];

预期输出为:

const filteredDepartments = [
  {
    name: "Brand",
    children: [
      {
        name: "Marketing",
        jobs: [
          { location: "Paris", title: "Brand Manager" },
          { location: "Berlin", title: "Brand Designer" }
        ],
      },
    ],
  },
  {
    name: "Business",
    children: [
      {
        name: "People",
        jobs: [
          { location: "Paris", title: "Office Manager" },
          { location: "Berlin", title: "Office Manager" }
        ],
      },
    ],
  },
];

任何帮助/指针都将不胜感激,谢谢!

4 个答案:

答案 0 :(得分:1)

像这样?这有点直截了当,但它适用于该结构,并且只有2层深。如果您需要工作n个级别,则需要递归。

const filters = ["Berlin", "Paris"];

const departments = [
  {
    name: "Brand",
    children: [
      {
        name: "Marketing",
        jobs: [
          { location: "Paris", title: "Brand Manager" },
          { location: "Berlin", title: "Brand Designer" },
          { location: "New York", title: "Brand Designer" }
        ],
      },
    ],
  },
  {
    name: "Business",
    children: [
      {
        name: "People",
        jobs: [
          { location: "Paris", title: "Office Manager" },
          { location: "Berlin", title: "Office Manager" },
          { location: "New York", title: "Office Manager" }
        ],
      },
    ],
  },
];

const filteredDepartments = departments.map(department => {
  department.children = department.children.map(child => {
    child.jobs = child.jobs.filter(job => filters.includes(job.location));
    
    return child;
  });
  
  return department;
});

console.log(filteredDepartments);

答案 1 :(得分:1)

请尝试使用此代码段。这段代码不会改变原始数组。

const filters = ["Berlin", "Paris"]
const departments = [
  {
name: "Brand",
children: [
  {
    name: "Marketing",
    jobs: [
      { location: "Paris", title: "Brand Manager" },
      { location: "Berlin", title: "Brand Designer" },
      { location: "New York", title: "Brand Designer" }
    ],
  },
],
  },
  {
name: "Business",
children: [
  {
    name: "People",
    jobs: [
      { location: "Paris", title: "Office Manager" },
      { location: "Berlin", title: "Office Manager" },
      { location: "New York", title: "Office Manager" }
    ],
  },
],
  },
];


const filteredDepartments = departments.map(department => {
  let children = [];
  children = department.children.map(child => {
let jobs = [...child.jobs];
jobs = jobs.filter(job => filters.includes(job.location));
return {
  name: child.name,
  jobs
};
  });
  return {
name: department.name,
children
  };
});
console.log(filteredDepartments);

答案 2 :(得分:1)

   const filters = ["Berlin", "Paris"];

const departments = [
  {
name: "Brand",
children: [
  {
    name: "Marketing",
    jobs: [
      { location: "Paris", title: "Brand Manager" },
      { location: "Berlin", title: "Brand Designer" },
      { location: "New York", title: "Brand Designer" }
    ],
  },
],
  },
  {
name: "Business",
children: [
  {
    name: "People",
    jobs: [
      { location: "Paris", title: "Office Manager" },
      { location: "Berlin", title: "Office Manager" },
      { location: "New York", title: "Office Manager" }
    ],
  },
],
  },
];

const filteredDepartments = departments.map(({name, children}) => {
  const filteredChildren = children.map(({name, jobs}) => {
  const newJobs = jobs.filter(job => filters.includes(job.location));

    return {name, jobs: newJobs};
  });
  
  return {name, children: filteredChildren};
});
console.log('filteredDepartments => ', filteredDepartments);
console.log('departments =>', departments);

答案 3 :(得分:1)

如果没有匹配的作业,此答案将从lateinit var passcodeComponent: PasscodeComponent override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val context = activity?.applicationContext ?: return passcodeComponent = (context as PasscodeSetupComponentProvider).providePasscodeSetupComponent() passcodeComponent.inject(this) 数组中删除child,如果没有子项,则从children数组中删除department

departments

此答案使用object destructuringobject spread语法。如果您不知道这些内容,请查看链接的文档。