0

i am working on splicing the old object ie first added object in an array when new element(object) is added in to it automatically.

this is the sample code i worked on

  var sampleArr = [{"id":4,"hostName":"henry"},
                    {"id":3,"hostName":"jeff"},
                      {"id":2,"hostName":"mark"},
                        {"id":1,"hostName":"holder"}];

the above array contains 4 objects when 5 object( {"id":5,"hostName":"punk"}) is added i want to splice first added object ie( {"id":1,"hostName":"holder"}).

here is what i tried in controller

for( var i=0; i<sampleArr.length; i++){
          var index = i;
          if(sampleArr.length > 4){
            sampleArr.splice(index,1,sampleArr[i]);

        } 
        }

but it not working as i expected. please any help me to sought out this!

Sukumar MS
  • 748
  • 1
  • 11
  • 42
  • No need of loop `if (arr.length === 4) { arr[0] = newObj; }` – Tushar Oct 07 '16 at 04:18
  • 1
    `splice` can do many things. Can you show us what output you expect? – LinuxDisciple Oct 07 '16 at 04:21
  • var sampleArr = [{"id":4,"hostName":"henry"}, {"id":3,"hostName":"jeff"}, {"id":2,"hostName":"mark"}, {"id":5,"hostName":"punk"}]; – Sukumar MS Oct 07 '16 at 04:23
  • If you were looking for a more... thorough solution, perhaps consider something like http://stackoverflow.com/questions/1583123/circular-buffer-in-javascript? – Shadowen Oct 07 '16 at 04:30
  • do you want first element to remove or the last one coz' {"id":1,"hostName":"holder"} is last element and {"id":4,"hostName":"henry"} is first one. – A.T. Oct 07 '16 at 05:13

4 Answers4

1

You can use the simple pop() to take out the last element and use push to add a new element into the array!

var sampleArr = [{
  "id": 4,
  "hostName": "henry"
}, {
  "id": 3,
  "hostName": "jeff"
}, {
  "id": 2,
  "hostName": "mark"
}, {
  "id": 1,
  "hostName": "holder"
}];



sampleArr.pop()

sampleArr.push({
  "id": 5,
  "hostName": "punk"
})

console.log(sampleArr)
Pranesh Ravi
  • 18,642
  • 9
  • 46
  • 70
1

You can use unshift to add new item in begin of array and use pop to remove last item in array. Read more here.

Try this:

sampleArr.unshift({"id":5,"hostName":"punk"});
sampleArr.pop();

Hope will helps.

Community
  • 1
  • 1
DieuNQ
  • 975
  • 1
  • 7
  • 11
  • See also http://stackoverflow.com/a/33608379/5195629 for an implementation that wraps this in a function (fairly simple really, but I wanted to include it for completeness). – Shadowen Oct 07 '16 at 04:32
1

I think OP is asking for FIFO (first in first out operation) , since I don't understand how {"id":1,"hostName":"holder"} could be first element in below array. But here is what I suggest.

Array.prototype.performFIFO = function(element) { this.push(element); this.shift(); return this; } 

var sampleArr = [{"id":4,"hostName":"henry"},
                    {"id":3,"hostName":"jeff"},
                      {"id":2,"hostName":"mark"},
                        {"id":1,"hostName":"holder"}];



sampleArr.performFIFO({"id":5,"hostName":"fifth element"})

console.log(sampleArr)
A.T.
  • 24,694
  • 8
  • 47
  • 65
0

Try this:

sampleArr.splice(sampleArr.length,1,sampleArr[i]);
Vojtech Ruzicka
  • 16,384
  • 15
  • 63
  • 66