gpt4 book ai didi

使用删除时的javascript奇怪

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:04 24 4
gpt4 key购买 nike

所以...

var outObj = people[0];
outObj.oAuthID = null;
delete outObj.oAuthID;

给我...

{
"uuid": "39b2b45f-1dde-4c9a-8765-1bc76f55848f",
"oAuthID": null,
"date": "2013-10-21T16:48:47.079Z",
"updated": "2013-10-21T16:48:47.079Z",
"id": "52655aefcc81bb9adc000001"
}

但是这...

function clone(obj) {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;

// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}

// Handle Array
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}

// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}

throw new Error("Unable to copy obj! Its type isn't supported.");
}

var outObj = clone(people[0]);
outObj.oAuthID = null;
delete outObj.oAuthID;

给我...

{
"uuid": "39b2b45f-1dde-4c9a-8765-1bc76f55848f",
"date": "2013-10-21T16:48:47.079Z",
"updated": "2013-10-21T16:48:47.079Z",
"id": "52655aefcc81bb9adc000001"
}

真的不想每次都克隆所有内容只是为了在我的结果中隐藏一个属性。这是怎么回事?这是怎么回事?我怎样才能修复它以“正常”工作?

最佳答案

我很想知道您是否在多个浏览器中测试过这段代码,因为它们给出了与 delete 运算符不一致的行为。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Cross-browser_issues

尝试的一件事是删除属性,就好像对象是一个数组一样。

删除 outObj['oAuthID'];

应该提供更一致的行为。

关于使用删除时的javascript奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19524219/

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