0

I am creating array using below format,

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
    });
});

and the result for dataArrayList will be like,

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

and it continues. But i need to append the label name inside the foreach and for every label i need to append value1.shiftName. like safeDayCount + "_"+value1.shiftName. So it will be like safeDayCount_X, accidentCount_X, hazardCount_X, nearMissCount_X.. Please help me to append the value.

Thanks in Advance,

1 Answer 1

2

You need to prepare the object like this

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

dataArrayList.push( obj );
Sign up to request clarification or add additional context in comments.

1 Comment

it is not working it is showing Unexpected token +

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.