javascript - When a standard array pushes an object, why does it become an array-like object?
阿神
阿神 2017-05-19 10:13:30
0
6
1082

When pushing an object into the array, in principle it is the same json object as I marked in the picture below

But the printed one changes and becomes a Array-like. May I ask what is the reason. Is there any way to solve this problem. Make it a json

阿神
阿神

闭关修行中......

reply all(6)
左手右手慢动作

Use JSON.stringify() to convert again

巴扎黑

If you want to print it out in JSON format, please convert it directly to a JSON string for printing.

var arr = [{a: 1}, {a: 2}];
console.log(JSON.stringify(arr));
刘奇

json definition json

Peter_Zhu

This is not an array-like array. This is an array of array elements 对象.

arr[0] refers to { a: 0 }

As for what is displayed Object 字眼 指代的是 类型

刘奇

There is no problem with this. You put 3 objects in the array. Shouldn’t this array be [对象1,对象2,对象3]?

This is still a standard array. As for the concept of array-like, please visit Baidu.

What you want[{a:0},{a:1},{a:2}] is just the way we use literals to create data and objects. This does not represent its presentation form.

Since the writing method of our literal form is similar to the format of json, it can be converted into a json string to view. This is the format you want.

console.log(JSON.stringify(arr,null,4));
// 第一个参数为你要格式化为字符串的对象,数组也是对象。
// 第二个格式化的处理函数规则
// 第三个格式化是使用多少个空白来美化
// 后两个参数可以省略,就是楼上的形式
// [
//     {
//         "a": 0
//     },
//     {
//         "a": 1
//     },
//     {
//         "a": 2
//     }
// ]

I just read your reply to others. It seems that you don’t understand the difference between json and js objects.
I have written an article before about JavaScript objects and JSON for your reference.

阿神

The json printed by the console is different from the json you wrote. If you look at the json you want to get directly, it is exactly the same as the json you get by pushing.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template