Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在对象中添加到数组时避免if..else梯形图_Javascript - Fatal编程技术网

Javascript 在对象中添加到数组时避免if..else梯形图

Javascript 在对象中添加到数组时避免if..else梯形图,javascript,Javascript,是否有办法避免以下代码中出现的if..else情况,并在一行中执行此操作 var myObject = {}, data = ["Chennai", "Thoothukkudi", "Madurai", "Coimbatore", "Trichy", "Salem", "Tirunelveli", "Erode", "Vellore", "Tiruppur"]; data.forEach(function(currentCity) {

是否有办法避免以下代码中出现的
if..else
情况,并在一行中执行此操作

var myObject = {},
    data = ["Chennai", "Thoothukkudi", "Madurai",
            "Coimbatore", "Trichy", "Salem", "Tirunelveli",
            "Erode", "Vellore", "Tiruppur"];

data.forEach(function(currentCity) {
    if (currentCity[0] in myObject) {
        myObject[currentCity[0]].push(currentCity);
    } else {
        myObject[currentCity[0]] = [currentCity];
    }
});

console.log(myObject);
输出

{ C: [ 'Chennai', 'Coimbatore' ],
  T: [ 'Thoothukkudi', 'Trichy', 'Tirunelveli', 'Tiruppur' ],
  M: [ 'Madurai' ],
  S: [ 'Salem' ],
  E: [ 'Erode' ],
  V: [ 'Vellore' ] }
问题是,我必须这样做,
if..else
检查代码中的许多地方。如果我在8个地方这样做,那么总共有40行。所以,我正在寻找一种方法,将其写在一行中

在Python中,有一种称为
defaultdict
的东西,普通dict也可以与
setdefault
函数一起使用。有什么优雅而简短(行数较少)的方法可以做到这一点吗?

像这样:

myObject[currentCity[0]] = myObject[currentCity[0]] || [];
myObject[currentCity[0]].push(currentCity);
像这样:

myObject[currentCity[0]] = myObject[currentCity[0]] || [];
myObject[currentCity[0]].push(currentCity);

您可以将这些行添加到单独的函数中,然后在每个实例中只需调用一次:

function wrangleCity(currentCity, myObject) {
  if (currentCity[0] in myObject) {
    myObject[currentCity[0]].push(currentCity);
  } else {
    myObject[currentCity[0]] = [currentCity];
  }
}

data.forEach(function (currentCity) {
  wrangleCity(currentCity, myObject);
});

您可以将这些行添加到单独的函数中,然后在每个实例中只需调用一次:

function wrangleCity(currentCity, myObject) {
  if (currentCity[0] in myObject) {
    myObject[currentCity[0]].push(currentCity);
  } else {
    myObject[currentCity[0]] = [currentCity];
  }
}

data.forEach(function (currentCity) {
  wrangleCity(currentCity, myObject);
});

与Array.Prototype.forEach比较
Array.Prototype.map
利用返回值并实际返回相同大小的新数组


与Array.Prototype.forEach比较
Array.Prototype.map
利用返回值并实际返回一个相同大小的新数组。

不应被要求@?编写一个可重用的命名函数。您可以使用三元运算符@thefourtheye不应被要求@?编写一个可重用的命名函数。您可以使用三元运算符@thefourtheye