Create similar C# dictionary in Typescript?

Asked

Viewed 279 times

1

I have the following dictionary in C#:

Dictionary<string, List<string>> ls = new Dictionary<string, List<string>>();
ls.Add("chave1", new List<string>());
ls['chave1'].Add("Valor adicionado");

I’m trying to create it like this in Typescript:

var dicionario:{[key:string]:string; [value:string[]:[]} = {}
dicionario['chave1'].push('Valor adicionado')

The above example of Typescript is not working, I tried it anyway, it seems that the above variable does not support two types in the same set {}

How can I make the same C# dictionary in Typescript?

1 answer

1

The objects in typescript and Javascript are very similar to a C#dictionary, you can enter values and identify them through a keyword, you do not need to create any structure for this, the objects work so natively.

Follow an example

var dicionario = {}; //criando o dicionário com valor de objeto vazio

dicionario["key"] = "valor"; //colocando um valor na chave "key"

var a = dicionario["key"]; //pegando o valor da chave "key" e colocando em uma variável

//fazendo uma verificação para ver se existe valor para a chave "erro" dentro do nosso dicionario
if(!dicionario["erro"]) {
   console.log("ops, não existe valor para a chave erro"); //não existe valor
} else {
   console.log("uhul! existe valor para a chave erro"); //existe valor
}

//fazendo uma verificação para ver se existe valor para a chave "key" dentro do nosso dicionario
if(!dicionario["key"]) {
   console.log("ops, não existe valor para a chave key"); //não existe valor
} else {
   console.log("uhul! existe valor para a chave key e seu valor é " + dicionario["key"]); //existe valor
}

But be careful, because unlike a dictionary in C#, objects do not play Exceptions when an already used key has its value inserted again, the behavior on objects is only to modify the value.

  • Hello, I’m trying to create the C# dictionary of the question, the answer gave a clear, but did not solve the problem

  • Hi System, is there anything the object couldn’t do for you? What is the need to create a new structure if a native already exists and does the same job?

  • I’m starting to learn Typescript , I can’t apply the example to the resolution.

  • 1

    it would help if you put some examples of how to put value in the key, check if there is value for that key and take the value with a specific key?

  • That would help me a lot :)

  • @System show, I made a more elaborate example, put some comments in the middle of the code to help you understand

  • It is in Javascript, how do I do the same in Typescript? It is because it is giving error of "Type"

  • @System can copy and paste the error here in the comment?

  • That is the mistake: Element implicitly has an 'any' type because Expression of type '"key"' can’t be used to index type '{}'.

  • can post how the code looks? I’m sorry but I can’t emulate the same error in the typescript fiddle, follow the link to test. https://www.typescriptlang.org/play/? ssl=1&ssc=1&pln=27&pc=2#

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.