admin管理员组

文章数量:1401484

Let's say I have an object that looks like this: {first: "asdasd", second: "asdas", third: "dasdas", four: "sdasa"} I need to convert this object into array.

if(values){
  var first=values.first;
  var second=values.second;
  var third=values.third;
  var four=values.four;
  var five=values.five;
  var six=values.six;
  var seven=values.seven;
  var eight=values.eight;
  var nine=values.nine;
  var ten=values.ten;

  if(first){
    userData.push(first);
  }
  if(second){
    userData.push(second);
  }
  if(third){
    userData.push(third);
  }
  if(four){
    userData.push(four);
  }
  if(five){
    userData.push(five);
  }
  if(six){
    userData.push(six);
  }
  if(seven){
    userData.push(seven);
  }
  if(eight){
    userData.push(eight);
  }
  if(nine){
    userData.push(nine);
  }
  if(ten){
    userData.push(ten);
  }
  console.log(userData);

I am currently doing by this code but i think it is the wrong approach. So how could i change this to array that looks like ["asdasd", "asdas", "dasdas", "sdasa", "asdasd", "asdas", "dasdas", "sdasa"]. In ionic drag and drop directive doesnot work when i use object in ng-repeat.And works perfectly when i apply array to ng-repeat.

Let's say I have an object that looks like this: {first: "asdasd", second: "asdas", third: "dasdas", four: "sdasa"} I need to convert this object into array.

if(values){
  var first=values.first;
  var second=values.second;
  var third=values.third;
  var four=values.four;
  var five=values.five;
  var six=values.six;
  var seven=values.seven;
  var eight=values.eight;
  var nine=values.nine;
  var ten=values.ten;

  if(first){
    userData.push(first);
  }
  if(second){
    userData.push(second);
  }
  if(third){
    userData.push(third);
  }
  if(four){
    userData.push(four);
  }
  if(five){
    userData.push(five);
  }
  if(six){
    userData.push(six);
  }
  if(seven){
    userData.push(seven);
  }
  if(eight){
    userData.push(eight);
  }
  if(nine){
    userData.push(nine);
  }
  if(ten){
    userData.push(ten);
  }
  console.log(userData);

I am currently doing by this code but i think it is the wrong approach. So how could i change this to array that looks like ["asdasd", "asdas", "dasdas", "sdasa", "asdasd", "asdas", "dasdas", "sdasa"]. In ionic drag and drop directive doesnot work when i use object in ng-repeat.And works perfectly when i apply array to ng-repeat.

Share Improve this question asked Oct 30, 2016 at 11:26 Suraz KhanalSuraz Khanal 2221 gold badge5 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

There are several ways you could do it.

One would be using Object.keys:

Object.keys(values).forEach(function(key) {
   userData.push(values[key]);
});

You could also use for ... in like this:

for (var key in values) {
    userData.push(value[key]);
}

If your object has any properties inherited from prototype then it would also show, so you do need to check if property really belongs to this instance:

for (var key in values) { //proper way to iterate keys using for..in
    if(values.hasOwnProperty(key)) { 
        userData.push(value[key]);
    }
}

Since you're using angular, you could also use angular.forEach:

angular.forEach(values, function(value){
    userData.push(value);
});

and I think this is the cleanest solution for you.

I would suggest using javascript library underscorejs or lodash for this kind of array and objects manipulation and iteration ,as it seems to be most preferable way of doing it ,keeping your code clean. Its a simple javascript file that you can include in your project and you don't have to do any plex thing to make it work.Just plug and play.

If you do include underscore/lodash ,all you have to do is :-

var mycoll =  _.values(yourobjectvariable);

This will fetch all the values in that object and convert it into an array automatically for you.

http://underscorejs/#values

Or if you want to do it with plain javascript , i think the above Randall Flag's answer is suffice for it.

`

        if(values)
    {
Object.keys(values).forEach(function(key) {
   userData.push(values[key]);
});
    }

` Sometimes instead of a simple solution we think too much and end up with plex time taking solution ,which we can achieve with small simple code

本文标签: javascriptHow to convert object into array in angularjs IonicStack Overflow