【问题标题】:How to iterate through an array of object properties, in an array of objects如何在对象数组中遍历对象属性数组
【发布时间】:2017-09-05 17:35:26
【问题描述】:

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

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

【问题讨论】:

  • 你试过什么?您需要使用ObjectArray 方法。
  • @T.J.Crowder 我已经编辑了问题并修正了语法
  • 那是什么类型的语法?您可以使用 console.log 在 json 中输出一个干净的变量表示,如下所示:console.log(JSON.stringify(someVar));
  • 我再次编辑了这个问题,之前我只是从控制台用 chrome 复制它,对此我深表歉意
  • 太棒了!那么你被困在哪里了?你的研究结果是什么?另请参阅:stackoverflow.com/questions/9329446/… 除非我遗漏了什么,否则这只是嵌套循环(您最喜欢的口味来自该答案中提供的菜单)。 (不是我的 dv)

标签: javascript arrays sorting ecmascript-6


【解决方案1】:

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

编辑:您提供的新代码和上下文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'}
    ]
  },
 	{
    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 = 
	departmentToCheck =>
  	  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);

【讨论】:

  • 我不明白拒绝投票的原因。它可以使用更多的解释,但这并不是拒绝它的理由。
  • 谢谢。嗯,这是一个非常快速的否决票(据我所知是即时的)所以我开始想知道当你回答一个负分的问题时它是否是一些自动的否决票? (以前没有任何此类规则的负责人,但它会一些有意义)。顺便提一句。我在代码中添加了一些 cmets。你觉得还有解释的余地​​吗?如果有,在哪里?
  • 非常感谢您的建议,我已经用您的代码更新了我的问题,我正在尝试使用部门对象动态创建一个数组,然后检查员工,但出于某种原因,我找到我的函数时不要得到不同的值。
  • 您忘记从您的found 函数返回。应该是return departments.some(function (el) .... ...。如果您只是对部门名称作为字符串感兴趣,那么您可以直接推送字符串(而不是以字符串作为属性推送新对象)
  • 非常感谢您的回答,这正是我想要的!再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 2018-10-18
  • 1970-01-01
  • 2022-11-17
  • 2017-09-19
相关资源
最近更新 更多