如何在 if 语句中使用对象 - Javascript

How to use objects within if statements - Javascript

目前我有一个条件语句列表,例如:

if(x = 1) { $(#div).html('A') };

if(x = 2) { $(#div).html('B') };

if(x = 3) { $(#div).html('C') };

if(x = 4) { $(#div).html('D') };

这样做可以,但效率不高。

我正在尝试对此采取 DRY 方法,但不确定如何去做。

我想知道类似于以下内容的方法是否可行,或者是否有更好的方法来实现此目的。

var arr = [
    [1, 'A'],
    [2, 'B'],
    [3, 'C'],
    [4, 'D']
]

if ( x === arr[any of sub arrays][0]{ // 1, 2, 3 or 4

    $('div#').html('[triggered sub array[1]'); // if 1 is triggered, set html content to A, if 2, set html content to B and so on.    
}

是否有一个术语可以描述我在这里要描述的内容,以便我查找?

我可能误解了你,但是...听起来你可以只使用地图?

var map = {
  1: 'A',
  2: 'B',
  3: 'C'
};

if (map.hasOwnProperty(incomingNum)) {
  $('div#').html(map[incomingNum));
}