Javascript 如何在jQuery中从数组中删除选定值?

Javascript 如何在jQuery中从数组中删除选定值?,javascript,jquery,arrays,Javascript,Jquery,Arrays,$(文档).ready(函数(){ var colorArray=新数组( “#ff0000”, "#000000", “#00ff00”, “#0000ff” ); var randColor,randListElem; var listlems=$('li'); 对于(变量i=0;i

$(文档).ready(函数(){
var colorArray=新数组(
“#ff0000”,
"#000000",
“#00ff00”,
“#0000ff”
);
var randColor,randListElem;
var listlems=$('li');
对于(变量i=0;i<3;i++)
{
//randColor=Math.floor(Math.random()*(colorArray.length));
sort(函数(){return 0.5-Math.random();});
var ran=colorArray.pop();
randListElem=Math.floor(Math.random()*(listlems.length));
$(listlems[randListElem]).css(“背景”,colorArray[ran]);
};
});
一旦我运行循环中的最后一个命令,我想将colorArray中的颜色从再次使用中移除。我该怎么做?

因为您使用的是jQuery

colorArray.splice($.inArray(randColor,myarray),1)

如果该项不在数组中。好一点的

var index = $.inArray(randColor, colorArray);
if (index>=0) colorArray.splice(index, 1);

var index = $.inArray(randColor, colorArray);
if (index>=0) colorArray.splice(index, 1);
var itemtoRemove = randcolor;
arr.splice($.inArray(itemtoRemove, colorArray),1);