Javascript 使用此特定代码从另一个数组中删除一个数组
我有以下数组,其中包含其他数组(它们实际上是html5画布的坐标) 他说:Javascript 使用此特定代码从另一个数组中删除一个数组,javascript,jquery,Javascript,Jquery,我有以下数组,其中包含其他数组(它们实际上是html5画布的坐标) 他说: x = 317; y = 193; 在以下函数中,如何从crossesPos中删除阵列[317193]? function checkForCrosses(x,y){ var position; jQuery.each(crossesPos, function(){ position = this; if(x == position[0] && y ==
x = 317;
y = 193;
在以下函数中,如何从crossesPos中删除阵列[317193]?
function checkForCrosses(x,y){
var position;
jQuery.each(crossesPos, function(){
position = this;
if(x == position[0] && y == position[1]){
// how do I remove the array [317,193] from crossesPos?
}
});
}
泰 应该使用.splice()函数
我认为您需要使用splice()删除找到的项。这是一个有效的工具。打开控制台以查看修改后的阵列 要添加到函数中的代码:
function checkForCrosses(x, y){
var position,
length = crossesPos.length;
for (var i = length; i > 0; i--) {
position = crossesPos[i - 1];
if (x == position[0] && y == position[1])
crossesPos.splice(i - 1, 1);
}
}
var x = 317,
y = 193,
newPos = $.grep(crossesPos, function(n, i){
return (n[0] != x || n[1] != y);
});
或者可以让这变得非常容易,但如果你坚持直截了当:
var pos = -1;
jQuery.each(crossesPos,function(idx,item) {
if (x===item[0] && y===item[1]) {
pos = idx;
}
});
crossesPos.splice(idx,1);
或者更简单地说
var ary = [];
jQuery.each(crossesPos,function(idx,item) {
if (x!==item[0] || y!==item[1]) {
ary.push(item);
}
});
crossesPos = ary;
实现这一点的“jQuery方法”是使用:
使用jQuery的实用功能:
function checkForCrosses(x, y){
var position,
length = crossesPos.length;
for (var i = length; i > 0; i--) {
position = crossesPos[i - 1];
if (x == position[0] && y == position[1])
crossesPos.splice(i - 1, 1);
}
}
var x = 317,
y = 193,
newPos = $.grep(crossesPos, function(n, i){
return (n[0] != x || n[1] != y);
});
使用以下代码拼接阵列的精确坐标。将此代码放入函数中。这比JQuery代码更有效 纯JavaScript
这是(在撰写本文时)执行您的需求的最有效和最高效的方式,因为最接近的结果仍然比我的答案慢90%
使用
过滤器
function checkForCrosses(x,y){
function myFilter(element, index, array) {
return (element[0] != x || element[1] != y);
}
crossesPos = crossesPos.filter(myFilter);
}
然后crossesPos
在没有[317193]
//
//
//
// here's some common Array operations you might find usefull and familiar:
//
// .each( callback, boolFlgFromLast )
// # iterate an array runing given callback-fn for every array item,
// # pass it curent array key and value, respectively,
// # set context of the callback to host array,
// # if boolFlgFromLast argument is ( === )true, iterate from last element,
// # break iteration if callback returns ( === )false,
// # return iterated array
//
// .not( callback )
// # remove items for which callback returns ( === )true
// # keep others
// # return host array
//
// .keep( callback )
// # keep items for which callback returns ( === )true
// # remove others
// # return host array
//
// .desparse()
// # 'desparse' host array in place
// # return host array
//
//
// var
// a = [
// [317, 193],
// [110, 334],
// [390, 347],
// ];
//
//
// a
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .not( function ( k, v ) { console.log(' * '); return ( v[0] == 317 ) && ( v[1] == 193 ); } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .keep( function ( k, v ) { console.log(' * '); return Math.random() > .1; } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v + ', [ this === a ] -> '+ ( this === a ) ); } );
//
// // make sparse array
// a[5] = [0,0];
// console.log('sparse array: ', a);
//
// a
// .desparse()
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
//
//
//
;( function( _a ) {
var
t = !0,
f = !t;
_a.each = function ( fn ) {
var
len = this.length,
i = 0;
for( ; i < len ; i++ ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
};
overload( 'each', _a, function ( fn, flgIterateBackwards ) {
if ( flgIterateBackwards === t ) {
var
i = this.length - 1;
for ( ; i >= 0 ; i-- ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
} else {
return this.each( fn );
}
}
);
_a.not = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) && this.splice( k, 1 );
}, t );
};
_a.keep = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) || this.splice( k, 1 );
}, t );
};
_a.desparse = function () {
return this.not( function ( k, v ) { return k in this === f; } );
};
// helper fn-s
function overload ( fn, obj, newfn ) {
return ( function ( origfn ) {
obj[fn] = function () {
var
args = _a.slice.call( arguments );
if ( newfn.length == arguments.length ) {
return newfn.apply( this, args )
} else if ( isfn ( origfn ) ) {
return origfn.apply( this, args )
} else {
// ignore non method props
}
};
} )( obj[fn] );
}
function isfn ( o ) {
return ( typeof o === 'function' ) &&
( Object.prototype.toString.call( o ) === '[object Function]' );
}
} )( Array.prototype );
//
//
//以下是一些您可能会觉得有用且熟悉的常见阵列操作:
//
//.each(回调、boolFlgFromLast)
//#对每个数组项迭代运行给定回调函数fn的数组,
//#分别传递当前数组键和值,
//#将回调的上下文设置为主机数组,
//#如果boolFlgFromLast参数为(===)true,则从最后一个元素迭代,
//#如果回调返回(===)false,则中断迭代,
//#返回迭代数组
//
//.not(回调)
//#删除回调返回(==)true的项
//#留住别人
//#返回主机阵列
//
//.保留(回拨)
//#保留回调返回(==)true的项
//#删除其他文件
//#返回主机阵列
//
//.desparse()
//#“desparse”主机阵列已就位
//#返回主机阵列
//
//
//变量
//a=[
// [317, 193],
// [110, 334],
// [390, 347],
// ];
//
//
//a
//.each(函数(k,v){console.log('['+k+']->'+v);})
//.not(函数(k,v){console.log('*');返回(v[0]==317)&&(v[1]==193);})
//.each(函数(k,v){console.log('['+k+']->'+v);})
//.keep(函数(k,v){console.log('*');返回Math.random()>.1;})
//.each(函数(k,v){console.log('['+k+']->'+v+',[this===a]->'+(this==a));});
//
////生成稀疏数组
//a[5]=[0,0];
//log('稀疏数组:',a);
//
//a
//.desparse()
//.each(函数(k,v){console.log('['+k+']->'+v);})
//
//
//
;(函数(_a){
变量
t=!0,
f=!t;
_a、 每个=函数(fn){
变量
len=这个长度,
i=0;
对于(;i=0;i--){
如果(fn.调用(this,i,this[i])==f)中断;
}
归还这个;
}否则{
返回此。每个(fn);
}
}
);
_a、 not=函数(回调){
返回这个。每个(函数(k,v){
(callback.call(this,k,v)==t)和&this.splice(k,1);
},t);
};
_a、 keep=函数(回调){
返回这个。每个(函数(k,v){
(callback.call(this,k,v)==t)| | this.splice(k,1);
},t);
};
_a、 desparse=函数(){
返回this.not(函数(k,v){returnk in this==f;});
};
//助手fn-s
函数重载(fn,obj,newfn){
返回(函数(origfn){
obj[fn]=函数(){
变量
args=_a.slice.call(参数);
if(newfn.length==arguments.length){
返回newfn.apply(此参数为args)
}否则如果(isfn(origfn)){
返回origfn.apply(此参数)
}否则{
//忽略非方法道具
}
};
})(obj[fn]);
}
函数isfn(o){
返回(o的类型=='函数')&&
(Object.prototype.toString.call(o)='[Object Function]');
}
})(Array.prototype);
//
测试应该是(n[0]!=x | | n[1]!=y)
。再读一遍-问题是移除数组[317193]
。是的,这个代码也会移除[317,0]
和[0193]
。呃,我想你是糊涂了。如果同一数组中的x为317,y为193,则不返回,即[317193]。它将返回所需的所有其他内容。对于[317,0]
,条件是(317!=317&&0!=193)
,这是(true和false)
这是false
,这意味着[317,0]
将被错误地删除。函数必须返回falsefor(var i=crossesPos.length-1; i>=0; i--)
{
if(crossesPos[i][0] === x)
if(crossesPos[i][1] === y) crossesPos.splice(i,1);
}
function checkForCrosses(x,y){
function myFilter(element, index, array) {
return (element[0] != x || element[1] != y);
}
crossesPos = crossesPos.filter(myFilter);
}
//
//
// here's some common Array operations you might find usefull and familiar:
//
// .each( callback, boolFlgFromLast )
// # iterate an array runing given callback-fn for every array item,
// # pass it curent array key and value, respectively,
// # set context of the callback to host array,
// # if boolFlgFromLast argument is ( === )true, iterate from last element,
// # break iteration if callback returns ( === )false,
// # return iterated array
//
// .not( callback )
// # remove items for which callback returns ( === )true
// # keep others
// # return host array
//
// .keep( callback )
// # keep items for which callback returns ( === )true
// # remove others
// # return host array
//
// .desparse()
// # 'desparse' host array in place
// # return host array
//
//
// var
// a = [
// [317, 193],
// [110, 334],
// [390, 347],
// ];
//
//
// a
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .not( function ( k, v ) { console.log(' * '); return ( v[0] == 317 ) && ( v[1] == 193 ); } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
// .keep( function ( k, v ) { console.log(' * '); return Math.random() > .1; } )
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v + ', [ this === a ] -> '+ ( this === a ) ); } );
//
// // make sparse array
// a[5] = [0,0];
// console.log('sparse array: ', a);
//
// a
// .desparse()
// .each( function ( k, v ) { console.log('['+ k +'] -> '+ v ); } )
//
//
//
;( function( _a ) {
var
t = !0,
f = !t;
_a.each = function ( fn ) {
var
len = this.length,
i = 0;
for( ; i < len ; i++ ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
};
overload( 'each', _a, function ( fn, flgIterateBackwards ) {
if ( flgIterateBackwards === t ) {
var
i = this.length - 1;
for ( ; i >= 0 ; i-- ) {
if ( fn.call( this, i, this[i] ) === f ) break;
}
return this;
} else {
return this.each( fn );
}
}
);
_a.not = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) && this.splice( k, 1 );
}, t );
};
_a.keep = function ( callback ) {
return this.each( function ( k, v ) {
( callback.call( this, k, v ) === t ) || this.splice( k, 1 );
}, t );
};
_a.desparse = function () {
return this.not( function ( k, v ) { return k in this === f; } );
};
// helper fn-s
function overload ( fn, obj, newfn ) {
return ( function ( origfn ) {
obj[fn] = function () {
var
args = _a.slice.call( arguments );
if ( newfn.length == arguments.length ) {
return newfn.apply( this, args )
} else if ( isfn ( origfn ) ) {
return origfn.apply( this, args )
} else {
// ignore non method props
}
};
} )( obj[fn] );
}
function isfn ( o ) {
return ( typeof o === 'function' ) &&
( Object.prototype.toString.call( o ) === '[object Function]' );
}
} )( Array.prototype );
//