1

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.

3 Answers 3

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.

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

2 Comments

Thank you very much @Randall Flagg .
You can accept this answer and upvote it if you like it.
2

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 complex 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.org/#values

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

Comments

1

`

        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 complex time taking solution ,which we can achieve with small simple code

2 Comments

Thanks @Parshuram for your help.
Welcome my friend

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.