简体   繁体   English

在JavaScript中删除数组的第一个元素/数组

[英]Remove first element/array of an array in JavaScript

I have a problem with removing the first element of an array. 我在删除数组的第一个元素时遇到问题。

To be short, this is how my array looks like if I show it in console: 简而言之,这就是我在控制台中显示数组时的样子:

(11) [Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4), Array(4)]
0 : (4) ["2017-09-20T16:00:00-07:00", 188.125, 0, 1]
1 : (4) ["2017-09-20T17:00:00-07:00", 123.125, 0, 1]
2 : (4) ["2017-09-20T18:00:00-07:00", 114.25, 0, 1]
3 : (4) ["2017-09-20T19:00:00-07:00", 115, 0, 1]
4 : (4) ["2017-09-20T20:00:00-07:00", 113.25, 0, 1]
5 : (4) ["2017-09-20T21:00:00-07:00", 115.625, 0, 1]
6 : (4) ["2017-09-20T22:00:00-07:00", 114.75, 0, 1]
7 : (4) ["2017-09-20T23:00:00-07:00", 114, 0, 1]
8 : (4) ["2017-09-21T00:00:00-07:00", 112.625, 0, 1]
9 : (4) ["2017-09-21T01:00:00-07:00", 108.375, 0, 1]
10 : (4) ["2017-09-21T02:00:00-07:00", 111.125, 0, 1]
length : 11
__proto__ : Array(0)

I want to remove the first one, 0 , I tried using .shift() but didn't work. 我想删除第一个, 0 ,我尝试使用.shift()但是没有用。

Like my array is called myArray.data and I tried myArray.data.shift() and I get this error message: 就像我的数组叫做myArray.data ,我尝试了myArray.data.shift()并收到以下错误消息:

TypeError: Cannot read property '0' of undefined
    at bundle.js:1554
    at Function._.map._.collect (vendor.js:11761)
    at renderChart (bundle.js:1547)
    at bundle.js:1611
    at Scope.$digest (vendor.js:34716)
    at Scope.$apply (vendor.js:34986)
    at bundle.js:259
    at vendor.js:14387
    at _fulfilled (vendor.js:13992)
    at self.promiseDispatch.done (vendor.js:14021)

Any ideas how to solve this? 任何想法如何解决这个问题?

Later edit: 以后编辑:

The code is inside a chart generation function, this is the whole snippet: 该代码在图表生成函数中,这是完整的代码段:

data: {
    type: chartType,
    columns: [
        ['x'].concat(_.map(myArray.data, function (dataPoint) {

        const x = (myArray.data).shift();
        console.log(myArray.data);          
        console.log(x);

            return moment(dataPoint[0]).valueOf();
        })),
        ['Expected'].concat(_.map(myArray.data, function (dataPoint) {
            return dataPoint[1];
        })),
        ['Actual'].concat(_.map(myArray.data, function (dataPoint) {
            return dataPoint[1];
        }))
    ],
    x: 'x'
},

I can reproduce the result wanted using Array.shift() 我可以使用Array.shift()重现所需的结果

 let arr = [ ["2017-09-20T16:00:00-07:00", 188.125, 0, 1], ["2017-09-20T17:00:00-07:00", 123.125, 0, 1], ["2017-09-20T18:00:00-07:00", 114.25, 0, 1], ["2017-09-20T19:00:00-07:00", 115, 0, 1], ["2017-09-20T20:00:00-07:00", 113.25, 0, 1], ["2017-09-20T21:00:00-07:00", 115.625, 0, 1], ["2017-09-20T22:00:00-07:00", 114.75, 0, 1], ["2017-09-20T23:00:00-07:00", 114, 0, 1], ["2017-09-21T00:00:00-07:00", 112.625, 0, 1], ["2017-09-21T01:00:00-07:00", 108.375, 0, 1], ["2017-09-21T02:00:00-07:00", 111.125, 0, 1] ]; console.log(arr[0]); arr.shift(); console.log(arr[0]); 

You can remove the first array item usingz myArray.data.splice(0, 1) but the error tells that you are trying to apply array method to undefined value. 您可以使用z myArray.data.splice(0, 1)删除第一个数组项myArray.data.splice(0, 1)但该错误表明您正在尝试将数组方法应用于未定义的值。 Perhaps the $scope has not received the value yet 也许$ scope尚未收到该值

.shift() is working fine. 

问题出在要移动的Array中,请检查myArray.data是否包含数据,Error表示您正在尝试从未定义(空)对象中移动值。

splice(0,1) also working fine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM