根据 javascript 中的键名过滤对象数组
filter the array of object on the basis of key name in javascript
我有如下对象数组
employees = [
{name: "Tony Stark", department: "IT"},
{name: "Peter Parker", department: "Pizza Delivery"},
{name: "Bruce Wayne", department: "IT"},
{name: "Clark Kent", department: "Editing"}
];
我想像
一样过滤对象数组
employees = [
{name: "Tony Stark"},
{name: "Peter Parker"},
{name: "Bruce Wayne"},
{name: "Clark Kent"}
];
你可以使用 map()
const employees = [
{ name: "Tony Stark", department: "IT" },
{ name: "Peter Parker", department: "Pizza Delivery" },
{ name: "Bruce Wayne", department: "IT" },
{ name: "Clark Kent", department: "Editing" }
];
const result = employees.map(({ name }) => ({ name }));
console.log(result);
const employees = [
{ name: "Tony Stark", department: "IT" },
{ name: "Peter Parker", department: "Pizza Delivery" },
{ name: "Bruce Wayne", department: "IT" },
{ name: "Clark Kent", department: "Editing" }
];
const result = employees.map((employee) => {
return {
name: employee.name
}
});
console.log(result);
我有如下对象数组
employees = [
{name: "Tony Stark", department: "IT"},
{name: "Peter Parker", department: "Pizza Delivery"},
{name: "Bruce Wayne", department: "IT"},
{name: "Clark Kent", department: "Editing"}
];
我想像
一样过滤对象数组 employees = [
{name: "Tony Stark"},
{name: "Peter Parker"},
{name: "Bruce Wayne"},
{name: "Clark Kent"}
];
你可以使用 map()
const employees = [
{ name: "Tony Stark", department: "IT" },
{ name: "Peter Parker", department: "Pizza Delivery" },
{ name: "Bruce Wayne", department: "IT" },
{ name: "Clark Kent", department: "Editing" }
];
const result = employees.map(({ name }) => ({ name }));
console.log(result);
const employees = [
{ name: "Tony Stark", department: "IT" },
{ name: "Peter Parker", department: "Pizza Delivery" },
{ name: "Bruce Wayne", department: "IT" },
{ name: "Clark Kent", department: "Editing" }
];
const result = employees.map((employee) => {
return {
name: employee.name
}
});
console.log(result);