繁体   English   中英

删除元素并更改对象数组中的元素属性

[英]Deleting element and changing element property in an Array of objects

因此,有一个名为 employees 的 object 数组具有员工的凭据。 如果员工的名字是 Theo,我应该删除 object,如果员工的名字是 Lorie,我应该将部门的属性更改为“HR”特性。 但它不会返回迭代数组。 也许我需要使用 .reduce() 方法这是我尝试过的代码

在此处输入图像描述

它应该工作!

你的对象数组。

var employees = [
 {
  "firstName": 'Von',
  "lastName": 'budibent',
  "email": "email@mail.com",
  "departement": "Sales"
 },
 {
  "firstName": 'Theo',
  "lastName": 'Trill',
  "email": "email@mail.com",
  "departement": "Services"
 },
 {
  "firstName": 'Lorie',
  "lastName": 'Trill',
  "email": "email@mail.com",
  "departement": "Research and Development"
 }
];

然后您需要按firstName过滤,然后映射数据。

var adjustedEmployees = employees
 .filter(employee => employee.firstName !== 'Theo')
 .map((employee) => {
   if (employee.firstName === 'Lorie') employee.departement = 'HR';
   return employee;
 });

//我需要使用没有参数的function,我想出了怎么做。

已解决的问题

//Remove employe with firstname 'Theo'
var result = employees.filter(employee => employee.firstName !== 'Theo');
//Change department of Lorie to 'HR'
result.forEach(el => {
  el.departement = el.firstName === 'Lorie' ? 'HR' : el.departement
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM