Javascript数组不反映推送项
我试图更新队列中的位置,然后添加到队列顶部。如果我背对背调用两个更新,则通过队列数组运行的循环不会反映新添加的项 队列是具有位置属性的对象数组:Javascript数组不反映推送项,javascript,arrays,Javascript,Arrays,我试图更新队列中的位置,然后添加到队列顶部。如果我背对背调用两个更新,则通过队列数组运行的循环不会反映新添加的项 队列是具有位置属性的对象数组: [ { position: 0, el: 'stuff' }, { position: 1, el: 'stuff' }, ] 队列是类中的一个属性。以下是我正在使用的两种方法。一个用于增加队列,另一个用于添加队列 addToQueue(el){ this.incrementQueue().push({
[
{
position: 0,
el: 'stuff'
},
{
position: 1,
el: 'stuff'
},
]
队列是类中的一个属性。以下是我正在使用的两种方法。一个用于增加队列,另一个用于添加队列
addToQueue(el){
this.incrementQueue().push({position:0, el:el});
console.log(this.queue)
}
incrementQueue(){
for(var i = 0; i < this.queue.length; i++){
this.queue[i].position++;
if(this.queue[i].position >= this.getMax()){
this.queue.splice(i,1);
}
}
return this.queue;
}
addToQueue(el){
this.incrementQueue().push({position:0,el:el});
console.log(this.queue)
}
递增队列(){
for(var i=0;i=this.getMax()){
这个.队列.拼接(i,1);
}
}
返回此.queue;
}
全班同学都在这里:
// Code goes here
class PassActive{
constructor(options){
this.options = options || (function(){console.warn('You need to pass in an options object'); return {};})();
this.passers = this.options.passers || console.warn('You have not specified any elements to pass active between');
this.max = this.options.max || 1;
this.min = this.options.min || 0;
this.removable = this.options.removable? true : false;
this.queue = this.createQueue();
}
getMin(){
return this.min;
}
getMax(){
return this.max;
}
createQueue(){
var obj = [];
for (var i = 0; i < this.getMax(); i++) {
obj[i] = {
position: i,
el: null
};
}
return obj;
}
isQueueFull(){
var total = 0;
this.queue.forEach(function(cv, ci, arr){
if(cv.el){
total++;
}
});
if(total >= this.max){
return true;
} else {
return false;
}
}
addToQueue(el){
this.incrementQueue().push({position:0, el:el});
console.log(this.queue)
}
incrementQueue(){
for(var i = 0; i < this.queue.length; i++){
this.queue[i].position++;
if(this.queue[i].position >= this.getMax()){
this.queue.splice(i,1);
}
}
return this.queue;
}
setActive(el){
if(el.classList.contains('active')){
if(this.removable) {
el.classList.remove('active');
} else {
return;
}
} else {
el.classList.add('active');
}
}
}
var ops = {
passers: [
document.getElementById('0'),
document.getElementById('1'),
document.getElementById('2'),
document.getElementById('3'),
document.getElementById('4'),
document.getElementById('5')
],
max: 3,
min: 1,
removable: false
};
var t = new PassActive(ops);
console.log(t);
t.addToQueue('Tom');
t.addToQueue('Maureen');
//代码在这里
类通行证{
构造函数(选项){
this.options=options | |(function(){console.warn('需要传入一个options对象');return{};}();
this.passers=this.options.passers | | | console.warn('您尚未指定要在这两者之间传递活动的任何元素');
this.max=this.options.max | | 1;
this.min=this.options.min | | 0;
this.removable=this.options.removable?真:假;
this.queue=this.createQueue();
}
getMin(){
返回这个.min;
}
getMax(){
返回此.max;
}
createQueue(){
var-obj=[];
对于(var i=0;i=此.max){
返回true;
}否则{
返回false;
}
}
addToQueue(el){
this.incrementQueue().push({position:0,el:el});
console.log(this.queue)
}
递增队列(){
for(var i=0;i=this.getMax()){
这个.队列.拼接(i,1);
}
}
返回此.queue;
}
设置激活(el){
if(el.classList.contains('active')){
如果(这个可移动){
el.classList.remove('active');
}否则{
返回;
}
}否则{
el.classList.add('active');
}
}
}
var ops={
过路人:[
document.getElementById('0'),
document.getElementById('1'),
document.getElementById('2'),
document.getElementById('3'),
document.getElementById('4'),
document.getElementById('5')
],
最高:3,
民:1,,
可移除:false
};
var t=新通行证(ops);
控制台日志(t);
t、 addToQueue(“Tom”);
t、 addToQueue('Maureen');
有一个
array.push方法是异步的吗?我可能错过了一些简单的东西
谢谢问题在于循环中的
拼接
调用。假设在索引为4的项目上,您将其拼接,这意味着您的旧项目@5现在是@index 4。但是你的循环计数器在4之后跳到5,所以你现在不会在索引4(以前是5)处检查项目。您可以在if语句中执行i--
问题在于循环中的splice
调用。假设在索引为4的项目上,您将其拼接,这意味着您的旧项目@5现在是@index 4。但是你的循环计数器在4之后跳到5,所以你现在不会在索引4(以前是5)处检查项目。您可以在if语句中执行i--
??您的代码正在调用.concat()
,而不是.push()
@Pointy。固定的。还有问题。很抱歉。嗯,.concat()
和.push()
做了非常不同的事情;这一定改变了行为。@Pointy如果您现在查看代码,您将看到我在increment方法中返回相同的数组,然后在add方法中推到它。我仍然会遇到同样的问题,第二个循环没有考虑第一个循环之后的推送值。一个问题是循环中的拼接。假设在索引为4的项目上,你拼接了它,这意味着你的旧项目@5不是@4。但是你的循环计数器在4之后跳到5,所以你不会在现在的4(以前是5)检查项目。您可以在if语句中执行i--
??您的代码正在调用.concat()
,而不是.push()
@Pointy。固定的。还有问题。很抱歉。嗯,.concat()
和.push()
做了非常不同的事情;这一定改变了行为。@Pointy如果您现在查看代码,您将看到我在increment方法中返回相同的数组,然后在add方法中推到它。我仍然会遇到同样的问题,第二个循环没有考虑第一个循环之后的推送值。一个问题是循环中的拼接。假设在索引为4的项目上,你拼接了它,这意味着你的旧项目@5不是@4。但是你的循环计数器在4之后跳到5,所以你不会在现在的4(以前是5)检查项目。您可以在if语句中执行i--
。我会解决的,唐克斯·詹姆斯。我会解决的。