0

I have a class like below;

function Request()
  {
      this.CompanyId;
      this.Password;
      this.SessionId;
      this.UserId;
      this.UserName; 
  }

I create an object and want to get byte array of object;

var request = new Request();

    request.UserName = GlobalProcess.SessionInfo.Server.UserName;
    request.Password = GlobalProcess.SessionInfo.Server.Password;
    request.CompanyId = GlobalProcess.SessionInfo.SelectedDatabase.CompanyId.toString();
    request.UserId = GlobalProcess.SessionInfo.UserId.toString();
    request.SessionId = GlobalProcess.SessionInfo.SessionId.toString();

var requestbinary = GetByte(request);

    console.log(requestbinary);

My GetByte function is;

function GetByteArrayFromStringArray(parameter) 
  {
     var mainbytesArray = [];
     for (var i = 0; i < parameter.length; i++)
         mainbytesArray.push(parameter.charCodeAt(i));

        return mainbytesArray;
  }

In console, I get empty array. What am I doing wrong?

3
  • Your long-named function works fine. (Creating a byte array), you could have used map, but it works Commented May 27, 2013 at 15:16
  • @BenjaminGruenbaum it works, but it won't do anything if it's passed an object (not a string), which is what's going on here. Commented May 27, 2013 at 15:17
  • What would you expect from that function when you pass in the entire "request" object? Where are the bytes supposed to come from, given that the object has five separate properties? Also, the code in your constructor function ("Request") does nothing at all. Commented May 27, 2013 at 15:18

2 Answers 2

2

Try this

function GetByteArrayFromStringArray(parameter) {
     for (var key in parameter) { // loop through properties

        var mainbytesArray = [];
         for (var i = 0; i < parameter[key].length; i++)
            mainbytesArray.push(parameter[key].charCodeAt(i));
     }
     return mainbytesArray;

}

It loops through the properties and gets you the array of theese

Sign up to request clarification or add additional context in comments.

1 Comment

@Rick Correct me if I am wrong, but are you not clearing the MainBytesArray variable each time you loop through a different property? So, you are returning only the last property as binary?
1

You're passing an object to a function that expects a string (I think). Your object has no "length" property, so the loop does nothing at all.

You could have the function iterate through the object's properties, I suppose, and accumulate an array from the values of each one. That would not be terribly useful, I don't think, as in JavaScript you're not guaranteed that you'll iterate through an object's properties in any particular order.

6 Comments

Ok. What can I do now? What is your solution? Is there anyway to achieve this?
@MehmetInce Achieve what? It's not clear what you expect to happen when you pass that entire object to the function. What do you want the function to do?
I expect that function returns byte array of request object.
@MehmetInce "byte array of request object" does not make any sense. Can you describe exactly what you think the byte array should contain here?
var requestbinary = [1,23,4,3,32, ..... , 44] vs. I want to get something like this.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.