我想在一个对象上映射并传递值作为道具
我在做一个redux项目时,我最初有一个对象数组,因此可以更轻松地对其进行迭代,但是我被要求将我的初始状态转换为js对象,但是我很难重构它 >
// Original State
lists = [
{
id: 0,
title: "List1",
cards: [
{
id: 0,
title: "card1",
user: "ali"
}
]
},
{
id: 1,
title: "List2",
cards: [
{
id: 1,
title: "card1",
user: "ahmed"
},
{
id: 2,
title: "card2",
user: "khalid"
}
]
}
];
// This is where I map over the card and send the values as props
cards.map(card => (
<TodoCard
key={card.id}
title={card.title}
listId={listId}
cardId={card.id}
user={card.user}
/>
));
代码运行良好,但是如果将数组转换为下面给出的js对象,我该如何遍历
lists = [
{
id: 0,
title: "List1",
cards: {
cardDetail: {
id: 0,
title: "card1",
user: "ali"
}
}
},
{
id: 1,
title: "List2",
cards: {
cardDetail: [
{
id: 1,
title: "card1",
user: "ahmed"
},
{
id: 2,
title: "card2",
user: "khalid"
}
]
}
}
];
答案 0 :(得分:0)
您不能那样做。 您要么需要使用
alpha_fun
你不能只是写
list = [{object}, {object}];
//or
list = {myList: [{object}, {object}]}
因为其语法无效。
看看什么是对象以及如何使用对象和数组
答案 1 :(得分:0)