1

I'm new to Javascript, I wanted to push all the data inside my text file to array, it works fine I do this:-

var pokemon_array = [];
$.get('pokemonlist.txt', function(data) {
        var lines = data.split('\n');

        for(var i=0;i<lines.length;i++) {
            var arr = lines[i].split('"');

            pokemon_array.push(arr[1]);
            pokemon_array.push(arr[3]);
            pokemon_array.push(arr[4]);
        }
        console.log(pokemon_array);
    });

I wanted it to have 2 dimensional array so I put this:-

var pokemon_array = [];
$.get('pokemonlist.txt', function(data) {
        var lines = data.split('\n');

        for(var i=0;i<lines.length;i++) {
            var arr = lines[i].split('"');

            pokemon_id = arr[1];
            pokemon_img = arr[3];
            pokemon_name = arr[4];

            pokemon_array[i].push(pokemon_id);
            pokemon_array[i].push(pokemon_img);
            pokemon_array[i].push(pokemon_name);
        }
        console.log(pokemon_array);
    });

Then I received this error:-

Uncaught TypeError: Cannot read property 'push' of undefined

Anything I did wrong here?

1
  • 1
    pokemon_array[i] is not an array, you'll need to define it as such first pokemon_array[i] = []; pokemon_array[i].push(pokemon_id); ..... Commented Aug 18, 2016 at 4:19

3 Answers 3

3

Change

pokemon_array[i].push(pokemon_id);
pokemon_array[i].push(pokemon_img);
pokemon_array[i].push(pokemon_name);

to

pokemon_array.push([ pokemon_id, pokemon_img, pokemon_name ]);

otherwise pokemon_array[i] is not there yet.

Also you'd better either declare your pokemon_... variables with var or just do pokemon_array.push([ arr[1], arr[3], arr[4] ]); if the only use for them is to be added to array.

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

Comments

0

The problem is that pokemon_array[i], which would be pokemon_array[0], pokemon_array[1] etc. was never assigned.

You can change the code to something like:

        pokemon_array[i] = [];
        pokemon_array[i].push(pokemon_id);
        pokemon_array[i].push(pokemon_img);
        pokemon_array[i].push(pokemon_name);

Or

pokemon_array[i] = [pokemon_id, pokemon_img, pokemon_name];

Or you can just write it shorter because you can just use the array you have:

pokemon_array[i] = arr;

But you don't need the [i] at all really, so you can go like:

pokemon_array.push([pokemon_id, pokemon_img, pokemon_name]);

which can be even shorter as mentioned above:

pokemon_array.push(arr);

This does not just append each element in arr. It adds the entire arr as one element of pokemon_array, as you want exactly.

4 Comments

Thanks for the explaination!
No problem. Please see the updated answer about multi-dimensional array in particular. And kindly accept the answer if it solves what you want, or leave another comment telling me what problem you still have. Cheers :)
I got a question, is it possible to add a new value on based on the key? Example:- Instead of array([0]=>abc,[1]=>def); I want to make it based on my pokemon_id variable, so the output will be something like this array(['pikachu']=>abc, ['donkey']=>def) I can't declare the pokemon_array inside the function else it will only be usable inside the function, therefore I declaring the variable outside of the function
If the IDs are unique, you are better off with an object instead of an array. var pokemons = {}; and pokemons[pokemon_id] = {id: pokemon_id, img: pokemon_img, name: pokemon_name};.
0

You get the error because pokemon_array[i] is undefined

pokemon_array[i].push(pokemon_id);
pokemon_array[i].push(pokemon_img);
pokemon_array[i].push(pokemon_name);

to

pokemon_array.push(pokemon_id);
pokemon_array.push(pokemon_img);
pokemon_array.push(pokemon_name);

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.