1

app.js

let ids = [1, 2, 3];

let obj = {};

for (let i in ids) {
  obj = { ...obj, ids[i]: "" };   
}

console.log(obj);

While executing the above code, I get the below error.

Parsing error: Unexpected token, expected ","

  4 |
  5 | for (let i in ids) {
> 6 |   obj = { ...obj, ids[i]: "" };  
    |                      ^
  7 | }
  8 |
  9 | console.log(obj);eslint

I get this error, when I hover the mouse above the line obj = { ...obj, ids[i]: "" }; in my VS Code.

When I run the code using node, i.e on node app.js. I get the following error in my terminal :

obj = { ...obj, ids[i]: "" };  
                     ^

SyntaxError: Unexpected token '['
    at wrapSafe (internal/modules/cjs/loader.js:988:16)
    at Module._compile (internal/modules/cjs/loader.js:1036:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

What am I doing wrong? I expect the value of obj at the end to be {1 : "", 2 : "", 3: ""}

Hari Krishnan U
  • 166
  • 5
  • 16
  • What do you want `obj` to end up looking like? – code Dec 07 '21 at 04:22
  • I expect the value of obj at the end to be {1 : "", 2 : "", 3: ""}. This is mentioned in the question too, towards the end. – Hari Krishnan U Dec 07 '21 at 04:25
  • 3
    Don't use `for`/`in` on arrays. It's meant for objects. Use [`for`/`of`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) or one of the other methods intended for iterating over arrays. – Ouroborus Dec 07 '21 at 04:29
  • Is there a particular reason why you want to do this? An array is technically already an object of the shape you're trying to achieve (just with a few additional properties). You can clone your array using `const newArr = [...ids];` which might be a better option for you. If you really need an object, one quick option could be to use `const obj = Object.assign({}, ids);` – Nick Parsons Dec 07 '21 at 04:32

4 Answers4

4

There are several solutions.

  1. Wrap it around a bracket:
for (let i in ids) {
  obj = { ...obj, [ids[i]]: "" }; // Brackets make it act as a key name instead of something else
}
  1. The simple and easy way:
for (let i in ids) {
  obj[ids[i]] = ""; // This adds a value
}
code
  • 5,690
  • 4
  • 17
  • 39
1

Use [ids[i]] instead of ids[i].

let ids = [1, 2, 3];

let obj = {};

for (let i in ids) {
  obj = { ...obj, [ids[i]]: "" };
}

console.log(obj);
Umair Khan
  • 1,684
  • 18
  • 34
Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37
0

is[i] is key that should be resolve runtime, so you should make it computed property names

Starting with ECMAScript 2015, the object initializer syntax also supports computed property names. That allows you to put an expression in brackets [], that will be computed and used as the property name. This is reminiscent of the bracket notation of the property accessor syntax, which you may have used to read and set properties already. - MDN

let ids = [1, 2, 3];

let obj = {};
for (let i in ids) {
  obj = { ...obj, [ids[i]]: "" };
}

console.log(obj);
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

If you want to dynamically set "" to each id as key in the object, I suggest you to do that:

let obj = {} let ids = [1,2,3]

ids.forEach((id) => {
   obj[id] = '';
});

Object will be { '1': '', '2': '', '3': ''};

BBM
  • 31
  • 3