-1

I'm new to JS and want to store the table below in a variable. I used dictionary like below. Maybe I'm wrong and it is not good.

<script>
    var dict = {}
    dict[0]['Name'] = "AD";
    dict[0]['Color'] = "Blue";
    dict[0]['Year'] = "2020";

    dict[1]['Name'] = "DC";
    dict[1]['Color'] = "Red";
    dict[1]['Year'] = "1809";

    dict[2]['Name'] = "FD";
    dict[2]['Color'] = "Green";
    dict[2]['Year'] = "2011";

    console.log(dict);
</script>

and all like that. But I only get the error output.

TypeError: Cannot set properties of undefined (setting 'Name')

I need to feel data like this structure via Javascript. But I'm wrong. why? What is the problem?

See the picture I attached of the table.

enter image description here

Ivar
  • 6,138
  • 12
  • 49
  • 61
Pichai
  • 9
  • Duplicate of [JavaScript - cannot set property of undefined](https://stackoverflow.com/questions/7479520/javascript-cannot-set-property-of-undefined) – Ivar Jan 12 '22 at 12:42
  • It is complecated. I can't understand. – Pichai Jan 12 '22 at 12:42
  • I think you are trying to make array of objects [these answers could help](https://stackoverflow.com/questions/35435042/how-can-i-define-an-array-of-objects) – Tomáš Šturm Jan 12 '22 at 12:44
  • 4
    @Pichai If fundamental JavaScript data structures are "*complicated* (sp.)", I would recommend re-visiting some more foundational-level introductory material to the language. Without this baseline knowledge, you'll find it a struggle to continue to build your skillset. – esqew Jan 12 '22 at 12:45

2 Answers2

2

First of all, dict should be an array, rather than an object. Then, you should add the objects to the array. Example:

let dict = [];
dict.push({'Name': 'AD', 'Color': 'Blue', 'Year': '2020'});
console.log(dict);
voiarn
  • 547
  • 6
  • 13
  • Is it a kind of json? – Pichai Jan 12 '22 at 12:47
  • @Pichai JSON is a textual representation of Javascript data structures. If you aren't sure you understand the difference, i recommend finding some tutorials on Javascript types and datastructures. Make sure you understand the fundamentals. – siride Jan 12 '22 at 12:50
  • @voiarn How add only one by one not all at one time? – Pichai Jan 12 '22 at 13:10
  • @Pichai This way, you are adding each "row" of your table one by one to the `dict`. You are adding the entire row as an object at once. I don't see how it would help if you add Name, Color, Year one by one. – voiarn Jan 12 '22 at 13:46
  • @voiam it is because I get the data from a json. Then I have to select only some vriables from all the json data. and then add some calculation and present the new data. This is why I have to select and store only some part of data. – Pichai Jan 12 '22 at 13:50
  • 1
    @Pichai can you edit your question to show how you are actually intending to use this? It sounds like what you are trying to do is different from what you showed us. – siride Jan 12 '22 at 14:22
0

You need to define the index you try to set as an object. However I would recommend using my example at index 3 instead. Alternatively you can an array instead of an object. I assume that you require key value pairs in this case.

    var dict = {}
    dict[0] = {}
    dict[0]['Name'] = "AD";
    dict[0]['Color'] = "Blue";
    dict[0]['Year'] = "2020";

    dict[1] = {}
    dict[1]['Name'] = "DC";
    dict[1]['Color'] = "Red";
    dict[1]['Year'] = "1809";

    dict[2] = {}
    dict[2]['Name'] = "FD";
    dict[2]['Color'] = "Green";
    dict[2]['Year'] = "2011";

    // I would recommend doing it like this instead
    dict[3] = {Name: 'CD', Color: 'Red', Year: '2012'}

    console.log(dict);
Vincent Menzel
  • 1,040
  • 1
  • 6
  • 17
  • I would edit your answer to use arrays as that makes the most sense. – siride Jan 12 '22 at 12:50
  • I agree from what I can see an array would make the most sense, however the question refers to a dictionary and there already is a response that explains the array approach so I think changing my response wouldn't add any value to the question. – Vincent Menzel Jan 12 '22 at 13:09
  • @VincentMenzel Your answer is good and close to what I want. the only problem is a new dictionary need to be defined each time. – Pichai Jan 12 '22 at 13:25
  • @Pichai That's a not a problem, that's literally what is required to make it work. – siride Jan 12 '22 at 14:22
  • The object has to be defined either way (when using an array or an dictionary) – Vincent Menzel Jan 13 '22 at 13:54