0

var items = [{
  //other properties... above
  item_name: [
    [1],
    [2, 3]
  ],
  item_description: [
    [1],
    [3, 4]
  ],
  item_quantity: [
    [1],
    [4, 5]
  ],
  item_value: null,
}, {
  //other properties... above
  item_name: 1,
  item_description: 2,
  item_quantity: 3,
  item_value: 4,
}, {
  //other properties... above
  item_name: [1, 2, 3],
  item_description: [1, 2, 3],
  item_quantity: [1, 2, 3],
  item_value: [1, 2, 3],
}];

var itemList = [];



items.forEach(function(item) {

  if (!_.isArray(item.item_name)) {
    itemList.push({
      name: item.item_name,
      description: item.item_description,
      quantity: item.item_quantity,
      value: item.item_value
    });
  }

  var names = item.item_name ? _.flatten(item.item_name) : [];
  var descriptions = item.item_description ? _.flatten(item.item_description) : [];
  var quantity = item.item_quantity ? _.flatten(item.item_quantity) : [];
  var values = item.item_value ? _.flatten(item.item_value) : [];

  names.forEach(function(name, index) {
    itemList.push({
      name: names[index],
      description: descriptions[index],
      quantity: quantity[index],
      values: values[index]
    });
  })
  

});
console.log(itemList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.1/underscore-min.js"></script>

is there a way I can perform this faster in underscore, to remove all of the flattens?

for each item in the array I am taking

item_name[i]
item_description[i]
item_quantity[i]
item_value[i]

and adding them to the itemList

item properties in items can be [[],[]] or [] or integer or null

currently it is outputting what is expected (unless a name is null and it can skip items) however I do not like all of the loops this is performing and I am wondering if I can make a better use of underscore library

1 Answer 1

1

You can use this:

var myKeys = ['name', 'description', 'quantity', 'value'];
var result = _.flatten(items.map(function(item) {
    return _.zip.apply(_, myKeys.map(function(key) {
        return _.flatten([item['item_'+key]]);
    })).map(function(arr) {
        return _.object(myKeys, arr);
    });
}));

Demo:

var items = [{
    //other properties... above
    item_name: [
        [1],
        [2, 3]
    ],
    item_description: [
        [1],
        [3, 4]
    ],
    item_quantity: [
        [1],
        [4, 5]
    ],
    item_value: null,
}, {
    //other properties... above
    item_name: 1,
    item_description: 2,
    item_quantity: 3,
    item_value: 4,
}, {
    //other properties... above
    item_name: [1, 2, 3],
    item_description: [1, 2, 3],
    item_quantity: [1, 2, 3],
    item_value: [1, 2, 3],
}];
var myKeys = ['name', 'description', 'quantity', 'value'];
var result = _.flatten(items.map(function(item) {
    return _.zip.apply(_, myKeys.map(function(key) {
        return _.flatten([item['item_'+key]]);
    })).map(function(arr) {
    	return _.object(myKeys, arr);
    });
}));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.1/underscore-min.js"></script>

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

Comments

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.