Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by curryingcurrying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return obj[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return obj[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return obj[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

Fixed issue with code snippet. Should be using the object passed into method, not the variable defined outside of method.
Source Link

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return input[k];obj[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return input[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return obj[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.

Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

When defining propertyArray, you don't need to handle undefined explicitly. (It would have been nice to use _.pluck(), but unfortunately it is designed to take arguments in the wrong order.)

_.object() is too delicious to pass up! You basically reimplemented it as createObject(). The function that is passed to map() can be further simplified by currying.

var input = {
  key1: [ 'val1', 'val2', 'val3' ],
  key2: [ 'val4', 'val5', 'val6' ],
  key3: [ 'val7', 'val8', 'val9' ]
};

window.alert("INPUT:\n" + JSON.stringify(input, null, '  '));

function zipObj(obj) {
  var keys = _.keys(obj);
  var values = _.map(keys, function(k) { return input[k]; });

  // Transpose the values matrix
  var valueSlices = _.zip.apply(_, values);
  return _.map(valueSlices, _.partial(_.object, keys));
}

window.alert("OUTPUT:\n" + JSON.stringify(zipObj(input), null, '  '));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

I suspect that var values = … could be simplified as _.values(obj), but I don't see anything in the Underscore.js documentation that guarantees that _.values(obj) will list the values in the same order as _.keys(obj), so I hesitate to recommend it.