Javascript将数组元素交换到另一个索引并删除旧索引

发布于 2024-12-29 12:30:51 字数 796 浏览 2 评论 0原文

是否可以将数组元素包含的对象的引用交换到另一个索引并删除/删除旧索引。

例子: 我有一个带有索引 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恏ㄋ傷疤忘ㄋ疼 2025-01-05 12:30:52

我想知道为什么“拼接”方法不是解决方案。

看看我的示例代码。我在那里使用拼接,没有任何参考丢失......

function Oo(s) {
    this._s = s;
}

Oo.prototype.toString = function() {
    return "-" + this._s+"-";
}

var a = [];
a.push(new Oo('x'));
a.push(new Oo('y'));
a.push(new Oo('z'));

a[3] = a[1];
alert(a);  // -x-,-y-,-z-,-y-

a[3]._s = 'XX'; // let's change the newly added item
alert(a); // -x-,-XX-,-z-,-XX- <- 3 and 1 points to the same object!

a.splice(1, 1); // remove one element, starting from index = 1
alert(a); // -x-,-z-,-XX-

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...

function Oo(s) {
    this._s = s;
}

Oo.prototype.toString = function() {
    return "-" + this._s+"-";
}

var a = [];
a.push(new Oo('x'));
a.push(new Oo('y'));
a.push(new Oo('z'));

a[3] = a[1];
alert(a);  // -x-,-y-,-z-,-y-

a[3]._s = 'XX'; // let's change the newly added item
alert(a); // -x-,-XX-,-z-,-XX- <- 3 and 1 points to the same object!

a.splice(1, 1); // remove one element, starting from index = 1
alert(a); // -x-,-z-,-XX-
于我来说 2025-01-05 12:30:52

要取消设置对象属性,您可以使用“删除”运算符。例如:

var f ={
    test1: "fire",
    test2: "water"
}
console.log(f.test2)
//Output water
delete f.test2;
console.log(f.test2)
//Output undefined

要解决您的整体问题,您可以使用类似的东西。

this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);
this.cache['E'] = this.cache['B'];
delete this.cache['B'];

To unset an object property you can use the "delete" operator. For example:

var f ={
    test1: "fire",
    test2: "water"
}
console.log(f.test2)
//Output water
delete f.test2;
console.log(f.test2)
//Output undefined

To resolve your overall problem you could use something similar to.

this.cache['A'] = new Orange(1);
this.cache['B'] = new Orange(2);
this.cache['C'] = new Orange(3);
this.cache['D'] = new Orange(4);
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
笛声青案梦长安 2025-01-05 12:30:52

您确定 E 不再是对象引用吗?

这段代码:

var cache = {};
cache['B'] = new Object( {"hello": "world"} );
cache['E'] = cache['B'];
delete cache['B'];

现在cache['E']仍然包含分配给cache['B']的对象。

> cache['E'].hello
"world"
>

Are you sure that the object reference is no more with E?

This code:

var cache = {};
cache['B'] = new Object( {"hello": "world"} );
cache['E'] = cache['B'];
delete cache['B'];

Now cache['E'] still contains the object, that was assigned to cache['B'].

> cache['E'].hello
"world"
>
浅语花开 2025-01-05 12:30:52

以下内容是否足够?

var myArray= ['a','b','c','d'];
myArray[4] = myArray[1];
myArray.splice(1,1)
var stuff = myArray.join("");
document.write(stuff)

结果:acdb

看看这篇文章

Is the following sufficient?

var myArray= ['a','b','c','d'];
myArray[4] = myArray[1];
myArray.splice(1,1)
var stuff = myArray.join("");
document.write(stuff)

Result: acdb

Take a look at this post

夜无邪 2025-01-05 12:30:52

我希望您使用的是对象而不是数组。 Javascript 中的对象没有 length 属性。

作为数组。这里即使它被声明为数组,值也不会添加到数组中(就像在 php 等中一样)。而是作为属性添加。所以长度将始终为零。

this.cache = [];
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

正如您在上面的示例中看到的,this.cache 没有用作数组。所以你可以创建和反对。

this.cache = {};
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

在这两种情况下,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.

this.cache = [];
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

As you can see in the above example this.cache is not used as an array. So you can create and object.

this.cache = {};
this.cache['A'] = 1;
this.cache['B'] = 2;
this.cache['C'] = 3;
this.cache['D'] = 4;
this.cache['E'] = this.cache['B'];
delete this.cache['B'];
console.log(this.cache['B'], this.cache.length, this.cache.hasOwnProperty('B'));

In both cases hasOwnProperty('B') will return false. If the object has the property B, hasOwnProperty will return true. Your first method should work.

http://jsfiddle.net/diode/hfLJz/1/

.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文