Q:

JavaScript: Assigning values to array of null/empty objects

I know the syntax is wrong, but in the following code key is supposed to equal the object's key. As the loop iterates, key gets assigned the i value, so a:1, b:2...etc.
var objArr = [
  {a: null},
  {b: null},
  {c: null}
];

for (var i = 0; i < objArr.length; i++) {
    objArr[i].key = i;
}

A:

Assuming the objects only contain one key, you can find the key using Object.keys[0]:

var objArr = [
  {a: null},
  {b: null},
  {c: null}
];
objArr.forEach((obj, i) => {
  const key = Object.keys(obj)[0];
  obj[key] = i;
});
console.log(objArr);

