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 3 123 LastLast
Results 1 to 15 of 38
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    push array into an array

    Edit: I know they ain't arrays after

    I have an array var sales = { }; which I need to make look like the following var sales = { "10" : { "KR" : 0, "LR" : 0 }};.

    How would I create a new key and then push the products array into it?

    This is what I have so far but I can't figure out how to add the array with the key of what every "num" is in the function args.

    PHP Code:
    var products = { "KR" 4"LR" };
    var 
    sales = { };

    function 
    createArray(num) {
        if (!
    sales[num]) 
        {
            for (
    x in products)
            {
                
                
    //fruits.push("Kiwi")
            
    }
        }

    So for every NEW number that is sent to the function, a new key would be made with the products array, so eventually something like this could end up being made:
    var sales = { "710" : { "KR" : 0, "LR" : 0 }, "711" : { "KR" : 0, "LR" : 0 }, "712" : { "KR" : 0, "LR" : 0}, "713" : { "KR" : 0, "LR" : 0 }};
    Last edited by martynball; 07-30-2013 at 10:51 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    {} is not Array but an Object. You append an item to an object by:

    myObject[key] = value;

    If the key exists, it will overwrite the existing one. If not, then it will add the item.

  • Users who have thanked glenngv for this post:

    martynball (07-30-2013)

  • #3
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Shouldn't this work?

    Code:
    function createArray(num) {
    	if (!sales[num]) 
    	{
    		for (x in products)
    		{
    			sales[num] = { x : 0 };
    		}
    	}
    }
    I can't figure out how to create the array I want!

  • #4
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Ah, this seems to work, look right to you?

    PHP Code:
    function createArray(num) {
        
    //Has the array been made?
        
    if (!sales[num]) 
        {
            
    //Create the array and populate with products, setting them to 0
            
    for (x in products)
            {
                
    temp[x] = 0;
                
    sales[num] = temp;
            }
        }
        
    //Debugging div
        
    document.getElementById("test").innerHTML sales[num]["CC"];


  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    No, that is not right.

    Where does temp come from?

    Just add it the SAME WAY you retrieve it:
    Code:
    if ( sales[num]["CC"] == null ) sales[num]["CC"] = someValue;
    If you omit the "if" test, then if the value already exists, it will be overwritten.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    martynball (07-30-2013)

  • #6
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    The product object will be editable so the sales object needs to be flexible, so I can't define sales sales[num]["CC"] = someValue;

    The way I posted above seems to work perfect :S

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If you say so. I sure don't see where temp is coming from, though, or what it's purpose is.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    there is no need to use temp at all

    for (x in products) {sales[num][x] = 0;}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    martynball (07-31-2013)

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    Quote Originally Posted by martynball View Post
    The product object will be editable so the sales object needs to be flexible, so I can't define sales sales[num]["CC"] = someValue;
    unless you freeze an object or declare a property non-writable, you can always modify it, so it is flexible by default.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    martynball (07-31-2013)

  • #10
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Nope that returns the error:
    Unable to set property "KR" of undefined or null reference

    Just noticed my function actually isn't working..
    Last edited by martynball; 07-31-2013 at 04:50 PM.

  • #11
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    then you pass the wrong num into the function.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #12
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Here is my function:
    Code:
    function createArray(num) {
            alert(num);
    	//Has the array been made?
    	if (!sales[num]) 
    	{
    		//Create the array and populate with products, setting them to 0
    		for (x in products) 
    		{
    			temp[x] = 0;
    			sales[num] = temp;
    		}
    	}
    }
    Here are the "num" variable that went was alerted: 712
    So there is a number and it is the correct one.

    See the below video of it acting weird, I display the object using:
    alert(JSON.stringify(sales))

    http://www.youtube.com/watch?v=WxRH8L8K1hc

    You see how it seems to change the values of the other ID when I change a different one.

  • #13
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Do you want to make something like a new record data of database ?
    I'm not quite understand what's you want from your code example code.

    Code:
    function Record(DB, Column, ID) {
        //Input new ID.
        this.ID = +ID;
        if (ID == "") this.ID = +DB[DB.length - 1].ID + 1;
    
        //Add column to this new row.
        for (Item in Column) this[Column[Item]] = 0;
    }
    
    function CreateRecord(DB, Column, ID) {
        //Check duplicate record
        if (ID != "") for (Item in DB) if (+DB[Item].ID == +ID) {
            return null;
            break;
        };
    
        DB.push(new Record(DB, Column, ID));
    }
    
    var Fruits = ["Apple", "Orange", "Melon"];
    var DB = [];
    
    CreateRecord(DB, Fruits, "101");
    CreateRecord(DB, Fruits, "");
    CreateRecord(DB, Fruits, "101");
    
    /*
    It will return DB = [ {ID: 101, Apple:0, Orange:0, Melon:0}, {ID: 102, Apple:0, Orange:0, Melon:0}  ]
    */

  • #14
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    The code I posted returns more or less the same as your code does, there is just some weird issue somewhere. It seems to me like the ID is being passed in as a string and not number.

  • #15
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,133
    Thanks
    12
    Thanked 332 Times in 328 Posts
    shouldn’t really matter as JS is a weakly typed language.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

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