-1

I am unable to decipher the error message Expected :. It occurs when it attempts to execute the code below. I have a string of characters e.g. "Joe":"HR" being passed to it

var p = {
    "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\""
};

Additional code

for (var key in p) 
            {
                if (p.hasOwnProperty(key))
                {
                client = selectedPackage.Elements.AddNew(key, p[key]);
                client.Update();
                }
            }   
9
  • = in object key assignment? Commented Mar 4, 2014 at 18:38
  • 1
    What is that code supposed to do? Commented Mar 4, 2014 at 18:40
  • @redV - I'm following an example of whether i have a string of characters e.g. "Joe":"HR" and need to parse them as name value pairs. The example i came across is stackoverflow.com/questions/14020941/… Commented Mar 4, 2014 at 18:40
  • @Barmar - It's meant to take a string of characters and parse it. I have added the additional code in the post. Commented Mar 4, 2014 at 18:41
  • 1
    I don't see anything like that in the question you referenced. Commented Mar 4, 2014 at 18:42

3 Answers 3

3

You're defining an object, but only have a key without value, e.g. you're trying to do:

var p = {
   key : value
}

But your code is only

var p = {
   key
}

You have a :, but since it's inside a string (" : "), it doesn't count.

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

9 Comments

well, if that " : " is supposed to be the the key:value separator, then then ... + " : " + ... into ... : ... instead.
I tried "\"" + m[0] + "\"" + : + "\"" + (m[0] = m[1]) + "\"" and it doesn't work either
remove the +. you're trying to concatenate a bare : now, which is illegal syntax.
I tried var p = { "\"" + m[0] + "\"" : "\"" + (m[0] = m[1]) + "\"" }; and it still throws an error. Can you post an example? I may be overlooking something.
Why are you concatting all those quotes, and WHY are you doing an assignment (m[0]=m[1]) in there?
|
2

{} is an object, so it expects a value for the key:

{
    "key": "value"
}

In your case, the key is "\"" + m[0] + "\"" + " : " + "\"" + (m[0] = m[1]) + "\"" (which is actually invalid, the error is a little unclear on this), so you need to specify a value as well.

I suspect you want this (from your string, which looks like JSON):

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

10 Comments

You also can't use an expression as the key in an object literal, the key has to be an identifier or a literal string.
So it looks like he's missing the key, and that concatenation should be the value.
@Tom Leese - All i get is [object Object].
As Barmar said, that's what you get when you try to get a string representation of an object. Sounds like this is the output from a JavaScript console?
@Tom Leese - That's correct and it throws an error when it hits the next block of code.
|
0

You don't create an object with a variable key and value by concatenating the text of a literal. You have to use array-style assignment:

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

5 Comments

I tried that and all it returns is [object Object]
That's what an object looks like when you try to display it as a string.
How do i know if the values are being passed in that case because i get an error at client = selectedPackage.Elements.AddNew(key, p[key]);? The error i get is invalid type
If this is the Firefox Console, you should be able to click on [object Object] to get more information about it.
@Tom Leese - Unfortunately i can't as it's a script editor of a third-party tool so debugging is limited.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.