javascript - How can I refresh an array? - Stack Overflow

With this function I create a list:var array = [];var oldarray = [];function port1() {oldarray.push(&#

With this function I create a list:

var array = [];
var oldarray = [];


function port1() {
    oldarray.push('Port '+parseFloat(document.getElementsByClassName(mylist.name)[1].getAttribute('id'))+': '+mylist.name);
    document.getElementById('text').textContent = array;
    $.each(oldarray, function(i, el){
        if($.inArray(el, array) === -1) array.push(el);
    });
}

I want to let the newest entries be add to the last index of the array, and the oldest at the start of the array. And if one entry is ing again, it should appear only once in the array but the index has to "refresh" so that this entry is again at the last position of the array.

With this function I create a list:

var array = [];
var oldarray = [];


function port1() {
    oldarray.push('Port '+parseFloat(document.getElementsByClassName(mylist.name)[1].getAttribute('id'))+': '+mylist.name);
    document.getElementById('text').textContent = array;
    $.each(oldarray, function(i, el){
        if($.inArray(el, array) === -1) array.push(el);
    });
}

I want to let the newest entries be add to the last index of the array, and the oldest at the start of the array. And if one entry is ing again, it should appear only once in the array but the index has to "refresh" so that this entry is again at the last position of the array.

Share Improve this question edited Oct 10, 2018 at 7:32 cнŝdk 32.2k7 gold badges60 silver badges80 bronze badges asked Oct 10, 2018 at 7:18 user10441435user10441435 2
  • So if an entry to be added is already in the array, it should be moved to the last position in the array? – AKX Commented Oct 10, 2018 at 7:25
  • Yes, and I want them later to delete the oldest entry – user10441435 Commented Oct 10, 2018 at 7:27
Add a ment  | 

3 Answers 3

Reset to default 3

You can test over the existence of the element in the array and if it does, remove it and push it in the end, otherwise just push it.

This is how should be your function:

function port1(arr, el) {
  let ind = arr.indexOf(el);
  if (ind > -1)
    arr.splice(ind, 1);
  arr.push(el);
}

Demo:

This is a working demo:

var array = [12, 5, 3, 6];

function port1(arr, el) {
  let ind = arr.indexOf(el);
  if (ind > -1)
    arr.splice(ind, 1);
  arr.push(el);
}

port1(array, 4);
port1(array, 5);
port1(array, 12);

console.log(array);

Something like

function addEntry(array, entry) {
    var entryIndex = array.indexOf(entry);
    if(entryIndex > -1) {
        array.splice(entryIndex, 1);  // Remove the entry from its original position.
    }
    array.push(entry);  // Add to the end of the array.
}

var array = ['Hello', 'World'];
console.log(array);
addEntry(array, 'This is new');
console.log(array);
addEntry(array, 'Hello');
console.log(array);

should work for you. This example outputs

[ 'Hello', 'World' ]
[ 'Hello', 'World', 'This is new' ]
[ 'World', 'This is new', 'Hello' ]

Note that the array is changed in-place.

thank you for your help, your solutions really helped me. I tried also this solution and it seems to work. Maybe not the best and finest solution, but I wanted to add this one. Tank's! :)

var array = ['bye bye', 'hello guys', 'good morning'];
    var entry = 'bye bye';
    if (array.includes(entry)) {
        array.splice(array.indexOf(entry),1);
        array.push(entry);
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744047872a4549519.html

相关推荐

  • javascript - How can I refresh an array? - Stack Overflow

    With this function I create a list:var array = [];var oldarray = [];function port1() {oldarray.push(&#

    9天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信