0

This little function uses tt (an array of test objects) and is expected to return a coherent array of objects like [{ testNum: [e, e, e, e] }, ...] where "e" is an empty array element.

  buildValues = tt => {
    tt.map( aTest => {
      const tn = aTest.testNum;
      return { tn: new Array( aTest.items.length )};
    })
  };

The linter gives a warning in the const line:
TestsPane.js|93 col 13 warning| 'tn' is assigned a value but never used. (no-unused-vars)
And effectively the return line does not use tn as I expected.
What am I doing wrong?

Below, a simplified element of the tt array:

  let tt = [
    { testNum: 1,
      items: [ { value: 0 },  { value: 1 },  { value: 0 }, { value: 0 } ]
    },
  ...

I've tried this in nodejs and it effectively fails as the linter suggests.

THE SOLUTION, built on top of the great answers I got:

  buildValues = tt => tt.map( aTest => {
      return { [aTest.testNum]: new Array( aTest.items.length )};
  });
Juan Lanus
  • 2,293
  • 23
  • 18
  • Please add a example of `tt`. We need a [mre]! – 0stone0 Mar 22 '21 at 13:17
  • you even never return the result of `map`. please add what you want as result. – Nina Scholz Mar 22 '21 at 13:18
  • 3
    `const tn` and `{ tn: ... }` are two separate things and they're not related to each other. the first one is a variable name and the second one is an object's key. So it's normal that the linter is giving you that warning. – pouria Mar 22 '21 at 13:18
  • You may want to see this post - [How to use map](https://stackoverflow.com/a/66519768/2873538) – Ajeet Shah Mar 22 '21 at 13:19
  • https://stackoverflow.com/questions/2241875/how-to-create-an-object-property-from-a-variable-value-in-javascript – CBroe Mar 22 '21 at 13:19
  • `tn` in the return object is not the same as the const declared outside it. The error basically says that you are declaring `tn` with `aTest.testNum`, but you are not using it for anything. – olawrdhalpme Mar 22 '21 at 13:19
  • may be you need the `tn` to be the returned object `key` so that would be something like this: `return {[tn] : new Array( aTest.items.length )}` – kunal panchal Mar 22 '21 at 13:20
  • Thanks to all who answered or commented. I learned how to code an object's key dinamicaliy: the key expression has to be enclosed in brackets for JS to consider it a reference, not a literal. – Juan Lanus Mar 22 '21 at 15:43
  • Also, my code example shed additional confusion because I omitted the `return` when I edited my original one-liner that leveraged the implicit arrow-functions return for simple cases. – Juan Lanus Mar 22 '21 at 15:46

3 Answers3

2

Indeed you are not using the variable tn. You return an object with the key "tn" and not the value of the variable tn.

const tn = aTest.testNum;
const myValue = {};
myValue[tn] = new Array( aTest.items.length );
return myValue
alaric
  • 169
  • 6
1

I think you wanted it to be like so:

buildValues = tt => tt.map( aTest => {
    const tn = aTest.testNum;
    return { [tn]: new Array( aTest.items.length )};
});
n--
  • 3,563
  • 4
  • 9
  • Initially I wanted to be like this: `tt.map( aTest => ({ aTest.testNum: new Array( aTest.items.length )})` a single liner, but the linter compleined of the dot I'll try it with your brackets. – Juan Lanus Mar 22 '21 at 13:25
  • it would work like this: tt.map( aTest => ({ [aTest.testNum]: new Array( aTest.items.length )}) – n-- Mar 22 '21 at 13:28
0
items: [ { value: 0 }.  { value: 1 }.  { value: 0 }.  { value: 0 } ] 

You should be separating them with a comma.

return { tn: new Array( aTest.items.length )};

If you have a dynamic key you should put them in brackets like this

return { [tn]: new Array( aTest.items.length )};
Marko
  • 66
  • 6
  • 1
    Yes @Marko, the dots instead of commas are a typo, I already fixed it to avoid confusion. And yes, the midding brackets seem to be my issue. – Juan Lanus Mar 22 '21 at 14:38