hussain hussain - 3 months ago 9
Javascript Question

How can i push data elements to two array at same time?

I have a

$scope.event
where i am pushing data when i received from a server , I am working with the angularjs progress-bar so based on user interaction i want to start recording data so when data start recording i want to create new array like
$scope.progressbarData
so i can set dynamic value of the progressbar.

ctrl.js

$scope.event = [];
$scope.progressbarData = [];
$scope.dynamic = 0;
$scope.max = 200;
socket.on('ditConsumer',function (data) {
var obj = {
file:$scope.filename,
data:data
}
$scope.event.push(data);
if(sendMessageFlag === true) {
$scope.dynamic = $scope.event.length;
return sendMessageToFile(obj);

}
});


main.html

<div class="row">
<div class="col-md-10">
<uib-progressbar max="max" animate="true" value="dynamic">{{dynamic}} / {{max}}</uib-progressbar>
</div>
</div>

Answer

You can just add another Array.push() inside your event handler. This is readable and is nothing short of the standard approach in this situation.

$scope.event = [];
 $scope.progressbarData = [];
 $scope.dynamic = 0;
    $scope.max = 200;
    socket.on('ditConsumer',function (data) {
        var obj = {
            file:$scope.filename,
            data:data
        }
        $scope.event.push(data);
        $scope.progressbarData.push(data);
        if(sendMessageFlag === true) {
            $scope.dynamic = $scope.event.length;
           return  sendMessageToFile(obj);

        }
    });