Javascript将数组元素交换到另一个索引并删除旧索引
是否可以将数组元素包含的对象的引用交换到另一个索引并删除/删除旧索引。
例子: 我有一个带有索引 A、B、C、D 的 OBJECTS 数组。
现在我想创建一个带有索引 F 的新数组元素,并为其分配索引 B 包含的对象的引用。之后,我想从数组中删除 B,以便只剩下 A、C、D、E。
我基本上想做的是复制索引 B 包含的对象的引用并将其复制到 E。
我已经尝试了下面的代码,但它不起作用:
this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);
// I want to pass the reference B contains
// and assign it to F (like something you can do in C++)
this.cache['E'] = this.cache['B'];
// First attempt
// length of array is 5 (cache['B'] just has value of undefined)
delete this.cache['B'];
// Second attempt
// the reference to object Orange was deleted in both B and E
this.cache.splice('B', 1);
我不想创建一个新对象并重新分配值,因为有很多对象的引用和绑定,所以做深拷贝是没有意义的。
Is it possible to swap a reference of an object that an array element contains to another index and remove/delete the old index.
Example:
I have an array of OBJECTS with indexes A, B, C, D.
Now I want to create a new array element with index F and assign it the reference of an object that index B contains. After that, I want to remove B from the array so that only A, C, D, E remains.
What I basically want to do is copy the reference of an object that index B contains and copy it to E.
I have tried the code below but it doesn't work:
this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);
// I want to pass the reference B contains
// and assign it to F (like something you can do in C++)
this.cache['E'] = this.cache['B'];
// First attempt
// length of array is 5 (cache['B'] just has value of undefined)
delete this.cache['B'];
// Second attempt
// the reference to object Orange was deleted in both B and E
this.cache.splice('B', 1);
I don't want to create a new object and reassign the values, because there are many references and bindings to the objects, so doing a deep copy would be meaningless.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想知道为什么“拼接”方法不是解决方案。
看看我的示例代码。我在那里使用拼接,没有任何参考丢失......
I'm wondering why the "splice" method is not the solution.
Look at my sample code. I'm using splice there and no any references is lost...
要取消设置对象属性,您可以使用“删除”运算符。例如:
要解决您的整体问题,您可以使用类似的东西。
To unset an object property you can use the "delete" operator. For example:
To resolve your overall problem you could use something similar to.
您确定 E 不再是对象引用吗?
这段代码:
现在cache['E']仍然包含分配给cache['B']的对象。
Are you sure that the object reference is no more with E?
This code:
Now cache['E'] still contains the object, that was assigned to cache['B'].
以下内容是否足够?
结果:acdb
看看这篇文章
Is the following sufficient?
Result: acdb
Take a look at this post
我希望您使用的是对象而不是数组。 Javascript 中的对象没有
length
属性。作为数组。这里即使它被声明为数组,值也不会添加到数组中(就像在 php 等中一样)。而是作为属性添加。所以长度将始终为零。
正如您在上面的示例中看到的,
this.cache
没有用作数组。所以你可以创建和反对。在这两种情况下,
hasOwnProperty('B')
都会返回 false。如果对象具有属性B
,则hasOwnProperty
将返回 true。你的第一个方法应该有效。http://jsfiddle.net/diode/hfLJz/1/
。
I hope you are using object not array. There is no property
length
for object in Javascript.As array. Here even if it is declared as array, values are not added to the array ( like in php etc. ) . Instead added as properties. So length will be always zero.
As you can see in the above example
this.cache
is not used as an array. So you can create and object.In both cases
hasOwnProperty('B')
will return false. If the object has the propertyB
,hasOwnProperty
will return true. Your first method should work.http://jsfiddle.net/diode/hfLJz/1/
.