如何通过对象属性的迭代一个数组,在对象

问题描述:

我有对象的数组的数组,看起来像这样:如何通过对象属性的迭代一个数组,在对象

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'}, 
    ] 
    }, 
] 

什么是动态地创建这种阵列的方法是什么?

更新

我试图想出了使用从答案,在那里我会动态地创建部门对象的数组,将有员工组成的数组建议的解决方案:

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)); 
}); 

但是,我不知道如何将员工数组推到数组中的最后一个循环中的对象? 这是fiddle

+0

你尝试过什么?您需要使用['Object'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor)和['Array'](https:// developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2)方法。 – Xufox

+0

@ T.J.Crowder我编辑了问题并修复了语法 – Leff

+0

这是什么类型的语法?你可以使用console.log来输出一个干净的json变量表示形式,如下所示:console.log(JSON.stringify(someVar)); – Ericson578

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

编辑:使用您提供的新代码和上下文this JSFiddle demo显示它将如何协同工作。

const employees = [ 
 
    { 
 
    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'} 
 
    ] 
 
    }, 
 
    \t { 
 
    title: 'Rick Stone', 
 
    departments: [ 
 
     { name: 'Operations', slug: 'operations'}, 
 
     { name: 'Marketing', slug: 'marketin'} 
 
    ] 
 
    } 
 
]; 
 

 
// given a department, this returns a function that checks 
 
// whether an employee is in the specified department 
 
// NOTE: the "find" returns the found object (truthy) 
 
// or undefined (falsy) if no match was found. 
 
const isInDepartment = 
 
\t departmentToCheck => 
 
    \t employee => employee.departments.find(dep => dep.name == departmentToCheck); 
 

 
const employeesInMarketing = employees.filter(isInDepartment('Marketing')); 
 
const employeesInOperations = employees.filter(isInDepartment('Operations')); 
 

 
console.log('Employees in marketing', employeesInMarketing); 
 
console.log('Employees in operations', employeesInOperations);

+0

我不明白这个downvoting的原因,它可以用更多的解释,但是这不是什么理由来downvote它 –

+0

谢谢,那是一个非常快(即时据我所知),所以我开始想知道,当你回答一个带有负分的问题时它是否会自动降级?(不是以前任何这样的规则的头,但它会使*一些*感觉)。Btw。我在代码中添加了一些评论,你认为还有更多的解释空间吗?如果是,在哪里? – jonahe

+1

非常感谢您的建议,我用您的代码更新了我的问题,我试图动态创建一个带有部门对象的数组,然后检查员工,但出于某种原因,我没有获得明确的值我的功能找到了。 – Leff