如何对这个对象数组进行解构
How to make a destructuring of this array of objects
我正在尝试对 subItems
数组的元素使用解构来获取直接引用 links
和 open
属性的变量。
具体来说,我正在尝试使用以下代码,但如评论中所指定的那样失败了。
const [{links, open}] = subItems.map(({ items }) => {
document.getElementById(items);
}); // Fails: returns nothing
const {links, open} = subItems((items ) => {
return items;
}); // Fails: returns only the first `link` and `open`
// This is what the `subItems` array looks like:
export const subItems = [
{
links: [
{ title: 'ret', to: '#' },
{ title: 'def', to: '#' },
{ title: 'rev', to: '#' },
{ title: 'red', to: '#' },
{ title: 'im', to: '#' },
{ title: 'glossar', to: '#' }
],
open: true
},
{
links: [
{ title: 'recloud', to: '#' },
{ title: 'broker', to: '#' },
{ title: 'mercury', to: '#' },
{ title: 'investor', to: '#' }
],
open: false
}
];
P.S。我是 JS 的新手,如果我误解了一些琐碎的事情,抱歉。
这是否完成了您想要做的事情?:
// Gets array (but you can get it any way you want, maybe by `import`)
const subItems = getSubItems();
// Loops through array (using `subItem` as the name for each array element)
for(subItem of subItems){
// Destructures the current element in array (using `{...}` for objects)
const {links, open} = subItem;
// Collects links info (This is not needed -- just makes nicer printing)
const linkString = links.reduce(
(linkStr, link) => linkStr + `title:${link.title}, to:${link.to}; `,
""
);
// Does something with links and open (We could print `links` instead of linkString)
console.log("links: ", linkString, "\nopen: ", open, "\n\n");
}
function getSubItems(){
return [
{
links: [
{ title: 'ret', to: '#' },
{ title: 'def', to: '#' },
{ title: 'rev', to: '#' },
{ title: 'red', to: '#' },
{ title: 'im', to: '#' },
{ title: 'glossar', to: '#' }
],
open: true
},
{
links: [
{ title: 'recloud', to: '#' },
{ title: 'broker', to: '#' },
{ title: 'mercury', to: '#' },
{ title: 'investor', to: '#' }
],
open: false
}
];
}
我正在尝试对 subItems
数组的元素使用解构来获取直接引用 links
和 open
属性的变量。
具体来说,我正在尝试使用以下代码,但如评论中所指定的那样失败了。
const [{links, open}] = subItems.map(({ items }) => {
document.getElementById(items);
}); // Fails: returns nothing
const {links, open} = subItems((items ) => {
return items;
}); // Fails: returns only the first `link` and `open`
// This is what the `subItems` array looks like:
export const subItems = [
{
links: [
{ title: 'ret', to: '#' },
{ title: 'def', to: '#' },
{ title: 'rev', to: '#' },
{ title: 'red', to: '#' },
{ title: 'im', to: '#' },
{ title: 'glossar', to: '#' }
],
open: true
},
{
links: [
{ title: 'recloud', to: '#' },
{ title: 'broker', to: '#' },
{ title: 'mercury', to: '#' },
{ title: 'investor', to: '#' }
],
open: false
}
];
P.S。我是 JS 的新手,如果我误解了一些琐碎的事情,抱歉。
这是否完成了您想要做的事情?:
// Gets array (but you can get it any way you want, maybe by `import`)
const subItems = getSubItems();
// Loops through array (using `subItem` as the name for each array element)
for(subItem of subItems){
// Destructures the current element in array (using `{...}` for objects)
const {links, open} = subItem;
// Collects links info (This is not needed -- just makes nicer printing)
const linkString = links.reduce(
(linkStr, link) => linkStr + `title:${link.title}, to:${link.to}; `,
""
);
// Does something with links and open (We could print `links` instead of linkString)
console.log("links: ", linkString, "\nopen: ", open, "\n\n");
}
function getSubItems(){
return [
{
links: [
{ title: 'ret', to: '#' },
{ title: 'def', to: '#' },
{ title: 'rev', to: '#' },
{ title: 'red', to: '#' },
{ title: 'im', to: '#' },
{ title: 'glossar', to: '#' }
],
open: true
},
{
links: [
{ title: 'recloud', to: '#' },
{ title: 'broker', to: '#' },
{ title: 'mercury', to: '#' },
{ title: 'investor', to: '#' }
],
open: false
}
];
}