Creating array of arrays with JavaScript - javascript

I have the following code, which when run gives error: TypeError: Cannot read property 'split' of undefined
Here you can run it.
var myData = "some1,some2,some3\nsome4,some5,some6\nsome7,some8,some9";
var arrayed = myData.split('\n');
var columns = arrayed.length;
var urlArray = new Array(columns);
console.log(arrayed);
var newarrayed = arrayed.split(',');
console.log(newarrayed);
I have myData array, I want to convert it to an array of arrays, splitting first at \n to seperate arrays, and second at , to create the items inside the arrays. so this list would be like:
[[data1, data2, data3], [data4, data5, data6], [data7, data8, data9]]
console.log(arrayed); does something similar, but when I try to access it using arrayed[0][0], it gives me just the first letter.

You're not splitting the strings correctly. You try to split them twice, but the second time fails because you are calling split on an array, not a string. Try looping over them instead.
var myData = "some1,some2,some3\nsome4,some5,some6\nsome7,some8,some9";
var arrayed = myData.split('\n');
var columns = arrayed.length;
var urlArray = new Array(columns);
console.log(arrayed);
var newarrayed = [];
for (var i in arrayed) {
newarrayed.push(arrayed[i].split(','));
}
console.log(newarrayed);

Related

Get all match values from two comma separated string

I have two comma separated string as follows,
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
I need to create another string based on the above two,
if hiddenString have unmatching value with strB,then without those unmatched values need to create e new string and also avoid duplicates.
simply says, I need to get all the matching values from both strings.
As the example based on my two string, I'm expecting the following:
varFinalHiddenString = 14172,10062,100;
How can I do this using JavaScript and that should work in safari and IE 11 or its earlier versions. Please help me, I'm new to the JS.
You can first split() strings to generate arrays from them. Then filter() the smaller array by checking the index of the current item with indexOf() in other array:
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var temp1 = hiddenString.split(',');
var temp2 = strB.split(',');
var varFinalHiddenString = temp2.filter(function(s){
return temp1.indexOf(s) > -1;
}).join(',');
console.log(varFinalHiddenString);
Make arrays of the strings, then use the "filter" method. Then convert back to string.
var hiddenString = '14172,10062,14172,14172,100,10,14172,15000,12000';
var strB = '14172,10062,10064,10025,100,14182';
var hiddenStringAsArray = hiddenString.split(',');
var strBArray = strB.split(',');
var resultObject = $(strBArray).filter(hiddenStringAsArray);
var resultArray = resultObject.toArray();
var resultString = resultArray.join(',');
console.log(resultString);

Getting last item in array using javascript

I'm trying to get the last item in a array using JavaScript.
But I'm always getting all items in the array.
So far, I've tried several methods. Here is my code:
var pathCoords = ['1','2','3','4','5'];
var sample1 = pathCoords[pathCoords.length -1];
var sample2 = pathCoords.slice(-1)[0];
console.log(sample1, sample2);
Maybe You can use splice method of array like this :
var sample = pathCoords.splice(pathCoords.length-1);

Undefined push when trying to insert to array

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?
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.
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.
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);

Not Able to Push Arrays into Array (Making Array of Arrays)

can you please take a look at this snippet and let me know why I am not able to create an array of array at this example?
var data1 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data2 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data3 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data4 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var all = [];
all[0].push(data1);
all[1].push(data2);
all[2].push(data3);
all[3].push(data4);
var myJsonString = JSON.stringify(all);
console.log(myJsonString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You should be doing all.push(dataX) since all[X] is undefined.
var data1 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data2 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data3 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var data4 = [5555,22,102858,12,.554,88888,99999999,12,1.5];
var all = [];
all.push(data1);
all.push(data2);
all.push(data3);
all.push(data4);
var myJsonString = JSON.stringify(all);
console.log(myJsonString);
Explanation:
In the code you wrote, all is an empty Array.
all[X] (e.g. all[0]) refers to the first element of the array, which doesn't exist (since array is empty). JavaScript interprets this as undefined. (There is no array out of bounds exception)
undefined has no method .push(...) which is why your code is failing.
What you want is to call all.push(...) where you're calling .push(...) on the all Array.
all[0].push(data1);
all[0] retrieves the first element of all. Since you just made the array and it's empty, this is undefined. (In other languages, it would be an array index access violation). You're then trying to push onto that first element; if it were an array itself, this would give you [[[5555,22,... which is one more array container than you want.
Easiest solution is to construct the array via a literal.
var all = [data1, data2, data3, data4];
// no "push()" necessary here.

Convert string to become variable

I have multiple variables containing JSON as string (received from AJAX).
data.output_data_1234
data.output_data_5678
I convert them to Array:
var outputdataarr = new Array(data.output_data_1234);
This works fine, but how do I add a number to the var name:
var outputdataarr = new Array('data.output_data_'+formid+'');
this one does not work.
formid contains a proper number.
This does not work too:
var outputvar = window['data.output_data_' + formid];
var outputdataarr = new Array(outputvar);
Please help. Thanks.
You probably mean, you need something like this:
var outputdataarr = new Array(data['output_data_'+formid]);
You can only use string in square brackets as an object field identifier. It cannot contain '.'.
UPDATE:
However, you will probably need a loop to fill the whole array, e.g.
var outputdataarr = new Array();
for (var i=1000; i<2000; i++) {
outputdataarr.push(data['output_data_'+formid]);
}
Use [] instead of new Array is better.
var outputdataarr = [];
outputdataarr.push(data['output_data_'+formid]);
//and so on

Categories