[英]How to create new array of objects from main one
I have array of objects and from that I am filtering on the basis of age and want the new array of object to not to have age我有对象数组,从中我根据年龄进行过滤,并希望新的对象数组没有年龄
Like this像这样
I have this array of oject我有这个对象数组
let arrayObj =[
{ name : "Joe", age:20, email: "joe@hotmail.com"},
{ name : "Mike", age:50, email: "mike@hotmail.com"},
{ name : "Joe", age:45, email: "mike@hotmail.com"}
]
now using filter
I am filtering the array with age like below现在使用
filter
我正在过滤具有年龄的数组,如下所示
let newArray = arrayObj.filter((el)=>{
if(el.age>20){
return el.name,el.email
}
})
console.log(newArray)
the console is always showing every thing like this控制台总是显示这样的一切
[{
age: 50,
email: "mike@hotmail.com",
name: "Mike"
},{
age: 45,
email: "mike@hotmail.com",
name: "Joe"
}]
But I want但我想要
[{
email: "mike@hotmail.com",
name: "Mike"
},{
email: "mike@hotmail.com",
name: "Joe"
}]
I don't know what to do next to achieve this我不知道下一步该怎么做才能实现这一目标
You want to remove the age property after filtering.您想在过滤后删除年龄属性。 You can use
Array.prototype.map
.您可以使用
Array.prototype.map
。
let arrayObj =[ { name : "Joe", age:20, email: "joe@hotmail.com"}, { name : "Mike", age:50, email: "mike@hotmail.com"}, { name : "Joe", age:45, email: "mike@hotmail.com"} ] let newArray = arrayObj.filter(e => e.age > 20).map(e => { return { name: e.name, email: e.email } }); console.log(newArray);
You need to use map instea of filter您需要使用 map instea of filter
let arrayObj = [ { name: "Joe", age: 20, email: "joe@hotmail.com" }, { name: "Mike", age: 50, email: "mike@hotmail.com" }, { name: "Joe", age: 45, email: "mike@hotmail.com" } ]; const newArray = arrayObj.filter(el => el.age > 20).map(el => { return {"name": el.name, "email": el.email}; }); console.log(newArray);
You can do something like this with only filter
你可以只
filter
做这样的事情
let arrayObj =[ { name : "Joe", age:20, email: "joe@hotmail.com"}, { name : "Mike", age:50, email: "mike@hotmail.com"}, { name : "Joe", age:45, email: "mike@hotmail.com"} ]; let newArray = []; arrayObj.filter(e => { if(e.age > 20){ newArray.push({ name: e.name, email: e.email }); } }); console.log(newArray);
You can try below你可以试试下面
let arrayObj =[ { name : "Joe", age:20, email: "joe@hotmail.com"}, { name : "Mike", age:50, email: "mike@hotmail.com"}, { name : "Joe", age:45, email: "mike@hotmail.com"} ] var result = arrayObj .filter(x => x.age > 20) //Filter by age .map(y => {return {name: y.name, email: y.email};}) //Map only required properties console.log(result)
One way to do with filter
, map
methods and object destructuring to make code more readable.一种使用
filter
、 map
方法和对象解构使代码更具可读性的方法。 Alternate way to do is using just one method reduce
.另一种方法是仅使用一种方法
reduce
。
let arrayObj = [ { name: "Joe", age: 20, email: "joe@hotmail.com" }, { name: "Mike", age: 50, email: "mike@hotmail.com" }, { name: "Joe", age: 45, email: "mike@hotmail.com" } ]; const res = arrayObj .filter(({ age }) => age > 20) .map(({ name, email }) => ({ name, email })); console.log(res); // Alternate way to do just with one method reduce const res2 = arrayObj.reduce( (acc, { name, email, age }) => age > 20 ? [...acc, { name, email }] : [...acc], [] ); console.log(res2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.