如何 select 按键从数组中取值

How to select value from array by key

我的 class:

中有这样的数组
   stuff = [
        { ['xwz']: 'https://site1.com' },
        { ['erx']: 'https://site2.com' },
        { ['qwery']: 'https://someurl-here.com' },
        { ['stuff']: 'http://morestuffhere.com' }
    ]

我想通过像 this.stuff['xwz'] 那样传递密钥来获取值 ('https://...'),但这种方式行不通。有什么想法吗?

这应该可以完成工作。

// declare as
stuff = {
    'xwz': 'https://site1.com',
    'erx': 'https://site2.com',
    'qwery': 'https://someurl-here.com',
    'stuff': 'http://morestuffhere.com'
 }

// Access with
this.stuff['xwz'] // returns 'https://site1.com'

所以您编写代码的方式需要先访问数组索引,然后再访问对象...像这样(假设您使用的是 class 属性)

this.stuff[0].xwz // This will retrieve the first array element

现在,为什么你不只使用一个对象来完成这个任务,就像这样

 stuff = {
    xwz: 'https://site1.com',
    erx: 'https://site2.com',
    qwery: 'https://someurl-here.com',
    stuff: 'http://morestuffhere.com'
 }

    this.stuff.xwz