Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push first object to last in javascript array keeping all other values intact

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.

like image 358
Roy M J Avatar asked Nov 13 '13 10:11

Roy M J


People also ask

Does push modify the original array?

push() adds item(s) to the end of an array and changes the original array. unshift() adds an item(s) to the beginning of an array and changes the original array. splice() changes an array, by adding, removing and inserting elements. slice() copies a given part of an array and returns that copied part as a new array.

How do you push an object into an array at first position JavaScript?

Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to push() method but it adds an element at the beginning of the array.

Can you push an object into an array JavaScript?

The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method. The object is hence added to the end of the array.

Does push add to front or back of array?

The push() method is similar to the unshift() method as it adds an element to the end of an array rather than the beginning. It returns the length of the new array and, like the unshift() method, can be used to add more than one element.


1 Answers

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());
like image 89
Sikander Avatar answered Nov 14 '22 23:11

Sikander