[英]Destructuring array inside object inside array in one go
Is there a way to destructure the "First shop" value in one go from this array?有没有办法从这个数组中解构一个 go中的“第一家商店”值?
const myArr = [
{
name: 'John',
age: '28',
hobbies: ['coding', 'chess', 'cards', 'reading'],
address: {
city: 'Some city',
province: 'Some province',
country: 'Some country',
},
places: {
coffeeShops: ['First shop', 'Second shop', 'Third shop'],
},
},
];
Extracting the object from the array first actually works but I want to destructure from the array itself.首先从数组中提取 object 实际上可行,但我想从数组本身中解构。
This works:这有效:
const obj = myArr[0];
const {
places: {
coffeeShops: [first],
},
} = obj;
console.log(first);
But I can't seem to do it when the object is inside the array.但是当 object 在阵列内时,我似乎无法做到这一点。 Is that possible in JS?
这在JS中可能吗?
Putting the object in square brackets works:将 object 放在方括号中有效:
const myArr = [ { name: 'John', age: '28', hobbies: ['coding', 'chess', 'cards', 'reading'], address: { city: 'Some city', province: 'Some province', country: 'Some country', }, places: { coffeeShops: ['First shop', 'Second shop', 'Third shop'], }, }, ]; const [ { places: { coffeeShops: [first], }, } ] = myArr; console.log(first);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.