如何用JS提取嵌套数组中的特定对象?
how can I extract specific objects in nested array with JS?
arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
我想创建一个仅包含 arr
、name
和 age
的新数组。
我尝试使用 map() 来实现,但我不知道如何使用它。
是否可以通过使用一次 map() 来完成,还是我必须使用除 map() 之外的其他方法?
你可以这样做
const arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
const result = arr.map(({_id, name, age}) => ({_id, name, age}))
console.log(result)
你有多种方法可以做到。
Here is the documentation of map .
你可以像这样实现映射方法:
const yourNewArray = arr.map((item)=>{
return {
id:item._id,
name:item.name,
age:item.age
}
})
You can make this with every loop, but the pros of using a map here are
because the map returns always the element if you want to implement
with normal for loop or forEach loop, you will have to add each
element in the array with .push
如果您只想 return 数组中的一些值,那么您可以使用 .filter()
方法
是的,您可以使用 Array.prototype.map():
const arr = [{_id: '6271fd40fe5c58d4cbd3a72f',index: 0,guid: '60f843d2-1b6b-4536-b421-e5df1c4ab735',isActive: true,balance: ',534.11',picture: 'http://placehold.it/32x32',age: 27,eyeColor: 'brown',name: 'Miranda Nguyen',gender: 'male',company: 'ZENTIA',email: 'mirandanguyen@zentia.com',phone: '+1 (833) 466-3207',address: '198 Dank Court, Hartsville/Hartley, Indiana, 8416',about: 'Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n',registered: '2021-06-16T11:50:54 -09:00',},{_id: '6271fd40e30856d15651c60c',index: 1,guid: 'ee647592-0647-4ad5-96b1-eb6c06a32dbd',isActive: true,balance: ',636.22',picture: 'http://placehold.it/32x32',age: 35,eyeColor: 'blue',name: 'Florence Flynn',gender: 'female',company: 'ELENTRIX',email: 'florenceflynn@elentrix.com',phone: '+1 (901) 525-3731',address: '246 Billings Place, Brandermill, Guam, 5037',about: 'Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n',registered: '2014-12-10T08:44:47 -09:00',},{_id: '6271fd404394e16d79143873',index: 2,guid: '3d3890ff-8241-4a2b-a361-789e0298817a',isActive: false,balance: ',047.32',picture: 'http://placehold.it/32x32',age: 25,eyeColor: 'green',name: 'Debbie Pratt',gender: 'female',company: 'OPTIQUE',email: 'debbiepratt@optique.com',phone: '+1 (831) 537-3188',address: '737 Quentin Street, Calpine, Louisiana, 9102',about: 'Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n',registered: '2016-09-19T10:00:38 -09:00',},]
const result = arr.map(({ _id, name, age }) => ({ id: _id, name, age }))
console.log(result)
arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
// long hand
let result = arr.map(function(obj) {
return {
_id: obj._id,
name: obj.name,
age: obj.age
}
})
console.log(result)
// shorthand, requires support for object destructuring, arrow functions
result = arr.map(({ _id, name, age }) => ({ _id, name, age }))
console.log(result)
arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
我想创建一个仅包含 arr
、name
和 age
的新数组。
我尝试使用 map() 来实现,但我不知道如何使用它。 是否可以通过使用一次 map() 来完成,还是我必须使用除 map() 之外的其他方法?
你可以这样做
const arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
const result = arr.map(({_id, name, age}) => ({_id, name, age}))
console.log(result)
你有多种方法可以做到。
Here is the documentation of map .
你可以像这样实现映射方法:
const yourNewArray = arr.map((item)=>{
return {
id:item._id,
name:item.name,
age:item.age
}
})
You can make this with every loop, but the pros of using a map here are because the map returns always the element if you want to implement with normal for loop or forEach loop, you will have to add each element in the array with
.push
如果您只想 return 数组中的一些值,那么您可以使用 .filter()
方法
是的,您可以使用 Array.prototype.map():
const arr = [{_id: '6271fd40fe5c58d4cbd3a72f',index: 0,guid: '60f843d2-1b6b-4536-b421-e5df1c4ab735',isActive: true,balance: ',534.11',picture: 'http://placehold.it/32x32',age: 27,eyeColor: 'brown',name: 'Miranda Nguyen',gender: 'male',company: 'ZENTIA',email: 'mirandanguyen@zentia.com',phone: '+1 (833) 466-3207',address: '198 Dank Court, Hartsville/Hartley, Indiana, 8416',about: 'Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n',registered: '2021-06-16T11:50:54 -09:00',},{_id: '6271fd40e30856d15651c60c',index: 1,guid: 'ee647592-0647-4ad5-96b1-eb6c06a32dbd',isActive: true,balance: ',636.22',picture: 'http://placehold.it/32x32',age: 35,eyeColor: 'blue',name: 'Florence Flynn',gender: 'female',company: 'ELENTRIX',email: 'florenceflynn@elentrix.com',phone: '+1 (901) 525-3731',address: '246 Billings Place, Brandermill, Guam, 5037',about: 'Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n',registered: '2014-12-10T08:44:47 -09:00',},{_id: '6271fd404394e16d79143873',index: 2,guid: '3d3890ff-8241-4a2b-a361-789e0298817a',isActive: false,balance: ',047.32',picture: 'http://placehold.it/32x32',age: 25,eyeColor: 'green',name: 'Debbie Pratt',gender: 'female',company: 'OPTIQUE',email: 'debbiepratt@optique.com',phone: '+1 (831) 537-3188',address: '737 Quentin Street, Calpine, Louisiana, 9102',about: 'Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n',registered: '2016-09-19T10:00:38 -09:00',},]
const result = arr.map(({ _id, name, age }) => ({ id: _id, name, age }))
console.log(result)
arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": ",534.11",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "brown",
"name": "Miranda Nguyen",
"gender": "male",
"company": "ZENTIA",
"email": "mirandanguyen@zentia.com",
"phone": "+1 (833) 466-3207",
"address": "198 Dank Court, Hartsville/Hartley, Indiana, 8416",
"about": "Consectetur proident anim do commodo ipsum. Elit consectetur irure dolore voluptate. Enim aute voluptate et qui do. Eu pariatur laboris labore esse enim reprehenderit mollit ullamco fugiat. Laborum sunt veniam consectetur laboris cupidatat. Lorem tempor occaecat labore ut et eiusmod amet.\r\n",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": ",636.22",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": "Florence Flynn",
"gender": "female",
"company": "ELENTRIX",
"email": "florenceflynn@elentrix.com",
"phone": "+1 (901) 525-3731",
"address": "246 Billings Place, Brandermill, Guam, 5037",
"about": "Sint non dolore cupidatat voluptate laboris adipisicing eu quis. Laboris nostrud qui dolor cillum dolor nulla sint culpa est reprehenderit sint ipsum nisi excepteur. Qui cupidatat sint do aliquip ut. Officia est aliquip fugiat ex enim do sunt consequat.\r\n",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": ",047.32",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "green",
"name": "Debbie Pratt",
"gender": "female",
"company": "OPTIQUE",
"email": "debbiepratt@optique.com",
"phone": "+1 (831) 537-3188",
"address": "737 Quentin Street, Calpine, Louisiana, 9102",
"about": "Officia nisi labore mollit cupidatat exercitation commodo duis adipisicing officia amet laborum. Nulla qui commodo aliqua reprehenderit dolore cupidatat esse ea pariatur sint ad dolore. Exercitation duis veniam velit occaecat est irure quis labore occaecat.\r\n",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
// long hand
let result = arr.map(function(obj) {
return {
_id: obj._id,
name: obj.name,
age: obj.age
}
})
console.log(result)
// shorthand, requires support for object destructuring, arrow functions
result = arr.map(({ _id, name, age }) => ({ _id, name, age }))
console.log(result)