功能 Javascript:在遍历具有嵌套对象和数组的对象时遇到问题
Functional Javascript: Having trouble iterating through an object that has nested objects and arrays
我在尝试访问结构如下的 table 中的数据时遇到困难。我想要一种通过功能性 Javascript 方法访问嵌套数据的简洁高效的方法。如果有人可以展示如何使用 Ramda 或纯 ES6 在功能上完成此操作,将不胜感激!请参阅下面的示例 table 结构。
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,我的最终目标是在 Table 组件中构建每个 table,因此我希望能够在组件中实现类似下面的内容
const headings = [ 'hey', 'there' ]
const rows = [ 'row 1 here', 'row 2 here' ]
for (var key in tables ) {
for (var innerKey tables[key] ) {
for (var i = 0; i < tables[key][innerKey].length; i++) {
console.log(tables[key][innerKey][i]);
}
}
}
有很多关于循环对象和数组的例子
如果你问的是如何枚举数据结构,像这样的东西会起作用:
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
});
});
如果您想做的是转换数据(而不是对其进行一些 side-effects 处理),那么 Ramda 确实有一些工具可以使它变得更容易,尤其是 evolve
。
如果您有一个要用于 body 元素的函数,例如:
const surround = (tag) => (content) => `<${tag}>${content}</${tag}>`;
并且您想 upper-case headers,您可以使用类似这样的东西
R.map(R.evolve({
headings: R.map(R.toUpper),
rows: R.map(surround('td'))
}))(tables);
您可以在 Ramda REPL.
上看到这一点。
我在尝试访问结构如下的 table 中的数据时遇到困难。我想要一种通过功能性 Javascript 方法访问嵌套数据的简洁高效的方法。如果有人可以展示如何使用 Ramda 或纯 ES6 在功能上完成此操作,将不胜感激!请参阅下面的示例 table 结构。
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,我的最终目标是在 Table 组件中构建每个 table,因此我希望能够在组件中实现类似下面的内容
const headings = [ 'hey', 'there' ]
const rows = [ 'row 1 here', 'row 2 here' ]
for (var key in tables ) {
for (var innerKey tables[key] ) {
for (var i = 0; i < tables[key][innerKey].length; i++) {
console.log(tables[key][innerKey][i]);
}
}
}
有很多关于循环对象和数组的例子
如果你问的是如何枚举数据结构,像这样的东西会起作用:
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
});
});
如果您想做的是转换数据(而不是对其进行一些 side-effects 处理),那么 Ramda 确实有一些工具可以使它变得更容易,尤其是 evolve
。
如果您有一个要用于 body 元素的函数,例如:
const surround = (tag) => (content) => `<${tag}>${content}</${tag}>`;
并且您想 upper-case headers,您可以使用类似这样的东西
R.map(R.evolve({
headings: R.map(R.toUpper),
rows: R.map(surround('td'))
}))(tables);
您可以在 Ramda REPL.
上看到这一点。