在Typescript/Javascript中访问对象

在Typescript/Javascript中访问对象,javascript,typescript,Javascript,Typescript,其思想是找出用户输入的颜色是否存在于对象颜色值中 我提到这一点: 我遇到找不到名称键的错误。我使用的是打字脚本,在线编辑器 请向我解释我做错了什么。看看@Owl的答案,它也修复了代码中的其他问题 在循环中,未定义变量键。你必须这样写: 对于此.colorValues中的常量键{ ... } 尽管我不会使用for-in循环,因为对象有它们自己的原型属性,您也会在for-in循环中接收这些属性。更好的解决方案是: 对于Object.keysthis.colorValues的常量键{ ... } 但是

其思想是找出用户输入的颜色是否存在于对象颜色值中

我提到这一点:

我遇到找不到名称键的错误。我使用的是打字脚本,在线编辑器


请向我解释我做错了什么。

看看@Owl的答案,它也修复了代码中的其他问题

在循环中,未定义变量键。你必须这样写:

对于此.colorValues中的常量键{ ... } 尽管我不会使用for-in循环,因为对象有它们自己的原型属性,您也会在for-in循环中接收这些属性。更好的解决方案是:

对于Object.keysthis.colorValues的常量键{ ... } 但是,由于您不需要这些键,只需使用它们来检索colorValues对象中的值,因此也可以使用以下方法:

对于Object.values的常量颜色,请参见此.colorValues{ ... } 找不到名称键错误由@MrCodingB回答

写这篇文章的更好方法是:

class ResistorColor {
  private colors: string[]
    
  public colorValues = {
    black: 0,
    brown: 1,
    red: 2,
    orange: 3,
    yellow: 4,
    green: 5,
    blue: 6,
    violet: 7,
    grey: 8,
    white: 9
  }

  constructor(colors: string[]) {
    
    if( colors.length > 2)
    {
      for( key in this.colorValues)
      {
        if (this.colorValues[key].indexOf(colors[0]) !== -1) 
        {
          return key;
        }
      }
    }

    this.colors = colors
    
  }
}
也可以通过使用打印出不正确的颜色值


您试图在运行时防止的一些错误可以在编译时使用更严格的类型来避免。您可以创建仅允许特定颜色名称的类型,还可以使用在颜色数组上强制使用最小长度

看起来是这样的。colorValues可能只是一个

现在,您只能使用有效参数调用构造函数

enum COLOR_VALUES {
    black = 0,
    brown = 1,
    red = 2,
    orange = 3,
    yellow = 4,
    green = 5,
    blue = 6,
    violet = 7,
    grey = 8,
    white = 9
}

type ColorNames = keyof typeof COLOR_VALUES;

class ResistorColor {

    // can have two or more colors
    constructor(private colors: [ColorNames, ColorNames, ...ColorNames[]]) {
    }
}
如果this.colorValues是一个实例变量,我将在类之外创建初始值,以便我们可以使用typeof


好吧还有其他写作方法吗?编辑我的答案谢谢你的帮助。与问题无关,而不是a.indexOfv!==-1使用@axiac谢谢。如果你把它写下来作为一个答案,我会投赞成票。谢谢你详细的回答。非常感谢你有用的回答。
class ResistorColor {
  private colors: string[]
    
  public colorValues = {
    black: 0,
    brown: 1,
    red: 2,
    orange: 3,
    yellow: 4,
    green: 5,
    blue: 6,
    violet: 7,
    grey: 8,
    white: 9
  }

  constructor(colors: string[]) {
    
    const colorValues = Object.keys(this.colorValues);
    const invalidColors = colors.filter(c => !colorValues.includes(c));

    if (invalidColors.length === 0) {
      this.colors = colors
    } else {
      throw new Error(`Invalid Color -> ${invalidColors.join(", ")}`);
    }
  }
}

const resistorColor = new ResistorColor(["black", "brown"]); // correct
console.log("resistorColor has correct color");

const resistorColor2 = new ResistorColor(["black", "brown", "gold", "foo"]); // throws error "Invalid Color -> gold, foo"
enum COLOR_VALUES {
    black = 0,
    brown = 1,
    red = 2,
    orange = 3,
    yellow = 4,
    green = 5,
    blue = 6,
    violet = 7,
    grey = 8,
    white = 9
}

type ColorNames = keyof typeof COLOR_VALUES;

class ResistorColor {

    // can have two or more colors
    constructor(private colors: [ColorNames, ColorNames, ...ColorNames[]]) {
    }
}
const a = new ResistorColor(["black", "blue"]); // ok
const b = new ResistorColor(["black", "blue", "red"]); // ok
const c = new ResistorColor(["black"]); // error: Source has 1 element(s) but target requires 2.
const d = new ResistorColor(["white", "cyan"]); // error: Type '"cyan"' is not assignable to type 
const initialValues = {
    black: 0,
    brown: 1,
    red: 2,
    orange: 3,
    yellow: 4,
    green: 5,
    blue: 6,
    violet: 7,
    grey: 8,
    white: 9
  }

type ColorNames = keyof typeof initialValues;

class ResistorColor {

    public colorValues = initialValues;

    // can have two or more colors
    constructor(private colors: [ColorNames, ColorNames, ...ColorNames[]]) {
    }
}