Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Okay so I have a 2D array that I am trying to alter using javascript. This is what I have so far:

for (var i = 0; i <= inputData.length; i++ {

        inputData[0,0] = inputData[0,0];

        inputData[i,0] = inputData[((i - 1) + 1/12), 0];

I want this to take array [i-1] value and then add 1/12 to it

        for (j = 13; inputData.length; j += 13) {

        delete inputData[j,0];
        delete inputData[j,1];
        }

Also, I want to delete the entire 2D array at every 13th increment value.

    }

This is what I have so far. I am sure there are probably errors within it. Can you guys help me out here? Any help would be greatly appreciated.

share|improve this question
 
Can you give an example of what your array would look like before the for loops and then what it would look like after? –  Seth McClaine Nov 22 '13 at 1:36
 
I am using it to change a year value to represent a month in the year. So var inputData = [[1990, 12345], [1990, 12345] ..... [1990, 12345]] 12345 just represents random data. Each 13th element needs to be removed. At the end, inputData = [[1990.08333333, 12345], [1990.16666, 12345] .... [1991, 12345]] –  user2562125 Nov 22 '13 at 1:38
 
You cannot have multiple indices on one array, you will need to use multiple property accessors like so: inputData[0][0]. Or were you trying to denote a range? –  Bergi Nov 22 '13 at 2:06
 
I just want to edit the first element in each array and then delete every 13th array. –  user2562125 Nov 22 '13 at 2:11
 
Which is what my code below does. –  Aidan Kane Nov 22 '13 at 2:13
show 2 more comments

1 Answer

up vote 1 down vote accepted

Couple of things - you need to be careful when iterating over an array that you're removing from, your indexes will end up offset with respect to your data as soon as you do a delete. Secondly your syntax for deletion is off.

Normally in these situations I favour creating a new array containing the data I want to keep.

var inputData = [[1,1],[2,2],[3,3],[4,4]];
var b = [];
for (i=0; i < inputData.length; i++) {
    if ((i + 1) % 13 != 0) {
        var year_with_month = inputData[i][0] + i * 1/12;
        var e = [year_with_month, inputData[i][1]]
        b.push(e);
    }
}
inputData = b;

Also, given a choice I'd use a library like underscore to make it easy to do the looping. I never manually write for loops anymore, took me a couple of attempts to get that one right :)

share|improve this answer
 
Okay how do I view the new array? Can I print it somehow? I am using this to use to draw a line graph, and it still doesn't look right. –  user2562125 Nov 22 '13 at 2:08
 
I've updated my answer to start with inputData and then to end up with that adjusted so other code will see the new array. Though it's impossible to tell without seeing the rest of your code - and I'm off to bed now :) –  Aidan Kane Nov 22 '13 at 2:12
 
Try it with this array: var inputData = [[1990,2808603],[1990,2805747],[1990,2818988],[1990,2823120],[1990,2845060],[199‌​0,2870665],[1990,2871062],[1990,2847298],[1990,2825344],[1990,2829529],[1990,2818‌​244],[1990,2802949],[1990,2830551]]; I am still getting every year = 1990.0833333 –  user2562125 Nov 23 '13 at 16:45
 
I've taken another stab at trying to understand what you mean but I'm not 100% sure I get it. Each element represents a different month in the year so you want the year to include that component? Anyway, really there's enough of an example here that you should be able to tweak it. –  Aidan Kane Nov 24 '13 at 16:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.