删除数组项 [英] removing array item

查看:62
本文介绍了删除数组项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容。必须有一些不那么麻烦的东西没有推动

pop。函数参数obj是event.srcElement并且具有

属性(属性?)picNum和alb固定到它。该功能是

的一部分,用于删除所选obj的过程,本案例中的照片来自专辑

vis albumArr。 TIA

Jimbo

功能专辑(albumName){

this.name = albumName;

this.paths = new Array();

}

var albumArr(); //包含一个Albums()数组;

函数removeArrSrc(obj){

var dummy = new Array();

for( i = 0; i< albumArr.length; i ++){

dummy [i] = new Album(albumArr [i] .name);

var el = albumArr [ i] .paths;

for(var j = 0; j< el.length; j ++){

if(dummy [i] .name == obj.alb ){

如果(j == obj.picNum)继续;

}

var len = dummy [i] .paths.length;

dummy [i] .paths [len] = el [j];

}

}

albumArr =假;

}

I have the following. There must be something less cumbersome without push
pop. The function parameter obj is the event.srcElement and has the
attributes(properties?) picNum and alb pinned to it. The function is part of
a process to delete the selected obj, a photo in this case from the album
vis albumArr. TIA
Jimbo
function Album(albumName) {
this.name=albumName;
this.paths=new Array();
}
var albumArr(); // holds an array of Albums();
function removeArrSrc(obj) {
var dummy=new Array();
for(i=0;i<albumArr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNum) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}

推荐答案

2004年8月30日星期一11:47:41 +0200 ,JJ Cale< ph **** @ netvision.net.il>

写道:


01234567890123456789012345678901234567890123456789 01234567890123456789
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:

01234567890123456789012345678901234567890123456789 01234567890123456789
I有以下几点。没有
push pop,必须有一些不那么麻烦的东西。函数参数obj是event.srcElement


这是针对Web的,还是只针对IE的环境?

并且具有属性(属性? )picNum和alb寄托在它上面。
功能是删除所选obj的过程的一部分,这张照片来自于专辑和albumArr。 TIA
功能专辑(albumName){
this.name = albumName;
this.paths = new Array();
}
var albumArr(); //包含一个Albums()数组;


我不知道那应该是什么,但我确定它会导致大多数

错误浏览器。也许你的意思是:


var albumArr = [];

函数removeArrSrc(obj){
var dummy = new Array();
for(i = 0; i< albumArr.length; i ++){
dummy [i] = new Album(albumArr [i] .name);
var el = albumArr [i] .paths ;
for(var j = 0; j< el.length; j ++){
if(dummy [i] .name == obj.alb){
if(j == obj) .picNum)继续;
}
var len = dummy [i] .paths.length;
dummy [i] .paths [len] = el [j];
}
}
albumArr = dummy;
}
I have the following. There must be something less cumbersome without
push pop. The function parameter obj is the event.srcElement
Is this for the Web, or just an IE-only environment?
and has the attributes(properties?) picNum and alb pinned to it. The
function is part of a process to delete the selected obj, a photo in
this case from the album vis albumArr. TIA function Album(albumName) {
this.name=albumName;
this.paths=new Array();
}
var albumArr(); // holds an array of Albums();
I don''t know what that''s supposed to be, but I''m certain it''ll cause an
error on most browsers. Perhaps you meant:

var albumArr = [];
function removeArrSrc(obj) {
var dummy=new Array();
for(i=0;i<albumArr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNum) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}




怎么样


函数removeArrSrc(o){

var i = 0,n = albumArr.length;


//找到元素,' 'o''

while(i< n&& albumArr [i]!= o){++ i;}

//如果''o ''存在于数组中,''我'现在将它指向

if(i< n){

//不想达到最后一个元素

--n;

//现在将剩余的元素向下移动

while(i< n){albumArr [i] = albumArr [i + 1]; ++ i;}

//最后,截断数组

albumArr.length = n;

}

}


就是就地删除元素。您还可以使用:


