简体   繁体   English

有没有办法在JavaScript中找到对象中变量的位置

[英]Is there any way to find the position of a variable in an object in JavaScript

For example in例如在

      {colour: 'red',x:0,y:20},
      {colour: 'orange',x:0,y:40},
      {colour: 'yellow',x:0,y:60},
      {colour: 'green',x:0,y:80},
      {colour: 'blue',x:0,y:90},
      {colour: 'purple',x:0,y:100},
      {colour: 'pink',x:0,y:120},
      {colour: 'brown',x:0,y:140},
      {colour: 'white',x:0,y:160},
      ]

Red is 0, orange is 1, etc.红色为 0,橙色为 1,以此类推。

You can do it like this你可以这样做

 var arr = [{colour: 'red',x:0,y:20}, {colour: 'orange',x:0,y:40}, {colour: 'yellow',x:0,y:60}, {colour: 'green',x:0,y:80}, {colour: 'blue',x:0,y:90}, {colour: 'purple',x:0,y:100}, {colour: 'pink',x:0,y:120}, {colour: 'brown',x:0,y:140}, {colour: 'white',x:0,y:160}, ]; var position = arr.findIndex(a=>a.colour == 'orange'); console.log(position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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