Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在对象数组中遍历对象属性数组_Javascript_Arrays_Sorting_Ecmascript 6 - Fatal编程技术网

Javascript 如何在对象数组中遍历对象属性数组

Javascript 如何在对象数组中遍历对象属性数组,javascript,arrays,sorting,ecmascript-6,Javascript,Arrays,Sorting,Ecmascript 6,我有一个对象数组,如下所示: data = [ { title: 'John Doe', departments: [ { name: 'Marketing', slug: 'marketing'}, { name: 'Sales', slug: 'sales'}, { name: 'Administration', slug: 'administration'}, ] }, { title: 'John Doe J

我有一个对象数组,如下所示:

data = [
  {
    title: 'John Doe',
    departments: [
      { name: 'Marketing', slug: 'marketing'},
      { name: 'Sales', slug: 'sales'},
      { name: 'Administration', slug: 'administration'},
    ]
  },
  {
    title: 'John Doe Junior',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Sales', slug: 'sales'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]
operations = [
  {
    title: 'John Doe Junior',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Sales', slug: 'sales'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]

marketing = [
  {
    title: 'John Doe',
    departments: [
      { name: 'Marketing', slug: 'marketing'},
      { name: 'Sales', slug: 'sales'},
      { name: 'Administration', slug: 'administration'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]
如何迭代每个对象的departments数组并创建新的数组,让员工按部门排序,以便最终结果如下:

data = [
  {
    title: 'John Doe',
    departments: [
      { name: 'Marketing', slug: 'marketing'},
      { name: 'Sales', slug: 'sales'},
      { name: 'Administration', slug: 'administration'},
    ]
  },
  {
    title: 'John Doe Junior',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Sales', slug: 'sales'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]
operations = [
  {
    title: 'John Doe Junior',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Sales', slug: 'sales'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]

marketing = [
  {
    title: 'John Doe',
    departments: [
      { name: 'Marketing', slug: 'marketing'},
      { name: 'Sales', slug: 'sales'},
      { name: 'Administration', slug: 'administration'},
    ]
  },
  {
    title: 'Rick Stone',
    departments: [
      { name: 'Operations', slug: 'operations'},
      { name: 'Marketing', slug: 'marketin'},
    ]
  },
]
动态创建这种数组的方法是什么

更新

我尝试使用答案中的建议提出一个解决方案,其中我将使用department对象动态创建一个数组,该数组将包含一个员工数组:

const isInDepartment = departmentToCheck => employer => employer.departments.find(department => department.slug == departmentToCheck);

var departments = [];
function check(departments, name) {
  return departments.some(object => name === object.department);
}

employees.forEach((employee) => {
    employee.departments.forEach((department) => {
        let found = check(departments, department.slug);
        if (!found) { 
            departments.push({ department: department.slug }); 
        }
        });
});

departments.forEach((department) => {
    // push an array of employees to each department
    //employees.filter(isInDepartment(department));
});
但是,我不知道如何将Employee数组推送到最后循环的数组中的对象?
这是我的房间。

这个怎么样?我使用
Array.prototype.filter
操作,并使用高阶函数(在本例中是返回函数的函数)创建谓词(返回布尔值的函数),以检查员工是否在特定部门。我在代码中添加了一些(希望)澄清的注释

编辑:使用您提供的新代码和上下文,可以显示它如何协同工作

const employees=[
{
标题:“约翰·多伊”,
部门:[
{name:'Marketing',slug:'Marketing'},
{name:'Sales',slug:'Sales'},
{name:'Administration',slug:'Administration'}
]
},
{
标题:“小约翰·多伊”,
部门:[
{name:'Operations',slug:'Operations'},
{name:'Sales',slug:'Sales'}
]
},
{
标题:“瑞克·斯通”,
部门:[
{name:'Operations',slug:'Operations'},
{name:'Marketing',slug:'marketin'}
]
}
];
//给定一个部门,这将返回一个检查
//员工是否在指定的部门
//注意:“find”返回找到的对象(truthy)
//或未定义(falsy),如果未找到匹配项。
const isInDepartment=
部门检查=>
employee=>employee.departments.find(dep=>dep.name==departmentToCheck);
const employeesInMarketing=employees.filter(isInDepartment(‘Marketing’);
const employeesInOperations=employees.filter(isInDepartment(‘Operations’);
console.log(“营销中的员工”,营销中的员工);

console.log(“员工参与运营”,员工参与运营)你试过什么?您需要使用和方法。@T.J.Crowder我已经编辑了问题并修复了语法,这是什么类型的语法?您可以使用console.log输出json中变量的清晰表示,如下所示:console.log(json.stringify(someVar));我再次编辑了这个问题,之前我只是从chrome的控制台上复制它,我为此道歉,太好了!你被困在哪里了?你的研究结果是什么?另请参见:除非我遗漏了什么,否则这只是嵌套循环(答案中提供的菜单中您最喜欢的口味)。(不是我的dv)我不明白对此投反对票的原因。这需要更多的解释,但这并不是否决它的理由。谢谢。嗯,这是一个非常快的否决票(据我所知是即时的),所以我开始想,当你回答一个分数为负数的问题时,这是否是自动否决票?(我以前没有制定过这样的规则,但这会有一些道理)。顺便说一句,我在代码中添加了一些注释。你认为还有更多的解释空间吗?如果是,在哪里?非常感谢您的建议,我已经用您的代码更新了我的问题,我正在尝试使用departments对象动态创建一个数组,然后检查员工,但是由于某些原因,我在找到的函数中没有得到不同的值。您忘记了从
找到的
函数返回。它应该是
返回部门。一些(函数(el)…
。如果您只是对作为字符串的部门名称感兴趣,那么您可以直接推送字符串(而不是推送带有字符串作为属性的新对象)。非常感谢您的回答,这正是我要找的!再次感谢!