Hi i wanted to take this slightly further and im unsure how to achieve this.
The array length is of a variable length. so for example it might be
[["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]] but could also be
[["Apple", "64","20"], ["Banana", "20","30"], ["pear", "12","10"], ["Orange", "16","20"]]
what i need to do is convert the type to number for all entries other than the first. What ammendments would i need to make to the following to achieve this?
Hi i wanted to take this slightly further and im unsure how to achieve this.
The array length is of a variable length. so for example it might be
[["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]] but could also be
[["Apple", "64","20"], ["Banana", "20","30"], ["pear", "12","10"], ["Orange", "16","20"]]
what i need to do is convert the type to number for all entries other than the first. What ammendments would i need to make to the following to achieve this?
Thanks for your help!
This should do the trick. For each inner Array it starts counting a [1] and keeps going until it hits the end. So even if one inner Array has 5 elements and another one has 10, it will still get to all the elements on each Array.
Give it a try and let me know if this works for you:
PHP Code:
function oneach(arr, fn) {
var i = 1, l = arr.length;
for (i, l; i < l; i += 1) {
fn(arr[i]);
}
}
Hi OvTech, much appreciated, your solution has definately helped. now i just need to fidure out how to rewrite the values with their type changed back to the main array.
Code:
// change value field to number from string
function oneach(arr, fn) {
var i = 1, l = arr.length;
for (i, l; i < l; i += 1) {
fn(arr[i]);
}
}
var csvdata=MyCol.csvdata;
console.log(csvdata);
var i = 0;
var aWrapLen = csvdata.length;
for (i; i < aWrapLen; i += 1) {
oneach(csvdata[i], function(item) {
item = Number(item);
console.log(item + ": " + typeof item);
});
}
Last edited by bolo77; 01-08-2012 at 03:07 PM..
Reason: saw OvTech post after initial submission
Hi OvTech, much appreciated, your solution has definately helped. now i just need to fidure out how to rewrite the values with their type changed back to the main array.
Hi oVTech that works as welll. So which method is more effecient considering both work does it matter which one i use?
Thanks
I don't really know which method is more efficient, however I prefer to understand my code bit by bit so x==0 just seems a little error prone. For example x===0 will not work. I know that JavaScript converts types as needed in the background so maybe that's what is going on with the x===0 .
__________________
I don't know, I don't care, and it doesn't make any difference!
-Albert Einstein-
it's not that they're equal to zero (exactly), it's that x is looping through each item in the fruits[j] array.
so when x==0 it's looking at the first item in those arrays, which is the string that we don't want to turn into a number
it's a kind of lazy way of writing:
Code:
for(var j = 0; j<fruits.length; j++){
for(var x = 0; x<fruits[j].length; x++){
if (x==0){
continue;
}
fruits[j][x]=Number(fruits[j][x]);
}
}
although the for (x in j) construction gives you less control than the for loop, which is why the continue is necessary - if you were using nested loops like the above, it would be cleaner and more sensible simply to write:
so that the inner loop doesn't even start at 0 and that first string is never considered.
they all do the same thing, though - I have no idea which would be more efficient. Writing them out like that, I guess I like the look of the last option, but you are right - the best code is the one that you understand most clearly and can modify yourself later without too many headaches.