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));
0

4 Answers 4

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});
Sign up to request clarification or add additional context in comments.

2 Comments

You also need to initialize the array: let tmpTabKV : { key : string , value : number}[] = [];
@AJRichardson Thanks.
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})

6 Comments

No, that doesn't match his interface of { key : string , value : number}
Can you actually use an array literal as the key in an object?
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.
@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
@SteevePitis ah, that's a good point. In that case he should change his interface to let tmpTabKV : { [key : string]: number} = {};
|
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});

Comments

-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});

2 Comments

You don't need the new keyword
^^ "don't need" => "can't have there" :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.