This question already has an answer here:
这个问题已经有了答案:
- Why does changing an Array in JavaScript affect copies of the array? 13 answers
- 为什么在JavaScript中修改数组会影响数组的副本?13个答案
I'm little confused with js 'delete' operator.
我对js 'delete'操作符有点困惑。
for example
例如
var data = {
list : [a, b, c]
}
var temp = data;
console.log(temp.list[2]); //Result 'c'
console.log(data.list[2]); //Result 'c'
delete temp.list[2];
console.log(temp.list[2]); //Result undefined
console.log(data.list[2]); //Result undefined why?
in this case, I have just only deleted temp.list[2]
but data.list[2]
shows undefined as well.
在这种情况下,我只删除了temp.list[2]但是删除了数据。list[2]表示未定义。
does 'delete' operator trace array address and delete data in it? is that why data.list[2]
is also undefined? (because data.list[2]
has address, not the actual data itself) I think there is explanation. can you help me?
是否“删除”操作符跟踪数组地址并删除其中的数据?这就是为什么数据。[2]列表也未定义的?(因为数据。清单[2]有地址,不是实际的数据本身)我认为有解释。你能帮我吗?
4 个解决方案
#1
0
reference type
引用类型
var data = [] ==> instance
var temp = data; ==> data reference
get temp ==> return data
// shallow copy !!!
var temp = Object.assign([], data); ===> instance
console.log(temp.list[2]); //Result undefined
console.log(data.list[2]); //Result c
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
#2
2
Check out all the great references others have posted. But for a quick solution use the ES6 spread operator to create a copy.
查看其他人发布的所有重要参考资料。但是为了快速解决问题,使用ES6扩展操作符创建一个副本。
var data = {
list : ["a", "b", "c"]
}
var temp = {...data}; // this is now a copy not a reference
更多关于传播算子
in other words this isnt a delete issue, its a reference vs copy issue
换句话说,这不是一个删除问题,它是一个参考和复制问题。
#3
1
Complex types in JS are passed by reference, unless you explicitly take care of copying them.
JS中的复杂类型通过引用传递,除非您显式地处理它们的复制。
What is happening here is:
这里发生的是:
var data = { // <--\
list : [a, b, c] // <----------------------------------------------
} // <--/ |
var temp = data; // this is NOT a copy, but literally a pointer to -/
Try
试一试
var temp = Object.assign({}, data);
if you want a copy.
如果你想要拷贝的话。
#4
1
Your question touches on several topics:
你的问题涉及几个主题:
First, delete operator removes the given property from the object, but when used to remove an element from an array, the array length is not affected:
首先,delete操作符从对象中删除给定的属性,但是当用于从数组中删除元素时,不影响数组长度:
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
delete trees[3];
alert(trees.length); //shows 5
If this is not what you want, consider using splice method instead. It is really powerful and can be used not only to remove elements.
如果这不是您想要的,可以考虑使用splice方法。它非常强大,不仅可以用来删除元素。
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
trees.splice(3, 1);
alert(trees.length); //shows 4
Second, as everybody has already mentioned, var temp = data;
doesn't create a copy of your object, it's just copies a reference. To make a fully identical shallow clone of your object, you could write:
第二,正如大家已经提到的,var temp = data;不是创建对象的副本,而是复制引用。要对你的对象进行完全相同的浅复制,你可以这样写:
let clone = Object.create(Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj));
This call makes a truly exact copy of obj, including all properties: enumerable and non-enumerable, data properties and setters/getters – everything, and with the right [[Prototype]]
, when Object.assign()
method only copies enumerable and own properties from a source object to a target object
这个调用真正精确地复制了obj,包括所有属性:可枚举和不可枚举,数据属性和setter /getter——所有的东西,以及正确的[Prototype],当object .assign()方法只将可枚举属性从源对象复制到目标对象时
#1
0
reference type
引用类型
var data = [] ==> instance
var temp = data; ==> data reference
get temp ==> return data
// shallow copy !!!
var temp = Object.assign([], data); ===> instance
console.log(temp.list[2]); //Result undefined
console.log(data.list[2]); //Result c
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
#2
2
Check out all the great references others have posted. But for a quick solution use the ES6 spread operator to create a copy.
查看其他人发布的所有重要参考资料。但是为了快速解决问题,使用ES6扩展操作符创建一个副本。
var data = {
list : ["a", "b", "c"]
}
var temp = {...data}; // this is now a copy not a reference
更多关于传播算子
in other words this isnt a delete issue, its a reference vs copy issue
换句话说,这不是一个删除问题,它是一个参考和复制问题。
#3
1
Complex types in JS are passed by reference, unless you explicitly take care of copying them.
JS中的复杂类型通过引用传递,除非您显式地处理它们的复制。
What is happening here is:
这里发生的是:
var data = { // <--\
list : [a, b, c] // <----------------------------------------------
} // <--/ |
var temp = data; // this is NOT a copy, but literally a pointer to -/
Try
试一试
var temp = Object.assign({}, data);
if you want a copy.
如果你想要拷贝的话。
#4
1
Your question touches on several topics:
你的问题涉及几个主题:
First, delete operator removes the given property from the object, but when used to remove an element from an array, the array length is not affected:
首先,delete操作符从对象中删除给定的属性,但是当用于从数组中删除元素时,不影响数组长度:
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
delete trees[3];
alert(trees.length); //shows 5
If this is not what you want, consider using splice method instead. It is really powerful and can be used not only to remove elements.
如果这不是您想要的,可以考虑使用splice方法。它非常强大,不仅可以用来删除元素。
let trees = ['redwood', 'bay', 'cedar', 'oak', 'maple'];
trees.splice(3, 1);
alert(trees.length); //shows 4
Second, as everybody has already mentioned, var temp = data;
doesn't create a copy of your object, it's just copies a reference. To make a fully identical shallow clone of your object, you could write:
第二,正如大家已经提到的,var temp = data;不是创建对象的副本,而是复制引用。要对你的对象进行完全相同的浅复制,你可以这样写:
let clone = Object.create(Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj));
This call makes a truly exact copy of obj, including all properties: enumerable and non-enumerable, data properties and setters/getters – everything, and with the right [[Prototype]]
, when Object.assign()
method only copies enumerable and own properties from a source object to a target object
这个调用真正精确地复制了obj,包括所有属性:可枚举和不可枚举,数据属性和setter /getter——所有的东西,以及正确的[Prototype],当object .assign()方法只将可枚举属性从源对象复制到目标对象时