0

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.

6
  • Can you give an example of what your array would look like before the for loops and then what it would look like after? Commented Nov 22, 2013 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]] Commented Nov 22, 2013 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? Commented Nov 22, 2013 at 2:06
  • I just want to edit the first element in each array and then delete every 13th array. Commented Nov 22, 2013 at 2:11
  • Which is what my code below does. Commented Nov 22, 2013 at 2:13

1 Answer 1

1

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 :)

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

4 Comments

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.
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 :)
Try it with this array: var inputData = [[1990,2808603],[1990,2805747],[1990,2818988],[1990,2823120],[1990,2845060],[1990,2870665],[1990,2871062],[1990,2847298],[1990,2825344],[1990,2829529],[1990,2818244],[1990,2802949],[1990,2830551]]; I am still getting every year = 1990.0833333
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.

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.