简体   繁体   English

灰烬功能:阵列项目推入和移除

[英]Ember function: Array item push and remove

I have an ember function that is continuing the sequence of 2^x where X is the length of the array. 我有一个余烬函数,它将继续2 ^ x的序列,其中X是数组的长度。

//sequence of ^2: 1, 2, 4, 8, 16, etc...
this.set("numSeq", Ember.Array([1, 2, 4]));

when i push a button I update the sequence using the update function 当我按下按钮时,我会使用update功能来update序列

update: function() {
    var numSeq = this.get("numSeq");
    let Sqlength = numSeq.length;
    let node = Math.pow(2, Sqlength);
    const nodeMax = Math.pow(2, 20);
    if (node >= nodeMax) {
        this.initializeSequence();
    } else if (Sqlength >= 10) {
        sequence.pushObject(node);
        let smallFry = sequence[0];
        sequence.removeObject(smallFry);

    } else {
        sequence.pushObject(node);
    }
}

there are two restrictions to the update function, when a new array input exceeds 2^20 we reset the array, and when the length is greater than 10 we remove the smallest element and still add the new node. 更新功能有两个限制,当新数组输入超过2 ^ 20时,我们将重置数组,而当长度大于10时,我们将删除最小的元素,并仍添加新节点。

My issue comes when trying to push new objects to array and remove the bottom most element, any method I have used to far slice , removeat , removeobject , it "freezes" the array and just adds the last element not the new node 当试图将新对象推入数组并删除最底层的元素时,我的问题就来了,我曾经使用过的任何方法都可以对sliceremoveatremoveobject进行远slice ,它“冻结”数组并仅添加最后一个元素而不是新节点

(example after pressing the button 12 times [1,2,4,...,512,1024,1024,1024] (例如,按下按钮12次[1,2,4,...,512,1024,1024,1024]

no errors so i believe this my fault and a misunderstanding of concepts 没有错误,所以我相信这是我的错,也是对概念的误解

My error was using the length as the exponent, I just had to use the currentNode*2 to get the next node, not use the length 我的错误是使用长度作为指数,我只需要使用currentNode * 2来获取下一个节点,而不是使用长度

keeping length at ten always gave me 1024 which is why it would never grow! 保持十个长度总是给我1024,这就是为什么它永远不会增长的原因! my own failure but figured it out 我自己的失败,但想通了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM