0

my service returns data from DB, now i want to push particular values from that data in to an array using AngularJS.

var prodData = make a call to the service.

var versionsArray = [];

The data returned from the service looks similar format

{"prodId":"31123","prodName":"IPhone","Versions":[4,5,6]}

Now I want to push the values of the field "Versions":[4,5,6] in to an array called versionsArray, so that later I can bind that array to a drop-down and display the versions in the drop-down.

I wanted to something in the below way

 Versions.push({
 id: prodData.Versions,
 value: prodData.Versions
 });

How do I retrieve those values an push them in to an array ?

2

2 Answers 2

0

well, Here how you can do it,

angular.forEach(prodData.Versions, function(version) {
    versionsArray.push(version);
});

I hope it helps you.

Cheers!

Sign up to request clarification or add additional context in comments.

5 Comments

thank you, but prodData.Versions is showing as undefined.. when i call the service from the browser its showing the data as showed above in my post. ?
If you can make a plunker, I will resolve your issue. Thanks
I tried by best to create this plunk, plnkr.co/edit/MEZbNfBv0iFrehNPbRVe?p=preview hope it helps you helping me.. it looks similar to this.
There were two many issues with your plunker. Please find the working here: plnkr.co/edit/J652pCD7XMNDbT6qHIge?p=preview
Welcome! Hope it helps you
0

You can push data into an array like you normally would in plain javascript using array.push() method

View

<div ng-app>
  <div ng-controller="sampleCtrl">
    <select ng-model="yourModel" ng-options="ver as ver for ver in    
     versionArray">
      <option value="">Select a version</option>
    </select>
  </div>
</div>

Controller

function sampleCtrl($scope) {
  var data = {
    "prodId": "31123",
    "prodName": "IPhone",
    "Versions": [4, 5, 6]
  }

  $scope.versionArray = [];
  angular.forEach(data.Versions, function(item) {
    $scope.versionArray.push(item);
  });
}

See fiddle here

Comments

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.