Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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>

  • Users who have thanked xelawho for this post:

    bolo77 (12-23-2011)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    thank you

    Hi I used the solution from xelawho and this worked perfectly, i cant thank you enough

  • #5
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!

  • #6
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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>

  • #7
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    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-




  • Users who have thanked oVTech for this post:

    bolo77 (01-08-2012)

  • #8
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #9
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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>

  • Users who have thanked xelawho for this post:

    bolo77 (01-08-2012)

  • #10
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks xelawho works perfectly

  • #11
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    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-




  • #12
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi oVTech that works as welll. So which method is more effecient considering both work does it matter which one i use?

    Thanks

  • #13
    Regular Coder oVTech's Avatar
    Join Date
    Nov 2010
    Location
    USA
    Posts
    296
    Thanks
    4
    Thanked 54 Times in 52 Posts
    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-




  • #14
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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.

  • #15
    New Coder
    Join Date
    Dec 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •