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.
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.
Share Improve this question asked Nov 13, 2013 at 10:18 Roy M JRoy M J 6,9387 gold badges53 silver badges79 bronze badges4 Answers
Reset to default 6Use 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());
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 arrayunshift
: add an element to the beginning of the arraypop
: remove an element from the end of the arraypush
: add an element to the end of the array
How about this:
days.unshift(days.pop())
EDIT, after op edit:
days.push(days.shift())
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);