简体   繁体   English

为什么我们不能使用 push 方法来迭代 javascript 数组?

[英]Why can't we use push method to iterate a javascript array?

I'm trying to use push method to add this object arrayObject to newArrayCheck but the values property comes out as [[array] [array]] in instead of [[1,2],[5,6]].我正在尝试使用 push 方法将此 object arrayObject 添加到 newArrayCheck 但值属性显示为 [[array] [array]] 而不是 [[1,2],[5,6]]。 How can I use push method to add the correct value for the value property which is [[1,2],[5,6]].如何使用 push 方法为 [[1,2],[5,6]] 的 value 属性添加正确的值。

arrayObject =[ {
    obj : "King",
    values : [[1,2],[5,6]]
}];

newArrayCheck = []
for (let i of arrayObject){
    newArrayCheck.push(i);
};
console.log(newArrayCheck);

Out Put:输出: 在此处输入图像描述

The implementation is correct and you are allowed to do so.实施是正确的,您可以这样做。

You can stringify the array for quick view by console.log(JSON.stringify(newArrayCheck[0].values));您可以通过 console.log(JSON.stringify(newArrayCheck[0].values)); 对数组进行字符串化以便快速查看

You will get the values if you iterate over the values property.如果您遍历values属性,您将获得这些值。

If you are trying to access the value use:如果您尝试访问该值,请使用:

for (let obj of newArrayCheck) {
    for (let value of obj.values) {
        console.log(value)
    }
}

You can even use map:您甚至可以使用 map:

newArrayCheck.map(obj => {
    obj.values.map(value => {
        console.log(value)
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM