jquery - how to add the top three elements in array and delete the previous results in javascript -


what need

  • i need store top 3 element in array .
  • and remove previous top first 3 element array.
  • when 4 th elements adds in array 1 deleted , whn 5 th adds 2 deleted.
  • max array limit 3.

  • like arr[1,2,3];

  • when 4 th element inserted delete arr[1] index , on.

  • and store array in 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 = 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 = new date;     i.setdate(i.getfullyear() ), document.cookie = "event_id =" + t + ";expires=" + i.toutcstring() + ";domain=.10times.com;path=/" } 
  • i want id should appended in array , array should pushed in cookie.

  • and delete first index of array when new element adds up, , on.

  • max limit of arrays 3.

if want add new element @ beginning of 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 1 or more elements beginning of array , returns new length of array.

docs: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/unshift

if want element added @ end: use push , shift:

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

push:

the push() method adds 1 or more elements end of array , returns new length of array.

docs: https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/array/push

shift:

the shift() method removes first element array , returns element. method changes length of array.

docs: https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/array/shift


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -