简体   繁体   中英

get index by value in javascript array

Reading the 'enum' in typescript, I see this code compiled to javascript.

var Season = [];
Season[Season["Spring"] = 0] = "Spring";
Season[Season["Summer"] = 1] = "Summer";
Season[Season["Fall"] = 2] = "Fall";
Season[Season["Winter"] = 3] = "Winter";

console.log(Season.Spring); // 0
console.log(Season[0]); // Spring

and, if I change Season to {} empty objext at first line, this also works and it makes sense. I don't know what's happening hear. What is this?

Edit: Yes. This is not what compiler generate. Compiler uses empty object. But if I changed it to empty array. It still works. My question was why array also works good. At first my question included both version, but someone edited question and delete object-use version.

This:

Season[Season["Spring"] = 0] = "Spring";
Season[Season["Summer"] = 1] = "Summer";
Season[Season["Fall"] = 2] = "Fall";
Season[Season["Winter"] = 3] = "Winter";`

Creates eight properties in Season object:

Season["Spring"] = 0;
Season["Summer"] = 1;
Season["Fall"] = 2;
Season["Winter"] = 3;
Season[0] = "Spring";
Season[1] = "Summer";
Season[2] = "Fall";
Season[3] = "Winter";

After that Season can be requested:

  1. by text to get its index (first four properties)
  2. by index to get its text (second four properties)

Looks like someone is just trying to make Season be a bidirectional map.

In other words, you can look up items by numeric index or by their string value.

var season1     = Season[1];        // season1 == 'Summer'
var summerIndex = Season['Summer']; // summerIndex == 1

if I change Season to {} empty objext at first line, this also works and it makes sense

I have some docs about enums , but your question is why:

Season = [];

Instead of

Season = {};

When in actuality the following TypeScript:

enum Season {
    Spring,
    Summer,
    Fall,
    Winter
}

Generates the JavaScript :

var Season;
(function (Season) {
    Season[Season["Spring"] = 0] = "Spring";
    Season[Season["Summer"] = 1] = "Summer";
    Season[Season["Fall"] = 2] = "Fall";
    Season[Season["Winter"] = 3] = "Winter";
})(Season || (Season = {}));

And as you can see Season = {} . So it should make sense to you.

PS:

Not your question but, if you are curious about Season[Season["Spring"] = 0] = "Spring"; that is covered here .

I'll address your question as to why it still works if you change the first statement in your code block to var Season = []; so that Season is an array instead of an empty object. Arrays are also objects, and you can set any property you like on an object in JavaScript, even arrays (as long as you don't run the object through Object.freeze ). So the expression Season["Spring"] = 0 is simply setting a property named "Spring" on the Season object which you could access using Season["Spring"] or Season.Spring the same as you would on any other object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM