underscore js mapping array of objects multiple properties to new array

user2950720

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

Ismail RBOUH

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Underscore.js, remove duplicates in array of objects based on key value

From Dev

How to find a string from array of objects using underscore js?

From Dev

How can i merge array of objects with underscore js

From Dev

group objects array underscore js

From Dev

Sort array of objects by value using underscore.js

From Dev

Underscore.js get an array of unique objects

From Dev

Object Array Filtering On Multiple Properties With underscore.js

From Dev

Convert array of objects into array of properties

From Dev

Convert array of objects and their properties to array

From Dev

Remove Multiple Items from Array using underscore.js?

From Dev

Return objects with duplicate properties to a new array

From Dev

Mapping multiple Array in React.js

From Dev

Sort an array of objects by multiple properties

From Dev

filter array of objects with underscore and get new array of values

From Dev

Create array from values of different objects using Underscore.js

From Dev

Flat mapping and projecting an array with Underscore

From Dev

Underscore.js sort an array of objects alphanumerically

From Dev

Underscore JS difference on array and array with objects

From Dev

convert multiple arrays objects to single array, underscore

From Dev

underscore js : compare key object to array keys objects if exist remove existing add new

From Dev

Remove objects from an array using underscore js or lodash

From Dev

Object Array Filtering On Multiple Properties With underscore.js

From Dev

Search the String inside array of Objects using underscore.js

From Dev

Merge array of objects with underscore

From Dev

Array to multiple object in underscore

From Dev

Underscore have some function to extract nested array of objects to new array?

From Dev

Array of properties of objects in an array

From Dev

Manipulation of array of objects in javascript using lodash or underscore.js

From Dev

how to set new properties in an array of objects explicitly

Related Related

  1. 1

    Underscore.js, remove duplicates in array of objects based on key value

  2. 2

    How to find a string from array of objects using underscore js?

  3. 3

    How can i merge array of objects with underscore js

  4. 4

    group objects array underscore js

  5. 5

    Sort array of objects by value using underscore.js

  6. 6

    Underscore.js get an array of unique objects

  7. 7

    Object Array Filtering On Multiple Properties With underscore.js

  8. 8

    Convert array of objects into array of properties

  9. 9

    Convert array of objects and their properties to array

  10. 10

    Remove Multiple Items from Array using underscore.js?

  11. 11

    Return objects with duplicate properties to a new array

  12. 12

    Mapping multiple Array in React.js

  13. 13

    Sort an array of objects by multiple properties

  14. 14

    filter array of objects with underscore and get new array of values

  15. 15

    Create array from values of different objects using Underscore.js

  16. 16

    Flat mapping and projecting an array with Underscore

  17. 17

    Underscore.js sort an array of objects alphanumerically

  18. 18

    Underscore JS difference on array and array with objects

  19. 19

    convert multiple arrays objects to single array, underscore

  20. 20

    underscore js : compare key object to array keys objects if exist remove existing add new

  21. 21

    Remove objects from an array using underscore js or lodash

  22. 22

    Object Array Filtering On Multiple Properties With underscore.js

  23. 23

    Search the String inside array of Objects using underscore.js

  24. 24

    Merge array of objects with underscore

  25. 25

    Array to multiple object in underscore

  26. 26

    Underscore have some function to extract nested array of objects to new array?

  27. 27

    Array of properties of objects in an array

  28. 28

    Manipulation of array of objects in javascript using lodash or underscore.js

  29. 29

    how to set new properties in an array of objects explicitly

HotTag

Archive