[英]map array inside nested array
I have an array with another nested array I need to map over.我有一个带有另一个嵌套数组的数组,我需要 map 过来。 I think I almost got it but I am not getting that data back, can only see its an object.我想我几乎得到了它,但我没有取回该数据,只能看到它的 object。 Looking at the new index I can see it is looping of the amount of objects in the object array.查看新索引,我可以看到它正在循环 object 数组中的对象数量。
Here is what I have currently:这是我目前拥有的:
class Menu extends Component {
constructor(props) {
super(props);
const arrayItems = [this.props.items.edges]
arrayItems.map((currentItem, index) => {
console.log("The current iteration is: " + index)
console.log("The current item is: " + currentItem)
return currentItem.map((newCurrentItem, newIndex) => {
console.log("The NEW current iteration is: " + newIndex)
console.log("The NEW current item is: " + newCurrentItem)
return newCurrentItem
})
})
...
}
Here is screenshot of what I can see in my console, which looks promising:这是我在控制台中看到的屏幕截图,看起来很有希望:
Can someone please point me in correct direction?有人可以指出我正确的方向吗?
Actually, currentItem
is equal to this.props.items.edges
in your code, so you could just map this.props.items.edges
.实际上, currentItem
在您的代码中等于this.props.items.edges
,因此您可以只使用 map this.props.items.edges
。
newCurrentItem
display as [object Object]
is because What does [object Object] mean? newCurrentItem
显示为[object Object]
是因为 [object Object] 是什么意思? (JavaScript) . (JavaScript) 。
So you could get data out of CurrentItem
as normal.所以你可以像往常一样从CurrentItem
中获取数据。
Your code could be simplified to:您的代码可以简化为:
class Menu extends Component {
constructor(props) {
super(props);
const arrayItems = this.props.items.edges
return arrayItems.map((currentItem) => {
return currentItem.id // or other attributes you what
})
...
}
After I've seen your command with sort items in state alphabetically , I believe map the node value should be what you want.在我看到你的命令在 state 按字母顺序排序项目之后,我相信 map 节点值应该是你想要的。
const newCurrentItem = [{ node: "abc" }, { node: "def" }];
// [Object, Object]
console.log(newCurrentItem);
// ["abc", "def"]
console.log(
newCurrentItem.map((data) => {
return data.node;
})
);
const newCurrentItem2 = [{ node: { aaa: "bbb" } }, { node: { aaa: "yyy" } }];
// [Object, Object]
console.log(newCurrentItem2);
// ["bbb", "yyy"]
console.log(
newCurrentItem2.map((data) => {
return data.node.aaa;
})
);
try arrayItems.forEach(currentItem => { //do something })
You can use .map
too bt in that case you need to extract the value of any property like @eux said尝试arrayItems.forEach(currentItem => { //do something })
你也可以使用.map
在这种情况下你需要提取像@eux说的任何属性的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.