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!

5
  • No need of loop if (arr.length === 4) { arr[0] = newObj; } Commented Oct 7, 2016 at 4:18
  • 1
    splice can do many things. Can you show us what output you expect? Commented Oct 7, 2016 at 4:21
  • var sampleArr = [{"id":4,"hostName":"henry"}, {"id":3,"hostName":"jeff"}, {"id":2,"hostName":"mark"}, {"id":5,"hostName":"punk"}]; Commented Oct 7, 2016 at 4:23
  • If you were looking for a more... thorough solution, perhaps consider something like stackoverflow.com/questions/1583123/…? Commented Oct 7, 2016 at 4: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. Commented Oct 7, 2016 at 5:13

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

2 Comments

can i push like this sampleArr.push(sampleArr[sampleArr[5]]);
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.

1 Comment

See also 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).
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)

Comments

0

Try this:

sampleArr.splice(sampleArr.length,1,sampleArr[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.