1

我正在尝试映射我的对象的一部分,但我有一些未定义的变量。这是我转储到 json 中的对象(数据,即对象数组):

[
  {
    "key": "Sam",
    "values": [
      {
        "label": "Name",
        "value": 38
      },
      {
        "label": "Surname",
        "value": 38
      },
      {
        "label": "Phone",
        "value": 36
      },
      {
        "label": "Text",
        "value": 38
      },
      {
        "label": "Website",
        "value": 28
      },
      {
        "label": "Rating",
        "value": 0
      },
      {
        "label": "Factor",
        "value": 2
      },
      {
        "label": "Cases",
        "value": 2
      },
      {
        "label": "Hours",
        "value": 5
      }
    ],
    "color": "#E05353"
  },
  {
    "key": "Niki",
    "values": [
      {
        "label": "Name",
        "value": 38
      },
      {
        "label": "Surname",
        "value": 38
      },
      {
        "label": "Phone",
        "value": 37
      },
      {
        "label": "Text",
        "value": 38
      },
      {
        "label": "Website",
        "value": 4
      },
      {
        "label": "Rating",
        "value": 0
      },
      {
        "label": "Factor",
        "value": 1
      },
      {
        "label": "Cases",
        "value": 0
      },
      {
        "label": "Hours",
        "value": 0
      }
    ],
    "color": "#293F90"
  },
  {
    "key": "Dan",
    "values": [
      {
        "label": "Name",
        "value": 21
      },
      {
        "label": "Surname",
        "value": 21
      },
      {
        "label": "Phone",
        "value": 6
      },
      {
        "label": "Text",
        "value": 21
      },
      {
        "label": "Website",
        "value": 2
      },
      {
        "label": "Rating",
        "value": 1
      },
      {
        "label": "Factor",
        "value": 3
      },
      {
        "label": "Cases",
        "value": 5
      },
      {
        "label": "Hours",
        "value": 1
      }
    ],
    "color": "#32B6E8"
  },
  {
    "key": "Jake",
    "values": [
      {
        "label": "Name",
        "value": 58
      },
      {
        "label": "Surname",
        "value": 58
      },
      {
        "label": "Phone",
        "value": 56
      },
      {
        "label": "Text",
        "value": 58
      },
      {
        "label": "Website",
        "value": 30
      },
      {
        "label": "Rating",
        "value": 1
      },
      {
        "label": "Factor",
        "value": 2
      },
      {
        "label": "Cases",
        "value": 1
      },
      {
        "label": "Hours",
        "value": 2
      }
    ],
    "color": "#77B242"
  },
  {
    "key": "Steve",
    "values": [
      {
        "label": "Name",
        "value": 100
      },
      {
        "label": "Surname",
        "value": 100
      },
      {
        "label": "Phone",
        "value": 100
      },
      {
        "label": "Text",
        "value": 100
      },
      {
        "label": "Website",
        "value": 33
      },
      {
        "label": "Rating",
        "value": 2
      },
      {
        "label": "Factor",
        "value": 2
      },
      {
        "label": "Cases",
        "value": 0
      },
      {
        "label": "Hours",
        "value": 17
      }
    ],
    "color": "#FFA614"
  }
]

我需要做的是将每个键的值乘以 100。这是我得到的:

$.each(data, function(i,val){
      $.each(val.values, function(i,v){
        v.value = (v.value * 100);
      });
    });

高级开发人员告诉我,我可以用 更好地做到这一点$.map,如何?

4

1 回答 1

1

这是地图版本 - 尽管我不同意您的“高级”开发人员,但它更好:

var mappedObjects = $.map(data, function(val){
      return { 
          key: val.key, 
          values: $.map(val.values, function(v) { return { label: v.label, value: v.value * 100 }; }
      };
});

在我看来,您拥有的代码更加清晰,更不用说正如下面 Felix 提到的那样,您不必要地创建了许多新对象。

于 2013-06-05T15:58:35.220 回答