var arr1 = [1, 2, 3, undefined, 4, 5];
var arr2 = JSON.parse(JSON.stringify(arr1));
console.log(arr2);
上述代码的输出将是......
[1, 2, 3, null, 4, 5]
为什么它将第四个值从“未定义”转换为“null”?
最佳答案
因为undefined
是一种Javascript类型。 JSON 中没有 未定义
。
根据the spec :
JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
关于javascript - 使用 JSON.parse 和 JSON.stringify 克隆 JS 数组后,为什么数组的 "undefined"值转换为 "null"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63451729/