-1

How would I write the code below in function form?

var obj = {
  name : 'Tim',
  age : 20,
  hasPets : false
};
console.log(Object.keys(obj).map(function(item){
    return obj[item];
})); //=> [ 'Tim', 20, false ]

For example--this type of function form:

 function objectToArray(obj) {
//code here
};

Thank you!

  • Shouldn't it be called `objToArray`? – Barmar Feb 20 '17 at 09:43
  • Simply putting your clog code into the arrayToObject function ? What are exactly I/O you expect ? – Digix Feb 20 '17 at 09:44
  • Your question is confusing. Do you want object to array (as the title suggests), or array to object (as the example function name you provide suggests)? – rasmeister Feb 20 '17 at 09:44
  • so simple to realize ... – RomanPerekhrest Feb 20 '17 at 09:44
  • What you want is this [http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array](http://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array) – psycho Feb 20 '17 at 09:49
  • 1
    Yes, I meant objectToArray. I just edited it. Sorry! I'm brand new to coding so it may be simple to you but I'm just learning. :) Thanks so much! – learninghowtocode Feb 20 '17 at 09:49
  • @Cerbus Are you sure that this is a dupe. OP know how to get values out of object, he is just trying to wrapping that thing in a function. Reopening. – Suresh Atta Feb 20 '17 at 09:59
  • 1
    @sᴜʀᴇsʜᴀᴛᴛᴀ: At least ___close___ it as dupe of something like that then. The question ___is___ a dupe. The target I chose does literally the same. Don't re-open stuff like this. Even if the close reason isn't a "perfect" match, we don't need dozens of answers on simple stuff like wrapping a few lines in a function. – Cerbrus Feb 20 '17 at 10:00
  • @Cerbrus Yeah. Partially agreed. The term 'simple' is subjective. And unless I find the dupe answers the question 100%, I don't mark questions as duplicate. – Suresh Atta Feb 20 '17 at 10:08
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: The dupe _does_ answer the question. It does literally the same as the OP needs. I didn't get the JS Mjölnir for no reason. Don't re-open a question if it should be closed. – Cerbrus Feb 20 '17 at 10:11

3 Answers3

1

This should do

 function getMappedItems(obj) {
  var result =  Object.keys(obj).map(function(item){
    return obj[item];
  });
 return result;
};

Can reduce the above function a bit, but just elaborated as you learning.

https://jsfiddle.net/sureshatta/be7q88qv/

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1
 var objectToArray= function (obj) {
      return Object.keys(obj).map(function(item){
          return obj[item];
      }); 
 };
beta-developper
  • 1,689
  • 1
  • 13
  • 24
0

You should use Object.prototype.objectToArray for a elegant way.

var obj = {
  name : 'Tim',
  age : 20,
  hasPets : false
};

Object.prototype.objectToArray=function(){
    return Object.keys(obj).map(function(item){
        return obj[item];
    }); 
};
console.log(obj.objectToArray())
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128