函数removeArrSrc(o){

var i = 0,n = albumArr.length;


//找到元素''o''

while(i< n&& albumArr [i]!= o){++ i;}

//如果数组中存在''o',''我'现在将它指向

if(i< n){

albumArr = albumArr.slice(0,i).concat(albumArr.slice(i + 1));

}

}


可能会更快一些(由于浏览器执行),

但可能不是(由于产生的数组分配)。

顺便说一下,我还没有测试过这些。


祝你好运,

Mike


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



How about:

function removeArrSrc(o) {
var i = 0, n = albumArr.length;

// Find the element, ''o''
while(i < n && albumArr[i] != o) {++i;}
// If ''o'' exists in the array, ''i'' will now point it
if(i < n) {
// Don''t want to reach the last element
--n;
// Now shift the remaining elements down
while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
// Finally, truncate the array
albumArr.length = n;
}
}

which is removes the element in-place. You could also use:

function removeArrSrc(o) {
var i = 0, n = albumArr.length;

// Find the element, ''o''
while(i < n && albumArr[i] != o) {++i;}
// If ''o'' exists in the array, ''i'' will now point it
if(i < n) {
albumArr = albumArr.slice(0, i).concat(albumArr.slice(i + 1));
}
}

which might be a little quicker (due to being performed by the browser),
but probably isn''t (due to the resulting array allocations).

I haven''t tested these, by the way.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.




" Michael Winter" < M ****** @ blueyonder.co.invalid>。在消息中写道

news:opsdjinqrxx13kvk @ atlantis ...

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsdjinqrxx13kvk@atlantis...
2004年8月30日星期一11:47:41 +0200,JJ Cale< ph **** @ netvision.net.il>
写道:

01234567890123456789012345678901234567890123456789 01234567890123456789
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:

01234567890123456789012345678901234567890123456789 01234567890123456789
我有以下内容。没有
push pop,必须有一些不那么麻烦的东西。函数参数obj是event.srcElement
这适用于Web,还是只适用于IE的环境?
I have the following. There must be something less cumbersome without
push pop. The function parameter obj is the event.srcElement
Is this for the Web, or just an IE-only environment?




理想情况下它虽然是通用的我正在写一个HTA。



Ideally it would be generic although currently I''m writing an HTA.

并且具有属性(属性?)picNum和alb固定到它。
功能是删除所选obj的过程的一部分,这张照片来自于专辑和albumArr。 TIA
and has the attributes(properties?) picNum and alb pinned to it. The
function is part of a process to delete the selected obj, a photo in
this case from the album vis albumArr. TIA


function Album(albumName){
this.name = albumName;
this.paths = new Array();


路径数组保存图像路径

}
var albumArr(); //包含一个Albums()数组;
function Album(albumName) {
this.name=albumName;
this.paths=new Array();
paths array holds image paths
}
var albumArr(); // holds an array of Albums();



我不知道那应该是什么,但我确定它会导致<大多数浏览器都有错误。也许你的意思是:

var albumArr = [];



I don''t know what that''s supposed to be, but I''m certain it''ll cause an
error on most browsers. Perhaps you meant:

var albumArr = [];



对不起!它声明为var albumArr = new Array();


Sorry! It is declared var albumArr=new Array();

function removeArrSrc(obj){
var dummy = new Array();
for (i = 0; i< albumArr.length; i ++){
dummy [i] = new Album(albumArr [i] .name);
var el = albumArr [i] .paths;
for(var j = 0; j< el.length; j ++){
if(dummy [i] .name == obj.alb){
if(j == obj.picNum)继续;
}
var len = dummy [i] .paths.length;
dummy [i] .paths [len] = el [j];
}
}
albumArr = dummy;
}
function removeArrSrc(obj) {
var dummy=new Array();
for(i=0;i<albumArr.length;i++) {
dummy[i]=new Album(albumArr[i].name);
var el=albumArr[i].paths;
for(var j=0;j<el.length;j++) {
if(dummy[i].name==obj.alb) {
if(j==obj.picNum) continue;
}
var len=dummy[i].paths.length;
dummy[i].paths[len]=el[j];
}
}
albumArr=dummy;
}



