简体   繁体   中英

Access nameless objects in a list

I have the following list:

var colorsList = [
  { nome: 'vermelho', rgb: [255, 0, 0] },
  { nome: 'verde', rgb: [0, 255, 0] },
  { nome: 'azul', rgb: [0, 0, 255] }
];

I'm trying to access the RGB values of "vermelho" (red). How can I do it? I tried colorsList.rgb[0] but this isn't it.

使用colorsList[0].rgb[0]colorsList.find(color => color.nome === 'vermelho').rgb[0]

If you're happy that you want the first item in the list

colorsList[0].rgb

If you want to lookup the item by name

colorsList.find(x => x.nome === "vermelho").rgb

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