如何實現(xiàn)一個索引數(shù)組?場景像下面這段代碼class User { constructor(id, name, age) { this.id = id this.name = name this.age = age }}const users = [ new User(1, 'rxliuli', 18), new User(2, '琉璃', 17), new User(3, '靈夢', 16), new User(4, '楚軒', 23), new User(5, '月姬', 1000), new User(6, '音無', 16),]console.log(users.find(({ id }) => id === 1))console.log(users.find(({ name }) => name === '音無'))console.log(users.filter(({ age }) => age >= 18))難道實現(xiàn)這些功能就只能遍歷么?能否實現(xiàn)某種數(shù)據(jù)結(jié)構(gòu)能夠指定某個字段有索引,然后能夠以常量時間根據(jù)該字段進行查找/過濾等操作呢?Pass: 結(jié)構(gòu)化的數(shù)據(jù)庫一般都有索引這種功能,為何程序中反而沒有呢?
如何實現(xiàn)一個簡單的有索引的數(shù)組?
冉冉說
2019-05-19 14:35:35