如何

函数removeArrSrc(o) {
var i = 0,n = albumArr.length;

//找到元素''o''
while(i< n&& albumArr [i]!= o){++ i;}



How about:

function removeArrSrc(o) {
var i = 0, n = albumArr.length;

// Find the element, ''o''
while(i < n && albumArr[i] != o) {++i;}



数组albumArr包含一个Album对象数组。我需要删除

一张图片的路径。

参考obj(这里''o'')有专辑名称和
中的索引
路径数组固定为属性

我正在搜索o.alb(专辑名称)和o.picNum

偏移到路径数组相册对象。

while(i< n&& albumArr [i] .name!= o.alb){++ i}应该工作

找到专辑

//如果数组中存在''o',''我'现在将它指向
if(i< n){
/ /不想达到最后一个元素
- n;
//现在将剩下的元素向下移动
while(i< n){albumArr [i] = albumArr [我+ 1]; ++ i;}
//最后,截断数组
albumArr.length = n;
}
}

这将删除整个专辑。我需要从相册的

路径数组中删除图像。带正则表达式的切片方法是我正在寻找

但是我只是进入正则表达式。

这就是就地删除了元素。你也可以使用:

函数removeArrSrc(o){
var i = 0,n = albumArr.length;

//找到元素,'' o''
while(i< n&& albumArr [i]!= o){++ i;}
//如果数组中存在o,''我现在将指出它
如果(i< n){alb / n} albumArr = albumArr.slice(0,i).concat(albumArr.slice(i + 1));
这可能会更快(由于浏览器执行),但可能不是(由于产生的数组分配)。<我希望这次我能更好地解释自己。感谢上面的
,特别是因为我还没有编写删除相册的功能。

我相信这可以完成这项工作。

Jimbo顺便说一下,我还没有测试过这些。

祝你好运,
迈克

-
Michael Winter
替换.invalid与.uk通过电子邮件回复。


the array albumArr contains an array of Album objects. I need to delete
the path for one image.
the reference obj (here ''o'') has the album name and the index in the
paths array pinned as attributes
I''m searching therefore for o.alb (the album name) and o.picNum the
offset into the paths array of the Album object.
while(i<n && albumArr[i].name != o.alb) {++i} should work to
find the album
// If ''o'' exists in the array, ''i'' will now point it
if(i < n) {
// Don''t want to reach the last element
--n;
// Now shift the remaining elements down
while(i < n) {albumArr[i] = albumArr[i + 1]; ++i;}
// Finally, truncate the array
albumArr.length = n;
}
}
This would delete the entire album. I need to delete the image from the
paths array of the album. The slice method with a regex is what I''m looking
for but I''m just getting into regex.
which is removes the element in-place. You could also use:

function removeArrSrc(o) {
var i = 0, n = albumArr.length;

// Find the element, ''o''
while(i < n && albumArr[i] != o) {++i;}
// If ''o'' exists in the array, ''i'' will now point it
if(i < n) {
albumArr = albumArr.slice(0, i).concat(albumArr.slice(i + 1));
}
}

which might be a little quicker (due to being performed by the browser),
but probably isn''t (due to the resulting array allocations).
I hope I''ve explained myself better this time. Thanks for the above particularly because I haven''t written the funtion to delete an album yet.
I''m sure this will do the job.
Jimbo I haven''t tested these, by the way.

Good luck,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.



2004年8月31日星期二09:02:32 +0200,JJ Cale< ph **** @ netvision.net.il>

写道:
On Tue, 31 Aug 2004 09:02:32 +0200, J. J. Cale <ph****@netvision.net.il>
wrote:
" Michael Winter" < M ****** @ blueyonder.co.invalid>。在消息中写道
新闻:opsdjinqrxx13kvk @ atlantis ...
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:opsdjinqrxx13kvk@atlantis...
2004年8月30日星期一11:47:41 + 0200,JJ Cale
< ph ****@netvision.net.il>写道:


[snip]
On Mon, 30 Aug 2004 11:47:41 +0200, J. J. Cale
<ph****@netvision.net.il> wrote:
[snip]
event.srcElement
这是针对Web,还是仅针对IE的环境?
event.srcElement
Is this for the Web, or just an IE-only environment?



理想情况下它会是通用的,虽然目前我正在写一个HTA。



Ideally it would be generic although currently I''m writing an HTA.




我问的原因是那个事件.srcElement是IE-only。您还需要

来检查event.target,以及可能的其他属性,以便代码与其他浏览器一起工作

。但是,既然你正在编写一个HTA,那么目前关注的并不是很多。


[snip]

数组albumArr包含一个Album对象数组。我需要
删除一个图像的路径。


抱歉。

参考obj(此处为'''')将专辑名称和
路径数组中的索引固定为因此,我正在搜索o.alb(专辑名称)和o.picNum
偏移到Album对象的路径数组中。
while(i< n& & albumArr [i] .name!= o.alb){++ i}应该工作
找到专辑


是的,它会。


[snip]

这会删除整张专辑。我需要从相册的
路径数组中删除图像。带正则表达式的切片方法是我正在寻找的,但我只是进入正则表达式。



The reason I asked is that event.srcElement is IE-only. You''ll also have
to check event.target, and possibly other properties, for the code to work
with other browsers. However, since you''re writing a HTA, it isn''t of much
concern at the moment.

[snip]
the array albumArr contains an array of Album objects. I need to
delete the path for one image.
Sorry.
the reference obj (here ''o'') has the album name and the index in the
paths array pinned as attributes
I''m searching therefore for o.alb (the album name) and o.picNum the
offset into the paths array of the Album object.
while(i<n && albumArr[i].name != o.alb) {++i} should work
to find the album
Yes, it would.

[snip]
This would delete the entire album. I need to delete the image from the
paths array of the album. The slice method with a regex is what I''m
looking for but I''m just getting into regex.




我不认为正则表达式非常有用。

当然,我看不到全貌,所以我错了。


[snip]


功能专辑(albumName){

this.name = albumName;

this.paths = [];

}

Album.prototype.removePath = function(i){

for(var n = this.paths - 1; i< n; ++ i){

this.paths [i] = this.paths [i + 1];

}

this.paths.length = n;

};


函数removeArrSrc(o){

for(var i = 0,n = albumArr.length; i< n; + + i){

if(albumArr [i] .name == o.alb){

albumArr [i] .removePath(o.picNum);

休息;

}

}

}


我认为可能问题在这里。假设您删除了第一条路径

(picNum 0)。在路径数组中,所有元素都将移位,使第二条路径成为第二条路径,第三条路径为第二条路径等。如果删除第二条

路径(picNum 1) ,你实际上将从

原始列表中删除第三条路径。你是否说明了这一点?


迈克


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。



I don''t think that a regular expression would be terribly useful. Of
course, I''m not seeing the whole picture, so I could be wrong.

[snip]

function Album(albumName) {
this.name = albumName;
this.paths = [];
}
Album.prototype.removePath = function(i) {
for(var n = this.paths - 1; i < n; ++i) {
this.paths[i] = this.paths[i + 1];
}
this.paths.length = n;
};

function removeArrSrc(o) {
for(var i = 0, n = albumArr.length; i < n; ++i) {
if(albumArr[i].name == o.alb) {
albumArr[i].removePath(o.picNum);
break;
}
}
}

I forsee a possible problem here. Say that you delete the first path
(picNum 0). In the paths array, all of the elements will shift making the
second path first, third path second, etc. If you then delete the second
path (picNum 1), you''ll actually be deleting the third path from the
original list. Do you account for that?

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


这篇关于删除数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