-1

I have an array like this:

data: Array[8]
0: 3054
1: 1986
2: 1065
3: 1053
4: 666
5: 1423
6: 137
7: NaN

and I want to shrink this array to the following in order to remove NAN so I want something like this:

data: Array[7]
0: 3054
1: 1986
2: 1065
3: 1053
4: 666
5: 1423
6: 137

according to this page : link

I use splice as follow:

allData[i].splice(allData[i].length-1, 1)

but it removes all element of array except NAN.

Can anyone help me what is my problem?

Marc B
  • 356,200
  • 43
  • 426
  • 500
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
  • 2
    Just use `allData[i].slice(0, -1)` – dan-lee Jun 02 '14 at 14:55
  • Will it always be the last single element you want to remove? – musefan Jun 02 '14 at 14:55
  • 1
    Skip w3fools. They're useless. Try this instead: http://stackoverflow.com/a/9815010/118068 – Marc B Jun 02 '14 at 14:56
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – cristian Jun 02 '14 at 14:57
  • @W3Schools is okay for beginners. They go there learn something quick, find some mistakes and learn that W3schools should never be trusted again. What is wrong with that :D? – Jack_of_All_Trades Jun 02 '14 at 14:58
  • @musefan Yes it is always last element – HMdeveloper Jun 02 '14 at 15:00
  • splice removes the last element in place, the array now has only the first seven values. But what splice returns is an array of the removed elements- in your case, NaN. ignore the return and check the array... – kennebec Jun 02 '14 at 15:01
  • Using `.slice` on the assumption that the last array value will always be `NaN` is setting you up to fail because it's brittle. If the problem is "to remove any array values that are not a number" then you should filter the array. – i_like_robots Jun 02 '14 at 15:03
  • Is `allData` an Array of Arrays? Otherwise I can't figure out why you're doing `allData[i].splice...` instead of `allData.splice...`. If it is an Array of Arrays, then your code should work, and you're probably storing the return value. – cookie monster Jun 02 '14 at 15:11
  • Are you doing `allData[i] = allData[i].splice(allData[i].length-1, 1)` in your code? If so, that's the actual source of the problem. Just get rid of the `allData[i] =` part – cookie monster Jun 02 '14 at 15:22

3 Answers3

3

If it's only the last value you want to remove, you can adjust the length of the array:

someArray.length = someArray.length-1;

KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

Use slice with a negative value to strip the end away:

allData[i].slice(0, -1)

Reference from docs:

As a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts the third element through the second-to-last element in the sequence.


But beware that the actual array is not touched, as slice clones the array.

So if you want to manipulate the actual array you need to reassign it like this:

allData[i] = allData[i].slice(0, -1)
Hans Z
  • 4,664
  • 2
  • 27
  • 50
dan-lee
  • 14,365
  • 5
  • 52
  • 77
  • This doesn't remove anything from the Array. It creates a new one. If this worked for the OP, then the actual issue is likely that he's storing the return value of `.splice()`. – cookie monster Jun 02 '14 at 15:14
  • Just a quick question, I am wondering why is it working with array of different size? (As far as I understood 0 means the first element and -1 means the second so how it works?) – HMdeveloper Jun 02 '14 at 15:18
  • @cookiemonster You're right, it clones the actual array. – dan-lee Jun 02 '14 at 15:21
  • 1
    @HamedMinaee Negative value means that it starts looking from the end. So it takes the first from the end. – dan-lee Jun 02 '14 at 15:21
  • `var allData[i] =` should be `allData[i] =`, but it's still an issue if there are other references to the original array, since they'll now be stale. OP's original code was a correct way to mutate the existing array. – cookie monster Jun 02 '14 at 15:28
1

If you want to remove all NaN elements, you can use a filter instead.

allData[i] = allData[i].filter(function(e) {
    return !isNaN(e);
});
Hans Z
  • 4,664
  • 2
  • 27
  • 50
  • This doesn't remove anything from the Array. It creates a new one. – cookie monster Jun 02 '14 at 15:12
  • Which is useless if there are other references to the original array. Point is that he's asking how to remove an item from the array, and this doesn't do it. Not to mention that his original code is correct. The described issue sounds like he's assigning the return value of `.splice()`, which of course would be wrong. – cookie monster Jun 02 '14 at 15:29
  • @cookiemonster "Which is useless if there are other references to the original array" wtf are you talking about where are the other references – Hans Z Jun 02 '14 at 15:36
  • Do we not know the definition of ***" if "***? Doesn't matter. You're not mutating the Array. Your answer is an alternate approach that may or may not be suitable depending on the situation, and doesn't explain what the actual issue in the question was. – cookie monster Jun 02 '14 at 16:26
  • This is an alternate solution that other people looking up this question could use, and it works in the context of the question. Why all the hate, bro? – Hans Z Jun 02 '14 at 17:05
  • The limited context provided in the question suggests mutation of an Array instead of replacement with a new one. A critique isn't hate. I just don't think it's a very good answer to the question. – cookie monster Jun 02 '14 at 17:57