0

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.

Like my array is called myArray.data and I tried myArray.data.shift() and I get this error message:

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'
},
4
  • What other code do you have? shift shouldn't give you this error I think. Commented Sep 21, 2017 at 10:31
  • @putvande, I added it Commented Sep 21, 2017 at 10:57
  • So, it looks like its complaining about dataPoint[0] Commented Sep 21, 2017 at 11:01
  • 1
    Where is your myArray? I see your snippets and you don't have myArray in any of it. If based on what you are showing you did a myArray.data then you are trying to shift() on an Object. Commented Sep 21, 2017 at 12:14

3 Answers 3

3

I can reproduce the result wanted using 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]);

Sign up to request clarification or add additional context in comments.

1 Comment

In my case it doesn't work... I added another snippet of code. Maybe now it is more clear
1
.shift() is working fine. 

Problem is in your Array which is going to shift, Check whether myArray.data consists of data, Error says you are trying to shift a value from a undefined (null) object.

splice(0,1) also working fine

Comments

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. Perhaps the $scope has not received the value yet

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.