0

Is there a way to dynamically assign an array? The code doesn't work as intended. It fails at var p = { z }

var z = "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\"";

if(i != u.length - 1){
  z = z + ",";
}

var p = {z}

for (var key in p) {
  if (p.hasOwnProperty(key)) {
    client = Elements.AddNew(key, p[key]);
    client.Update();
  }
}   

Entire code

var fso, f1, ts, s;
        var ForReading = 1;
        fso = new ActiveXObject("Scripting.FileSystemObject");
        // Read the contents of the file.
        Session.Output("Reading file");
        ts = fso.OpenTextFile("c:\\temp\\roles.txt", ForReading);
        s = ts.ReadAll();
        u = s.split('\r\n');
        Session.Output(u);
        for(i = 0; i < u.length; i++){
        m = u[i].split(",");
        var z = "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\"";

            if(i != u.length - 1){
                z = z + ",";
            }

                    var p = {
                        z
                    }
                    Session.Output(p);

        for (var key in p) 
        {
            if (p.hasOwnProperty(key))
            {
            client = Elements.AddNew(key, p[key]);
            client.Update();
            }
        }   


        }

The contents of the file are as follows. It's a comma delimited file.

abc,1
def,2
ghi,3
1
  • Should (m[0] = m[1]) be (m[0] == m[1])? It doesn't make sense to me to do an assignment here. Commented Mar 4, 2014 at 1:56

2 Answers 2

1

You can't create an object like that.

You need to use bracket notation as the member operator to do this

var p = {}

p[m[0]] = m[1];


for (var key in p) {
    if (p.hasOwnProperty(key)) {
        client = Elements.AddNew(key, p[key]);
        client.Update();
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks. I tried that but as soon as it hits ` client = Elements.AddNew(key, p[key]);, it returns the message invalid type`.
why not just use var p = JSON.parse(z)
@clancer when there is such a cleaner and right way to do it why do you want to complicate things
@PeanutsMonkey check the value assigned to the object and see whether it is matching with the expected behavior
|
1

I'll assume that:

(m[0] = m[1]) 

should have been:

(m[0] == m[1]) 

If you have an array m that is like:

var m = ['a', 'b', 'b'];

you seem to be trying to make an object using the pattern:

var z = { m[0] : (m[0] == m[1]) };

which can be written:

var z = {};
z[m[0]] = m[0] == m[1];

An object can be created from the array using:

var z = {};
for (var i=0, iLen=m.length - 1; i<iLen; i++) {
  z[m[i]] = m[i] == m[i+1];
} 

Which will create an object like:

{a: false, b: true}

1 Comment

I have updated my post with the entire code to show what i am trying to achieve.

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.