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.
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.
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.
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.
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.
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With