簡體   English   中英

如何讓數組不再是 object?

[英]How to get an array to stop being an object?

我有一個數組字段,在我的產品集合中稱為變體。 但是當我控制台記錄它時,它說它是 object,所以現在我無法 map 它。

這就是firestore上的樣子

這是產品對象的輸出,以及 product.variation “數組”

我所做的是 map 使用 useEffect 獲取我的產品詳細信息的文檔

useEffect(() => {
        const getProducts = async () => {
            const q = query(
                collection(db, "Products"),
                where("slug", "==", router.query.slug)
            );
            const querySnapshot = await getDocs(q);
            querySnapshot.docs.map((doc) => setProduct(doc.data()));
                    
        };
        getProducts();
    }, [router.query.slug]);
//Lets say we have an object 
const obj1 = {calories: 300, price: 800, size: 'medium'};
const obj2 = {calories: 500, price: 1300, size: 'large'};

//Since you want to have two objects in your array, spread the two objects
const arr = [...obj1, ...obj2];
//arr becomes an array which you can now map

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM