James Quek
James Quek

Reputation: 15

How to create a complex Javascript object

I am new to JavaScript and Python, and programming in general.

I want to store data about common English phonograms in a JavaScript data object. There are about 80 phonograms. Each phonogram has one or more possible pronunciations. Each phonogram’s pronunciation would have a list of one or more word examples (say, 30 maximum) which would include the IPA phonetic symbols and a translation to a foreign language. E.g., the phonogram ‘ea’ has three pronunciation,

(1) 'iːˈ, (2)ˈɛˈ & (3)ˈeɪˈ:

(1)ˈbeadˈ, 'feat', 'beat'... (2)'bread', 'head', 'dead'... (3)'break'...

Is there a built-in data structure best suited for this? I am thinking of a class to make these objects and store it in an array or something. And how should I write my text for populating the the data objects?

Upvotes: 1

Views: 93

Answers (2)

T.J. Crowder
T.J. Crowder

Reputation: 1074335

JavaScript has four fundamental structured data types:

  • objects, which have properties, which have keys (names which are strings or Symbols) and values (any type)
  • arrays, which have elements, which have indexes and values (arrays are technically objects, but ignore that for now)
  • Map, which has entries, which have keys (any type) and values (any type)
  • Set, which has unique entries of any type (probably not useful for what you're doing)

It sounds like you'd probably want either an object or a Map where the keys are the phonographs and the values are objects. Within each object, you'd probably have another Map or object keyed by the pronunciation where the values are objects giving further information (examples and translations).

Here's an example using Maps, which you initialize by passing an array of arrays into the Map constructor:

const data = new Map([
    [
        "ea", 
        {
            pronunciations: new Map([
                [
                    "iː",
                    {
                        examples: ["bead", "feat"],
                        transations: [/*...*/]
                    }
                ]
            ]),
            otherInfo: /*...*/
        }
    ],
    // ...the other 79 entries...
]);

Getting the data for an entry based on the phonogram:

const entry = data.get("ea");

The entry object will have a pronunciations property with a Map of the pronunciations and the objects (with examples and translations) they map to.

More on MDN:

Upvotes: 1

Mayank Metha
Mayank Metha

Reputation: 162

map of an array should do the work. map key can contain an identifier while the map value is an array that can contain the pronunciation.

map is a (key, value) pair where value will be an array/[] in your case.

Upvotes: 0

Related Questions