0

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();
                }
            }   
PeanutsMonkey
  • 6,919
  • 23
  • 73
  • 103
  • `=` in object key assignment? – redV Mar 04 '14 at 18:38
  • 1
    What is that code supposed to do? – Barmar Mar 04 '14 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 http://stackoverflow.com/questions/14020941/splitting-a-key-value-pair-response-seperated-by-a-using-javascript – PeanutsMonkey Mar 04 '14 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. – PeanutsMonkey Mar 04 '14 at 18:41
  • 1
    I don't see anything like that in the question you referenced. – Barmar Mar 04 '14 at 18:42
  • @Barmar - Apologies. I have added it now. – PeanutsMonkey Mar 04 '14 at 18:51
  • I mean there's nothing in that question that looks like your `var p = ...` statement. The answer in that question shows how to set properties that are computed from a string, why aren't you following it? – Barmar Mar 04 '14 at 18:52
  • @Barmar - Sorry Barmar. Am new at this. Not sure what you mean. – PeanutsMonkey Mar 04 '14 at 19:06
  • You said you got this code from the other question. I looked at that question and I don't see anything like the code you wrote. – Barmar Mar 04 '14 at 19:13

3 Answers3

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • well, if that `" : "` is supposed to be the the key:value separator, then then `... + " : " + ...` into `... : ...` instead. – Marc B Mar 04 '14 at 18:43
  • I tried `"\"" + m[0] + "\"" + : + "\"" + (m[0] = m[1]) + "\""` and it doesn't work either – PeanutsMonkey Mar 04 '14 at 18:48
  • remove the `+`. you're trying to concatenate a bare `:` now, which is illegal syntax. – Marc B Mar 04 '14 at 18:53
  • 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. – PeanutsMonkey Mar 04 '14 at 18:55
  • Why are you concatting all those quotes, and WHY are you doing an assignment (`m[0]=m[1]`) in there? – Marc B Mar 04 '14 at 18:58
  • Because i have tried `m[0] : (m[0] = m[1])` and it fails as well. – PeanutsMonkey Mar 04 '14 at 19:04
  • but WHY do you want the value of m[0] to be used as a key? and why are you OVERWRITING that value in the value section? `m[0] = m[1]` stuffs the value of m[1] into m[0]. – Marc B Mar 04 '14 at 19:06
  • The reason is `m[0] ` is the key e.g. Joe and `m[0] = m[1]` is the value. – PeanutsMonkey Mar 04 '14 at 19:09
  • no. `m[0] = m[1]` is AN ASSIGNMENT. You're overwriting `Joe` with whatever was in `m[1]`. – Marc B Mar 04 '14 at 19:12
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];
Tom Leese
  • 19,309
  • 12
  • 45
  • 70
  • 1
    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. – Barmar Mar 04 '14 at 18:39
  • So it looks like he's missing the key, and that concatenation should be the value. – Barmar Mar 04 '14 at 18:39
  • @Tom Leese - All i get is `[object Object]`. – PeanutsMonkey Mar 04 '14 at 18:49
  • 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 Mar 04 '14 at 18:52
  • @Tom Leese - That's correct and it throws an error when it hits the next block of code. – PeanutsMonkey Mar 04 '14 at 19:05
  • @Tom Leese - When it reaches `client = selectedPackage.Elements.AddNew(key, p[key]);` The error i get is `invalid type` – PeanutsMonkey Mar 04 '14 at 19:07
  • I can't see anything obviously wrong, perhaps the error is in `AddNew` itself? You could try doing `console.log(key, p[key]);` on in the for loop to get more information. – Tom Leese Mar 04 '14 at 19:11
  • @Tom Leese - I could show you the entire code if i had the option for a chat – PeanutsMonkey Mar 04 '14 at 19:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48990/discussion-between-peanutsmonkey-and-tom-leese) – PeanutsMonkey Mar 04 '14 at 19:12
  • @Tom Leese - Thanks so much for your help. I was able to resolve it. Interestingly enough when i keyed in the values again into the text file, they worked. I don't why but they did. – PeanutsMonkey Mar 05 '14 at 00:07
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];
Barmar
  • 741,623
  • 53
  • 500
  • 612