dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Javascript array related question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    Resolved [RESOLVED] Javascript array related question

    I have a project where I need to take the results of a query and populate an array of sorts. The query is returned into the var results
    Code:
    avLog(JSON.stringify(results[i]));
    The line of code above logs the first record returned by said query. The result looks like
    {"Allow_Upc":"N","Allow_Whitem":"Y","Commodity":"Produce","Department":"129","DepartmentName":"Produ ce","DepartmentTypeCode":"S","DisplayFlag":"N","MenuLevel":"C","ParentDepartmentCode":"7","Partition Code":"64","PosDept":"29","SequenceNumber":"3"}
    I am looking to put that data into an array base on the following
    Code:
    // Define Department property default values
    Department.prototype.Menulevel = null;
    Department.prototype.Department = null;
    Department.prototype.DepartmentName = null;
    Department.prototype.DepartmentTypeCode = null;
    Department.prototype.ParentDepartmentCode = null;
    Department.prototype.SequenceNumber = null;
    Department.prototype.Commodity = null;
    Department.prototype.PartitionCode = null;
    Department.prototype.PosDept = null;
    Department.prototype.DisplayFlag = null;
    Department.prototype.NextLevel = null;
    Department.prototype.AllowWhitem = null;
    Department.prototype.AllowUpc = null;
    The problem is that my JavaScript skills are a bit weak and I have never did this before so I could use a push in the right direction.

    Thanks

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,293

    Re: Javascript array related question

    Well first that's not an array. results[] is an array, but that's where the array usage ends. What you posted as a result is JSON - JavaScriptObjectNotation ... it's a portable way of sending objects around, that's what the JSON in the JSON.stringfy code does. That said, I've only ever used the JSON.stringify call when I'm trying to see the JSON of an object... I don't actually use it to create the JSON to send... that is normally done by the service itself automatically ... so I wonder if all you need is
    Department.prototype = results[1]; ... maybe... it depends. What isn't clear is where results came from and where it's going.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    Re: Javascript array related question

    Quote Originally Posted by techgnome View Post
    Well first that's not an array. results[] is an array, but that's where the array usage ends. What you posted as a result is JSON - JavaScriptObjectNotation ... it's a portable way of sending objects around, that's what the JSON in the JSON.stringfy code does. That said, I've only ever used the JSON.stringify call when I'm trying to see the JSON of an object... I don't actually use it to create the JSON to send... that is normally done by the service itself automatically ... so I wonder if all you need is
    Department.prototype = results[1]; ... maybe... it depends. What isn't clear is where results came from and where it's going.
    -tg
    I will definitely give that a try and see what happens.

    The data is coming from a sqllite database and the prototype is used later in the code as well as 2 or 3 others which are later referenced to build another that contains a combination of parts from the others. Given that I have never did anything quite like this before I really had no idea where to begin and yes the json.stringfy was just to see the content of result.

    The array comes in my thing anyway because I am thinking that I need an array of Department and that would hold the records returned by the query in the named properties of Department fr later use. Of course I could be totally wrong in that assumption.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    Re: Javascript array related question

    Perhaps there is some other method I should be using. I basically just need to get the data from each of those fields for each record into the prototype but not sure how. The code was written by someone else and has been moved to a new platform where some of the functions used do not work and thus needs to be modified.

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,894

    Re: Javascript array related question

    Thanks for the info techgnome I think this is the push I needed.

    I tried a couple of different things then found something that seemed to work.
    Code:
    var results = db.executeSql('select * from Department', []);	
    var department=new Department();
    department=results[0];
    console.log('result=' + department.DepartmentName);
    When executed department.DepartmentName looks to have the expected data within so hopefully I can run with it from here.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width