gpt4 book ai didi

javascript - 使用 JSON.parse 和 JSON.stringify 克隆 JS 数组后,数组 "undefined"值转换为 "null"?

转载 作者:行者123 更新时间:2023-12-04 09:05:47 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





JSON undefined value type

(2 个回答)


去年关闭。



var arr1 = [1, 2, 3, undefined, 4, 5];
var arr2 = JSON.parse(JSON.stringify(arr1));
console.log(arr2);
上面代码的输出将是......
[1, 2, 3, null, 4, 5]
为什么它将它的第四个值从“未定义”转换为“空”?

最佳答案

因为 undefined是一种 Javascript 类型。没有undefined在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com