angularjs数组内部数组的动态标签名称

时间:2016-09-22 14:44:50

标签: javascript angularjs arrays

我正在使用以下格式创建数组

angular.forEach(value.data, function(value1, key1) {
    shiftArrayList.push({
        shift: value1.shiftName
    });
    dataArrayList.push({
        safeDayCount: value1.safeDayCount,
        accidentCount: value1.accidentCount,
        hazardCount: value1.hazardCount,
        nearMissCount: value1.nearMissCount
    });
});

,dataArrayList的结果将是,

"safeDayCount": 0,
"accidentCount": 0,
"hazardCount": 39,
"nearMissCount": 0

然后继续。但是我需要在foreach中附加标签名称,并且我需要附加value1.shiftName的每个标签。比如safeDayCount + "_"+value1.shiftName。所以它就像safeDayCount_X, accidentCount_X, hazardCount_X, nearMissCount_X.。请帮我附上价值。

先谢谢,

1 个答案:

答案 0 :(得分:2)

您需要像这样准备对象

var props = [ "safeDayCount", "accidentCount", "hazardCount", "nearMissCount" ];
var obj = {};
props.forEach( function(item){
  obj[ item + "_" + value1.shiftName ] = value1[ item ];  
});

dataArrayList.push( obj );