How to add top three elements to an array and remove previous results in javascript

what I need

  • I need to store the top item in an array.
  • and remove the previous top first three elements from the array.
  • when the 4th element is added to the array 1 is removed and the 5th adds 2 is removed.
  • The maximum limit of the array is 3.

  • as arr [1,2,3];

  • when the 4th element is inserted it will remove the index arr [1] and so on.

  • and store the array in a cookie

js code

   var cookie_event_id=document.getElementById("eventID").value;

    var e = $(window).height();

   var arr=[];
   if (cookie.get("event_id")) {
    var t=cookie_event_id;
    var t = cookie.get("event_id");
    console.log(arr.toString());

    arr.push(cookie_event_id);
    arr.shift(); 
    alert(cookie_event_id);

    var i = new Date;
    i.setDate(i.getFullYear() ), document.cookie = "event_id =" + cookie_event_id + ";expires=" + i.toUTCString() + ";domain=.10times.com;path=/"
} else
 {
    var t=cookie_event_id;
    var i = new Date;
    i.setDate(i.getFullYear() ), document.cookie = "event_id =" + t + ";expires=" + i.toUTCString() + ";domain=.10times.com;path=/"
}

      

  • I want this id to be added to an array, then this array should be clicked into the cookie.

  • and remove the first index of the array when adding a new item, etc.

  • The maximum limit for arrays is 3.

+3


source to share


4 answers


If you want to add a new element to the beginning of the array: Use unshift

:

var arr = [1, 2, 3];
arr.unshift(4); // [4, 1, 2, 3]
arr.length = 3; // [4, 1, 2]

      

The unshift () method adds one or more elements to the beginning of the array and returns the new length of the array.

Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

If you want the element to be added at the end: Use push

and shift

:

var arr = [1, 2, 3];
arr.push(4); // [1, 2, 3, 4]
arr.shift(); // [2, 3, 4]

      



push

:

The push () method adds one or more elements to the end of the array and returns the new length of the array.

Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/push

shift

:

The shift () method removes the first element from the array and returns that element. This method changes the length of the array.

Docs: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

+3


source


I understand that you need to keep the last three added items:



myArray = [];

var addEItem = function(item) {
    myArray.push(item); // Add at the end of the array
    if (myArray.length > 3) {
        myArray.shift(); // Remove 1st item if size is larger than 3
    }
}

      

+1


source


You can use array.splice()

. Here's a small example:

var arr = ["el1", "el2", "el3","el4", "el6", "el7"];
console.log(arr.splice(3)); // ["el4", "el6", "el7"]

      

What you need to do is click on the result and then concatenate it as shown in the example above. http://jsfiddle.net/ztvs2trt/

0


source


updated working code

 var cookie_event_id=document.getElementById("eventID").value;
 //alert(cookie_event_id);
var e = $(window).height();

 var arr=[];
if (cookie.get("event_id")) {

    var t = cookie.get("event_id") + ','+cookie_event_id;
   var arr = JSON.parse("[" + t+ "]");
    if(arr.length > 3)
    arr.shift(); 
    alert(arr);
    arr.toString();


    var i = new Date;
    i.setDate(i.getFullYear() ), document.cookie = "event_id =" + arr + ";expires=" + i.toUTCString() + ";domain=.10times.com;path=/"
} else
 {
    var t=cookie_event_id;
    var i = new Date;
    i.setDate(i.getFullYear() ), document.cookie = "event_id =" + t + ";expires=" + i.toUTCString() + ";domain=.10times.com;path=/"
}

      

0


source







All Articles