javascript React本地SectionList数据,而不是数组,而是对象{ items:[..]}代替

enyaitl3  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(73)

我在我的React Native上使用节列表,问题是节列表接收这种数据。

const DATA = [
            {
                date: "Main dishes",
                data: ["Pizza", "Burger", "Risotto"],
            },
            {
                date: "Sides",
                data: ["French Fries", "Onion Rings", "Fried Shrimps"],
            },
            {
                date: "Drinks",
                data: ["Water", "Coke", "Beer"],
            },
            {
                date: "Desserts",
                data: ["Cheese Cake", "Ice Cream"],
            },
        ];

但是我的API只能发送这种数据

const DATA = [
    {
        date: "Main dishes",
        data: { 
                items: ["Pizza", "Burger", "Risotto"]
            },
    },
    {
        date: "Sides",
        data: { 
                items: ["French Fries", "Onion Rings", "Fried Shrimps"]
            },,
    },
];

问题是,我的节列表不适用于此,因为数据不是数组,而是对象。
有人知道我该怎么做吗?
非常感谢你的帮助!!

ix0qys7i

ix0qys7i1#

您可以尝试将接收到的数据序列化为:

const serializedData = DATA.map(item => ({...item, data: item?.data?.items}))

相关问题