5

I have the following JavaScript array :

 var days = [
            {
                "day": "sunday",
                "morning": "geschlossen",
            },
            {
                "day": "monday",
                "morning": "geschlossen",
            },
            {
                "day": "tuesday",
                "morning": "geschlossen",
            },
            {
                "day": "wenesday",
                "morning": "geschlossen",
            },
            {
                "day": "thursday",
                "morning": "16:30 - 19:00 Uhr",
            },
            {
                "day": "friday",
                "morning": "09:00 - 18:00 Uhr",
            },
            {
                "day": "saturday",
                "morning": "geschlossen",
            }
        ];

How can i change the 0th index object to last value in the array?

so my expected array will be like this :

 var days = [               
            {
                "day": "monday",
                "morning": "geschlossen",
            },
            {
                "day": "tuesday",
                "morning": "geschlossen",
            },
            {
                "day": "wenesday",
                "morning": "geschlossen",
            },
            {
                "day": "thursday",
                "morning": "16:30 - 19:00 Uhr",
            },
            {
                "day": "friday",
                "morning": "09:00 - 18:00 Uhr",
            },
            {
                "day": "saturday",
                "morning": "geschlossen",
            }, 
            {
                "day": "sunday",
                "morning": "geschlossen",
            }
        ];

I played around with splice and pop, but not getting anywhere intended.

4 Answers 4

6

Use javascript shift and push function push function adds the element in last and shift function removes and returns the first element

days.push(days.shift());
Sign up to request clarification or add additional context in comments.

Comments

6

You need to remove the element from the beginning of the array. Array#shift does this. Then you need to add that element to the end of the array. Array#push does that. Since shift returns the element shifted, you can do it in one call:

days.push(days.shift());

The key functions for affecting the first and last elements of an array are:

  • shift: remove an element from the beginning of the array
  • unshift: add an element to the beginning of the array
  • pop: remove an element from the end of the array
  • push: add an element to the end of the array

1 Comment

Thankyou.. Worked perfectly.. Since all of you posted the same answer, i accepted the one with the lowest reputation..
5

How about this:

days.unshift(days.pop())

EDIT, after op edit:

days.push(days.shift())

2 Comments

I want the popped out 0th index to be inserted at the last...:(
:) In cases like these, accept the answer from the guy(or gal) with the lowest reputation.
3

Try this (not tested) :

var days= [
            {
                "day": "sunday",
                "morning": "geschlossen"
            },
            {
                "day": "monday",
                "morning": "geschlossen"
            },
            {
                "day": "tuesday",
                "morning": "geschlossen"
            },
            {
                "day": "wenesday",
                "morning": "geschlossen"
            },
            {
                "day": "thursday",
                "morning": "16:30 - 19:00 Uhr"
            },
            {
                "day": "friday",
                "morning": "09:00 - 18:00 Uhr"
            },
            {
                "day": "saturday",
                "morning": "geschlossen"
            }
        ];


var first = days[0];
days.shift();
days.push(first);
console.log(days);

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.