Would love a _.toArray() function for objects #989

Closed
EvHaus opened this Issue Feb 27, 2013 · 6 comments

Comments

Projects
None yet
7 participants
@EvHaus

EvHaus commented Feb 27, 2013

I find myself constantly needing the following:

var obj = {a:0,b:1,c:3}
var result = SomeMagicFunction(obj)
// result: [{a:0},{b:1},{c:3}]

If would be great if that magic function was something underscore provided. Like a _.toArray() function.

Implementation is fairly simple. For instance:

var SomeMagicFunction = function(obj) {
var result = []
for (var key in obj) {
var o = {}
o[key] = obj[key]
result.push(o)
}
return result
}

@ming-codes

This comment has been minimized.

Show comment Hide comment
@ming-codes

ming-codes Mar 6, 2013

I think you're looking for _.map

_.map({ a: 0, b: 1, c: 3 }, function(value, key) {
  return {
    key: value
  }
})

I think you're looking for _.map

_.map({ a: 0, b: 1, c: 3 }, function(value, key) {
  return {
    key: value
  }
})
@spadgos

This comment has been minimized.

Show comment Hide comment
@spadgos

spadgos Mar 6, 2013

Contributor

It'd actually have to be:

_.map({ a: 0, b: 1, c: 3 }, function(value, key) {
  var obj = {};
  obj[key] = value;
  return obj;
})
Contributor

spadgos commented Mar 6, 2013

It'd actually have to be:

_.map({ a: 0, b: 1, c: 3 }, function(value, key) {
  var obj = {};
  obj[key] = value;
  return obj;
})
@matt-y

This comment has been minimized.

Show comment Hide comment
@matt-y

matt-y Mar 11, 2013

Am I understanding correctly that you want something that (sort of mimics what the toArray does in underscore's collections util, but instead of raw values they should be a list of {key: value} objects? I think in this instance I would argue that this could be left out of the library, and instead be something user defined. The tools are there to make this easy (see above).

matt-y commented Mar 11, 2013

Am I understanding correctly that you want something that (sort of mimics what the toArray does in underscore's collections util, but instead of raw values they should be a list of {key: value} objects? I think in this instance I would argue that this could be left out of the library, and instead be something user defined. The tools are there to make this easy (see above).

@aaylward

This comment has been minimized.

Show comment Hide comment
@aaylward

aaylward Jun 17, 2013

You could also _.pairs use http://underscorejs.org/#pairs

You could also _.pairs use http://underscorejs.org/#pairs

@mlanza

This comment has been minimized.

Show comment Hide comment
@mlanza

mlanza Jun 20, 2013

Contributor

I have actually been using this idea myself. I had already implemented this in my codebase as Object.split and it does exactly what you suggest, breaks an object apart into tiny single-property objects.

I have found this useful (over the suggestion to use _.pairs) in situations where I'm calling a method with a set of these tiny objects and that method expects an object not an array.

Another option: you could create a custom helper

function piece(value, key) {
  var obj = {};
  obj[key] = value;
  return obj;
}

so that map will suffice.

_.map({ a: 0, b: 1, c: 3 }, _.piece)
Contributor

mlanza commented Jun 20, 2013

I have actually been using this idea myself. I had already implemented this in my codebase as Object.split and it does exactly what you suggest, breaks an object apart into tiny single-property objects.

I have found this useful (over the suggestion to use _.pairs) in situations where I'm calling a method with a set of these tiny objects and that method expects an object not an array.

Another option: you could create a custom helper

function piece(value, key) {
  var obj = {};
  obj[key] = value;
  return obj;
}

so that map will suffice.

_.map({ a: 0, b: 1, c: 3 }, _.piece)
@jashkenas

This comment has been minimized.

Show comment Hide comment
@jashkenas

jashkenas Jun 24, 2013

Owner

Neat idea, but I don't see how breaking apart an object into an array of single key-valued pairs is useful in real code. Isn't it always easier to simply iterate over the object itself? Make your case for your use case, if you'd like.

Owner

jashkenas commented Jun 24, 2013

Neat idea, but I don't see how breaking apart an object into an array of single key-valued pairs is useful in real code. Isn't it always easier to simply iterate over the object itself? Make your case for your use case, if you'd like.

@jashkenas jashkenas closed this Jun 24, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment