gpt4 book ai didi

javascript - 函数式 JavaScript : Having trouble iterating through an object that has nested objects and arrays

转载 作者:行者123 更新时间:2023-12-03 05:40:54 25 4
gpt4 key购买 nike

我在尝试访问如下结构的表中的数据时遇到困难。我想要一种干净有效的方式通过函数式 Javascript 方法访问嵌套数据。如果有人能展示如何使用 Ramda 或普通 ES6 来实现这一功能,我们将不胜感激!请参阅下面的示例表结构。

<小时/>
let tables = {
table1: {
headings: [ 'hey', 'there' ],
rows: [ 'row 1 here', 'row 2 here' ],
},
table2: {
headings: [ 'sup', 'yall' ],
rows: [ 'row1 3 here', 'row 4 here' ],
},
table3: {
headings: [ 'what', 'up' ],
rows: [ 'row 5 here', 'row 6 here' ],
},
}

编辑

我正在使用 React,我的最终目标是在表组件中构建每个表,所以我希望能够在组件中执行如下操作
const 标题 = [ '嘿', '那里' ]
const rows = [ '此处为第 1 行', '此处为第 2 行' ]

最佳答案

如果您询问如何枚举数据结构,类似这样的事情会起作用:

let tables = {
table1: {
headings: ['hey', 'there'],
rows: ['row 1 here', 'row 2 here'],
},
table2: {
headings: ['sup', 'yall'],
rows: ['row1 3 here', 'row 4 here'],
},
table3: {
headings: ['what', 'up'],
rows: ['row 5 here', 'row 6 here'],
},
};


Object.keys(tables).forEach((tableId) => {

tables[tableId].headings.forEach((heading) => {
// do something with heading
});

tables[tableId].rows.forEach((row) => {
// do something with the row
});
});

关于javascript - 函数式 JavaScript : Having trouble iterating through an object that has nested objects and arrays,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com