2

I have this objet : keyValue : { key : string , value : number}[];

I want to add a new element to this array from two values, like this :

 let tmpTabKV : { key : string , value : number}[];
 [...]
 tmpTabKV.push({projet.libelle, statKV.value});
 [...]
 keyValue  = tmpTabKV;

I tried multiple syntaxes and seen this : How can I add a key/value pair to a JavaScript object?

But I don't see any key to create a new object. The usage of an Array makes me an error

tmpTabKV.push(Array(projet.libelle, statKV.value));
Community
  • 1
  • 1
Callehabana
  • 95
  • 2
  • 14

4 Answers4

6

You should be pushing an object literal to your array

let tmpTabKV : { key : string , value : number}[] = []
tmpTabKV.push({key:projet.libelle, value:statKV.value});
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

According to let tmpTabKV : { key : string , value : number}[]; just do :

tmpTabKV.push({key: project.libelle, value: statKV.value})

but I think you want this :

tmpTabKV.push({[project.libelle]: statKV.value})

Steeve Pitis
  • 4,283
  • 1
  • 21
  • 24
  • No, that doesn't match his interface of `{ key : string , value : number}` – AJ Richardson Feb 20 '17 at 14:59
  • Can you actually use an array literal as the key in an object? – Jamiec Feb 20 '17 at 15:00
  • I know, but I think this is what he want, and his interface is wrong. That's why I say 'according to his interface' first. – Steeve Pitis Feb 20 '17 at 15:00
  • @Jamiec: It's not an array literal, it's a computed property name. And yes, as of ES2015 (aka "ES6"), you can. It will take the *value* of `project.libelle` and use that as the property name. E.g.: `let x = "answer"; let o = {[x]: 42}; console.log(o.answer); // 42` – T.J. Crowder Feb 20 '17 at 15:01
  • @SteevePitis ah, that's a good point. In that case he should change his interface to `let tmpTabKV : { [key : string]: number} = {};` – AJ Richardson Feb 20 '17 at 15:03
  • Thanks @T.J.Crowder. I knew that, but somehow when I see square braces I still think array literal! My brain hasn't caught up with my ES6 knowledge. – Jamiec Feb 20 '17 at 15:17
1

A few problems I've noticed: first, you're pushing a new object to the array as if it was itself an array. That is, you're relying on position of the values to map to the position of the keys. In an object, the keys are always unordered and therefore must always be specified.

tmpTabKV.push({key: project.libelle, value: statKV.value})

What you're doing with {project.libelle, startKV.value} is making an object with shorthand syntax, which would not work in your example.

In typescript, if you want to restrict the keys in an object, implement an interface.

interface KeyValue { key: string, value: number }

let tmpTabKV: KeyValue[];

// ...

tmpTabKV.push({key: project.libelle, value: number});
Nick
  • 78
  • 6
-1

you should push a new object:

interface KeyValuePair{
   key: string:
   value: number;
}

let tmpTabKV: KeyValuePair[];

tmpTabKV.push(new {key: projet.libelle, value: statKV.value});
Juergen Gutsch
  • 1,774
  • 2
  • 17
  • 28