javascript - 无法循环类似数组的对象

标签 javascript vue.js

<分区>

我有一组从服务器接收到的对象产品。

return response()->json(['products' => $products->toArray()]);

这是它的日志:

enter image description here

我需要遍历它来获取 product.attributes 我认为它是一个类似数组的对象,所以我使用 Array.prototype.forEach.call

                this.products.forEach(product => {
                    console.log(product);
                    console.log(product.attributes);

                    Array.prototype.forEach.call(product.attributes, function(child) {
                        // It seems the loop doesn't work, so nothing is printed out.
                        console.log(child);
                    });
                });

但似乎类数组对象上的循环不起作用,所以什么也没有打印出来,甚至我的 product.attributes 也不为空。这是 product.attributes 日志:

enter image description here

最佳答案

products.attributes 不是类似对象的数组,它是对象。

但如果您愿意,您仍然可以重复此操作。您只需要:

Object.entries(product.attribues).forEach(([key, value]) => {  })

关于javascript - 无法循环类似数组的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913847/

相关文章:

javascript - 创建美国谷歌地图,没有标记,但每个州都有自己的标记(边界)

javascript - 如何处理 HTML 表单?

javascript - 如何即时删除 CSS 样式

javascript - Vue JS如何更改所选选择选项上的变量值

node.js - 如何在Windows上后台运行Vue应用程序

javascript - 当表中状态获得批准时更改按钮文本

javascript - 使用 jQuery 或纯 JavaScript 进行淡入淡出

vue.js - 如何使用 vue 和 vue-router 保留路由之间的某些状态

javascript - 如果数据为真,则向用户显示确认消息

javascript - vue导入失败 'marker-animate-unobtrusive'