Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-23-2011, 03:16 PM   PM User | #1
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
trouble with array

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!
bolo77 is offline   Reply With Quote
Old 12-23-2011, 03:47 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by bolo77 View Post
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:

Code:
<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>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
bolo77 (12-23-2011)
Old 12-23-2011, 04:17 PM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
in the row loop that does your second spit (to make cells):

Code:
row=row.map(function(a){
  return isFinite(a) ? +a : a;
})
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 12-23-2011, 05:23 PM   PM User | #4
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
thank you

Hi I used the solution from xelawho and this worked perfectly, i cant thank you enough
bolo77 is offline   Reply With Quote
Old 01-08-2012, 12:32 PM   PM User | #5
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
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?

Code:
<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!
bolo77 is offline   Reply With Quote
Old 01-08-2012, 01:51 PM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
so just start the loop at 1 (the array starts at 0, so you skip the first item):

Code:
<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>
xelawho is offline   Reply With Quote
Old 01-08-2012, 02:05 PM   PM User | #7
oVTech
Regular Coder

 
oVTech's Avatar
 
Join Date: Nov 2010
Location: USA
Posts: 296
Thanks: 4
Thanked 54 Times in 52 Posts
oVTech is an unknown quantity at this point
Quote:
Originally Posted by bolo77 View Post
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(arrfn) {
    var 
1arr.length;
    for (
ill+= 1) {
        
fn(arr[i]);
    }
}

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

var 
0;
var 
aWrapLen fruits.length;

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

__________________




I don't know, I don't care, and it doesn't make any difference!
-Albert Einstein-



oVTech is offline   Reply With Quote
Users who have thanked oVTech for this post:
bolo77 (01-08-2012)
Old 01-08-2012, 02:25 PM   PM User | #8
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
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
bolo77 is offline   Reply With Quote
Old 01-08-2012, 04:25 PM   PM User | #9
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
sorry I misread your question before. not enough coffee obviously. you could also try this:

Code:
<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>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
bolo77 (01-08-2012)
Old 01-08-2012, 04:34 PM   PM User | #10
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
Thanks xelawho works perfectly
bolo77 is offline   Reply With Quote
Old 01-08-2012, 06:02 PM   PM User | #11
oVTech
Regular Coder

 
oVTech's Avatar
 
Join Date: Nov 2010
Location: USA
Posts: 296
Thanks: 4
Thanked 54 Times in 52 Posts
oVTech is an unknown quantity at this point
Quote:
Originally Posted by bolo77 View Post
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:
PHP Code:

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

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



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


console.logfruits ); 

@xelawho

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

["Apple"], ["Banana"], ["pear"], ["Orange"]
__________________




I don't know, I don't care, and it doesn't make any difference!
-Albert Einstein-



oVTech is offline   Reply With Quote
Old 01-08-2012, 06:09 PM   PM User | #12
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
Hi oVTech that works as welll. So which method is more effecient considering both work does it matter which one i use?

Thanks
bolo77 is offline   Reply With Quote
Old 01-08-2012, 06:27 PM   PM User | #13
oVTech
Regular Coder

 
oVTech's Avatar
 
Join Date: Nov 2010
Location: USA
Posts: 296
Thanks: 4
Thanked 54 Times in 52 Posts
oVTech is an unknown quantity at this point
Quote:
Originally Posted by bolo77 View Post
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-



oVTech is offline   Reply With Quote
Old 01-08-2012, 08:33 PM   PM User | #14
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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:

Code:
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.
xelawho is offline   Reply With Quote
Old 01-09-2012, 01:19 AM   PM User | #15
bolo77
New Coder

 
Join Date: Dec 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
bolo77 is an unknown quantity at this point
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?
bolo77 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, javascript, strip quotes

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:35 AM.


Advertisement
Log in to turn off these ads.