最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin7浏览0评论

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 badges
Add a ment  | 

4 Answers 4

Reset to default 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());

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

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);
发布评论

评论列表(0)

  1. 暂无评论