admin管理员组文章数量:1356294
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
3 Answers
Reset to default 3You 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);
}
本文标签: javascriptHow can I refresh an arrayStack Overflow
版权声明:本文标题:javascript - How can I refresh an array? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744047871a2581868.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论