5

I have the follow object:

 formData : {
       _id: "550de8956e2d0948080e220f" 
       category: "Tag1"
       isFeatured: "Yes" 
       likeCount: 557 
       title: "Integrating WordPress with Your Website"
    }

I tried JavaScript but it returned a null value:

 var arryFormData = Array.prototype.slice.call(formData)

How can I convert formData into an array of just its values, not properties?

As in ...

 arryFormData = ["550de8956e2d0948080e220f", "Tag1", "Yes", 557, "Integrating WordPress with Your Website"]

5 Answers 5

5

or if You like to more functional code:

var arr = [];
angular.forEach(obj, function(value, key){
    arr.push(value);
});
Sign up to request clarification or add additional context in comments.

Comments

4

If you are using underscore.js,

_.values(formData)
// will get ["550de8956e2d0948080e220f", "Tag1", "Yes", 557, "Integrating WordPress with Your Website"]

See: here

Alternatively:

var res = [];
for (var x in formData){
   formData.hasOwnProperty(x) && res.push(formData[x])
}
console.log(res);

In any event, the array elements might not be in the order that you want.

Comments

3

I prefer one line solution with Object.keys() and es6 syntax, until Object.values() is not here

const values = Object.keys(obj).map(it => obj[it])

or in es5

var values = Object.keys(obj).map(function(it) { 
   return obj[it]
})

Comments

1

I think there's No magic way, you just have to use a for loop:

for (var key in obj) {
    values.push(obj[key])
}

To make it angular, you could use angular.forEach I guess...

Comments

0

This is how i have handled in Angular 5 to convert Object into Array as API gives response in JSON Object so we can convert it into array to use it.

    let tmepArr = {};
    Object.keys(res).forEach( key => {
        tmepArr['name'] = [res[key].name];
        tmepArr['id'] = [res[key].id];
    });
    this.marketplaceDropDown = [tmepArr];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.