Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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 ?

share|improve this question
    
Can you share the code that you are using in your Angular service? Like a fiddler or plunkr would be helpful. – Pratik Bhattacharya Jul 15 at 4:40
    
up vote 0 down vote accepted

well, Here how you can do it,

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

I hope it helps you.

Cheers!

share|improve this answer
    
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. ? – Henriques Jul 15 at 4:36
    
If you can make a plunker, I will resolve your issue. Thanks – varit05 Jul 15 at 4:39
    
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. – Henriques Jul 15 at 5:18
    
plunkr link is not working. – Bon Macalindong Jul 15 at 5:27
    
There were two many issues with your plunker. Please find the working here: plnkr.co/edit/J652pCD7XMNDbT6qHIge?p=preview – varit05 Jul 15 at 5:28

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.