0

我有一些看起来像这样的 JSON。

[
{
    "items": [
        {
            "id": "xxxx",
            "description": {
                "style": "",
                "specs": ""
            }
        },
        {
            "id": "xxxx",
            "description": {
                "style": "",
                "specs": ""
            }
        }
    ],
    "category": "xxxxxx",
    "name": "xxxxxx"
},
{
    "items": [
        {
            "id": "xxxx",
            "description": {
                "style": "",
                "specs": ""
            }
        },
        {
            "id": "xxxx",
            "description": {
                "style": "",
                "specs": ""
            }
        }
    ],
    "category": "xxxxxx",
    "name": "xxxxxx"
}
]

它作为“newItem”传递给我。并且在我正在处理的现有代码中设置了两次:

that.cart.addItem(newItem.toObject());

然后:

that.origCart.replaceWith(that.cart);

当“项目”是一个平面列表时,这传统上工作得很好。但是,上面的 JSON 是新的 JSON 格式,因此我需要为每个类别/名称传递“项目”。

在“for”循环中,我有这个输出我需要的每个。(我需要将它们分开。)

newItem.items

但是,当我尝试执行以下操作时,会出现类型错误。(是的,一旦我开始工作,我将拥有多个“推车”。)

that.cart.addItem(newItem.items.toObject());

TypeError: newItem.items.toObject is not a function

我该怎么做才能将“newItem.items”设置为正确的类型,或者我可以用什么操作来替代这些设置?我尝试了一堆我在网上找到的东西但没有成功,现在我正在求助于专家。只是不是我的强项(还)。

4

1 回答 1

1

看起来您正在尝试从 newItems 对象调用 items 函数,即使 items 是一个数组。尝试以下操作:

that.cart.addItem(newItem.items);
于 2013-06-28T23:56:47.030 回答