5

我有以下 JavaScript 数组:

 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",
            }
        ];

如何更改0th index object数组中的最后一个值?

所以我预期的数组将是这样的:

 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",
            }
        ];

我玩过拼接和流行音乐,但没有达到预期的效果。

4

4 回答 4

6

您需要从数组的开头删除元素。Array#shift做这个。然后您需要将该元素添加到数组的末尾。Array#push这样做。由于shift返回移动的元素,您可以在一次调用中完成:

days.push(days.shift());

影响数组的第一个和最后一个元素的关键函数是:

  • shift: 从数组的开头删除一个元素
  • unshift: 在数组的开头添加一个元素
  • pop: 从数组末尾删除一个元素
  • push: 在数组末尾添加一个元素
于 2013-11-13T10:23:13.847 回答
6

使用javascriptshiftpush函数 push函数在最后添加元素,shift函数删除并返回第一个元素

days.push(days.shift());
于 2013-11-13T10:28:56.970 回答
5

这个怎么样:

days.unshift(days.pop())

编辑,操作编辑后:

days.push(days.shift())
于 2013-11-13T10:20:50.993 回答
3

试试这个(未测试)

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);
于 2013-11-13T10:44:47.640 回答