如何在Coffeescript/Javascript中重构列表中的重复字段?

如何在Coffeescript/Javascript中重构列表中的重复字段?,javascript,coffeescript,Javascript,Coffeescript,代码如下所示: $scope.markerList = [ { id: 1 pos: latitude: 45 longitude: -73 options: {animation: map.Animation.DROP} } { id: 2 pos: latitude: 44.5 longitude: -72.7 options:

代码如下所示:

  $scope.markerList = [
    {
      id: 1
      pos:
        latitude: 45
        longitude: -73
      options: {animation: map.Animation.DROP}

    }
    {
      id: 2
      pos:
        latitude: 44.5
        longitude: -72.7
      options: {animation: map.Animation.DROP}
    }
    {
      id: 3
      pos:
        latitude: 44.6
        longitude: -72.6
      options: {animation: map.Animation.DROP}

    }
    {
      id: 4
      pos:
        latitude: 44.95
        longitude: -72.95
      options: {animation: map.Animation.DROP}

    }

  ]
class Marker
  id = 0
  constructor: (lat, long, animation) ->
    @id = ++id
    @position =
      latitude: lat
      longitude: long
    @options = 
      animation: animation

可以看出,列表中的每个元素都有相同的字段,如选项:{…}。有没有更简单的方法在Coffeescript中编写此列表?

可能类似于:

$scope.markerList = [{
  id: 1,
  pos: {
    latitude: 45,
    longitude: -73
  }
}, {
  id: 2,
  pos: {
    latitude: 44.5,
    longitude: -72.7
  }
}, {
  id: 3,
  pos: {
    latitude: 44.6,
    longitude: -72.6
  }
}, {
  id: 4,
  pos: {
    latitude: 44.95,
    longitude: -72.95
  }
}].map(function(marker) {
  marker.options = {animation: map.Animation.DROP};
  return marker;
});
您可以为列表中的项目创建一个类,该类可能如下所示:

  $scope.markerList = [
    {
      id: 1
      pos:
        latitude: 45
        longitude: -73
      options: {animation: map.Animation.DROP}

    }
    {
      id: 2
      pos:
        latitude: 44.5
        longitude: -72.7
      options: {animation: map.Animation.DROP}
    }
    {
      id: 3
      pos:
        latitude: 44.6
        longitude: -72.6
      options: {animation: map.Animation.DROP}

    }
    {
      id: 4
      pos:
        latitude: 44.95
        longitude: -72.95
      options: {animation: map.Animation.DROP}

    }

  ]
class Marker
  id = 0
  constructor: (lat, long, animation) ->
    @id = ++id
    @position =
      latitude: lat
      longitude: long
    @options = 
      animation: animation
然后用该类的实例填充标记列表:

defaultAnimation = map.Animation.DROP

$scope.markerList = [
    new Marker(45, -73, defaultAnimation)
    new Marker(44.5, -72.7, defaultAnimation)
    new Marker(44.6, -72.6, defaultAnimation)
    new Marker(44.95, -72.95, defaultAnimation)
]

假设map是全局的或更好的,可以通过DI注入的东西,您甚至可以考虑将Debug处理添加到构造函数中,这将进一步简化它:

class Marker
  id = 0
  constructor: (lat, lng, animation = map.Animation.DROP) ->
    ...

$scope.markerList = [
    new Marker(45, -73)
    new Marker(44.5, -72.7)
    ...
]
我会这样做: 注意-根据下面的注释,我编辑了这个以修复缩进

points = [
  [1, 45, -73]
  [2, 44.5, -72.7]
  [3, 44.6, -72.6]
  [4, 44.95, -72.95] ]

sml = []

for p in points
  pp = {
    id: p[0]
    pos:
      latitude: p[1]
      longitude: p[2]
    options: {animation: map.Animation.DROP} }

  sml.push(pp)

$scope.markerList = sml

for循环还有一个点中p,i的迭代器,这样就可以对id:id:i+1使用0索引i。您也可以考虑更具描述性的析构函数赋值:对于[LAT,LUL],I点。最后,您的sml.pushpp缩进过多。否则,我喜欢它!关于缩进,你是对的。我在发布之前尝试过这个,它很有效,所以我可能在复制过程中的某个地方引入了打字错误。我选择不去假设我是一个永远可以被认为是连续的、没有间隔的索引。也许,也许不是,但我们没有这些信息。非常好的评论,谢谢。