我如何提取特定的对象嵌套数组与JS?


arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": "$1,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.rn",
"registered": "2016-09-19T10:00:38 -09:00"
}
]

我想创建一个新的数组,仅包含arr中的id,nameage

我试着用map()来做,但我不知道如何使用好它。是否可以通过使用map()一次来做到这一点,或者我必须使用除map()之外的其他方法?

您有多种方法可以做到。

这是map的文档

可以这样实现map方法:


const yourNewArray = arr.map((item)=>{
return {
id:item._id,
name:item.name,
age:item.age
}
})

您可以在每个循环中都这样做,但在这里使用地图的优点是因为如果要实现,映射总是返回元素对于普通的for循环或forEach循环,您必须添加每个循环数组中.push

的元素

如果你想从数组中返回一些值那么你有.filter()方法

arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": "$1,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.rn",
"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)

你可以这样做

const arr = [
{
"_id": "6271fd40fe5c58d4cbd3a72f",
"index": 0,
"guid": "60f843d2-1b6b-4536-b421-e5df1c4ab735",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2021-06-16T11:50:54 -09:00"
},
{
"_id": "6271fd40e30856d15651c60c",
"index": 1,
"guid": "ee647592-0647-4ad5-96b1-eb6c06a32dbd",
"isActive": true,
"balance": "$2,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.rn",
"registered": "2014-12-10T08:44:47 -09:00"
},
{
"_id": "6271fd404394e16d79143873",
"index": 2,
"guid": "3d3890ff-8241-4a2b-a361-789e0298817a",
"isActive": false,
"balance": "$1,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.rn",
"registered": "2016-09-19T10:00:38 -09:00"
}
]
const result = arr.map(({_id, name, age}) => ({_id, name, age}))
console.log(result)

是的,你可以使用Array.prototype.map():

const arr = [{_id: '6271fd40fe5c58d4cbd3a72f',index: 0,guid: '60f843d2-1b6b-4536-b421-e5df1c4ab735',isActive: true,balance: '$2,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.rn',registered: '2021-06-16T11:50:54 -09:00',},{_id: '6271fd40e30856d15651c60c',index: 1,guid: 'ee647592-0647-4ad5-96b1-eb6c06a32dbd',isActive: true,balance: '$2,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.rn',registered: '2014-12-10T08:44:47 -09:00',},{_id: '6271fd404394e16d79143873',index: 2,guid: '3d3890ff-8241-4a2b-a361-789e0298817a',isActive: false,balance: '$1,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.rn',registered: '2016-09-19T10:00:38 -09:00',},]
const result = arr.map(({ _id, name, age }) => ({ id: _id, name, age }))
console.log(result)

相关内容

  • 没有找到相关文章

最新更新