...

View Full Version : trouble with array



bolo77
12-23-2011, 04:16 PM
Hi i start of with a some csv data and convert this into an array. The resulting array looks like the following

[["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]]

But i need this to be formatted as below without the quotes around the second value.

[["Apple", 64], ["Banana", 20], ["pear", 12], ["Orange", 16]]

Im not sure how to achieve this. I guess i need to loop through the array and strip out the quotes but im failing to get this working. Help!

xelawho
12-23-2011, 04:47 PM
I guess i need to loop through the array and strip out the quotes

that's right - what you have are strings and what you need are numbers. Try this:



<script type = "text/javascript">

var fruits = [["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]]

for(var j = 0; j<fruits.length; j++){
fruits[j][1]=Number(fruits[j][1]);
alert(typeof(fruits[j][1]));
}
</script>

rnd me
12-23-2011, 05:17 PM
in the row loop that does your second spit (to make cells):


row=row.map(function(a){
return isFinite(a) ? +a : a;
})

bolo77
12-23-2011, 06:23 PM
Hi I used the solution from xelawho and this worked perfectly, i cant thank you enough :)

bolo77
01-08-2012, 01:32 PM
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?



<script type = "text/javascript">

var fruits = [["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]]

for(var j = 0; j<fruits.length; j++){
fruits[j][1]=Number(fruits[j][1]);
alert(typeof(fruits[j][1]));
}
</script>



Thanks for your help!

xelawho
01-08-2012, 02:51 PM
so just start the loop at 1 (the array starts at 0, so you skip the first item):



<script type = "text/javascript">

var fruits = [["Apple", "64"], ["Banana", "20"], ["pear", "12"], ["Orange", "16"]]

for(var j = 1; j<fruits.length; j++){
fruits[j][1]=Number(fruits[j][1]);
}

for(var k = 0; k<fruits.length; k++){
alert(typeof(fruits[k][1]));
}
</script>

oVTech
01-08-2012, 03:05 PM
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:



function oneach(arr, fn) {
var i = 1, l = arr.length;
for (i, l; i < l; i += 1) {
fn(arr[i]);
}
}

var fruits = [
["Apple", "64", "8"],
["Banana", "20", "19", "32", "55"],
["pear", "12", "7", "2"],
["Orange", "16", "65", "88"]
];

var i = 0;
var aWrapLen = fruits.length;

for (i; i < aWrapLen; i += 1) {
oneach(fruits[i], function(item) {
item = Number(item);
console.log(item + ": " + typeof item);
});
}

bolo77
01-08-2012, 03:25 PM
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.



// 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);


});

}

xelawho
01-08-2012, 05:25 PM
sorry I misread your question before. not enough coffee obviously. you could also try this:



<script type = "text/javascript">

var fruits = [["Apple", "64"], ["Banana", "20"], ["pear", "12","10"], ["Orange", "16","20"]]

for(var j = 0; j<fruits.length; j++){
for (x in fruits[j]){
if (x==0){
continue;
}
fruits[j][x]=Number(fruits[j][x]);
alert(typeof(fruits[j][x]));

}
}

</script>

bolo77
01-08-2012, 05:34 PM
Thanks xelawho works perfectly :)

oVTech
01-08-2012, 07:02 PM
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.



Here is how you could do it with my method:



var fruits = [
["Apple", "64", "8"],
["Banana", "20", "19", "99"],
["pear", "12", "7", "2"],
["Orange", "16", "65", "33"]
];

function modify(arr) {
for (var i = 1; i < arr.length; i += 1) {
arr[i] = parseFloat(arr[i]);
}
return arr;
}


for (var i = 0; i < fruits.length; i += 1) {
fruits[i] = modify(fruits[i]);
}

console.log( fruits );



@xelawho

I am not sure how this line works if (x==0){ continue; }. How are these strings equal to 0?

["Apple"], ["Banana"], ["pear"], ["Orange"] :confused:

bolo77
01-08-2012, 07:09 PM
Hi oVTech that works as welll. So which method is more effecient considering both work does it matter which one i use?

Thanks

oVTech
01-08-2012, 07:27 PM
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 .

xelawho
01-08-2012, 09:33 PM
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:



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:



for(var j = 0; j<fruits.length; j++){
for(var x = 1; x<fruits[j].length; x++){
fruits[j][x]=Number(fruits[j][x]);
}
}

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.

bolo77
01-09-2012, 02:19 AM
Thanks for the explanation guys!

The final issue im having is converting a variables value from

fresh frozen Damaged Unknown

to
"fresh" "frozen" "Damaged" "Unknown"

It needs to be in this form to allow spaces to be included in each variable name (example "fresh foods" "frozen foods".)

any ideas how to do this?

xelawho
01-09-2012, 03:54 AM
I suspect I don't get it, but are you talking about splitting a string wherever there is a space?



str="fresh frozen Damaged Unknown"
arr=str.split(" ")
for(var x = 0; x<arr.length; x++){
alert(arr[x])
}
</script>

bolo77
01-09-2012, 01:08 PM
Hi xelawho i needed to do this.



var List = new Array(); // new array
List = String.split('\n') ; // split at \n
// console.log(String);
// console.log(List);

// join the list
var arr = List;
var List1 = '\"' + arr.join('\"\n\"') + '\"';
// the List data is now in the format needed



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum