How to map a multidimensional array (using Underscore)?

GloryOfThe80s

I have a large, multidimensional array of JSON objects that I want to map through (using Underscore). For example:

var dummyData = [
    [{title: 'a'},{title : 'b'}],
    [{title: 'a'},{title : 'b'}],
    [{title: 'a'},{title : 'b'}],
    [{title: 'a'},{title : 'b'}]
];

For the function body of the _.map, I want to run each JSON object through a Backbone Model constructor. So far, I've tried something like this to accomplish this:

_.map(dummyData, function() {
    _.each(dummyData, function(el, i) {
        // run each object through the constructor
    }
})

I'm getting caught up on the _.each, though - since dummyData isn't actually the 'list' that I want to loop through.

Or am I thinking about this wrong altogether?

thefourtheye

Iterate over the elements of dummyData, with _.map like this

_.map(dummyData, function(currentDummyData) {
    return _.map(currentDummyData, function(el, i) {
        // run each object through the constructor
    })
});

dummyData is an Array of Arrays. When you use _.map with that, it picks up each array in the array of arrays and passes to the function, which we accept with function(currentDummyData) {..}.

Inside that function, we again _.map that array, because it is still an array. So, we iterate that to get individual elements and pass them to the function function(el, i) {..}, where new Backbone models are created.

Note: You have to return the result of _.map, like in the answer. Because, _.map expects the function called to return an object and all the returned objects will be gathered to create a new array.

For example,

console.log(_.map(dummyData, function(currentDummyData) {
    return _.map(currentDummyData, function(el, i) {
        return {title: el.title + el.title};
    })
}));

will produce

[ [ { title: 'aa' }, { title: 'bb' } ],
  [ { title: 'aa' }, { title: 'bb' } ],
  [ { title: 'aa' }, { title: 'bb' } ],
  [ { title: 'aa' }, { title: 'bb' } ] ]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Return first index in multidimensional array using map

From Dev

underscore - transform an array with _.map()

From Dev

How to get duplicates in a JavaScript Array using Underscore

From Dev

Changing keys using array_map on multidimensional arrays using PHP

From Dev

Remove Duplicate Items From Multidimensional Array Using Javascript, jQuery, & underscore.js

From Dev

Moving a deep-set member of a multidimensional array tree - using lodash or underscore

From Dev

how to push array or objects into one array using underscore?

From Dev

how to split a multidimensional array using key in php

From Dev

How to retrieve multidimensional array using json in android

From Dev

How to print multidimensional array using PHP?

From Dev

How to access data in multidimensional array using jquery?

From Dev

How to retrieve a multidimensional array using json object?

From Dev

how to do multidimensional array intersection using lodash?

From Dev

how to echo multidimensional array using foreach?

From Dev

Using underscore in scala map function

From Dev

How would I apply the python map function to an multidimensional array

From Dev

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

From Java

How do you clone an array of objects using underscore?

From Dev

How to sort an array of objects depending on a custom sort order using underscore

From Dev

How to iterate an array synchronously using lodash, underscore or bluebird

From Dev

how to combine two array of Objects in javascript without using Underscore or Lodash?

From Dev

Map multidimensional array with join as proc

From Dev

Using foreach() with a multidimensional array

From Dev

how to map an array using React

From Dev

Array map, flatten, and unset on a single multidimensional array

From Dev

Convert a JSON to map using underscore in angular

From Dev

How to filter this multidimensional array?

From Dev

How to sort multidimensional array

From Dev

How to Group a multidimensional array

Related Related

  1. 1

    Return first index in multidimensional array using map

  2. 2

    underscore - transform an array with _.map()

  3. 3

    How to get duplicates in a JavaScript Array using Underscore

  4. 4

    Changing keys using array_map on multidimensional arrays using PHP

  5. 5

    Remove Duplicate Items From Multidimensional Array Using Javascript, jQuery, & underscore.js

  6. 6

    Moving a deep-set member of a multidimensional array tree - using lodash or underscore

  7. 7

    how to push array or objects into one array using underscore?

  8. 8

    how to split a multidimensional array using key in php

  9. 9

    How to retrieve multidimensional array using json in android

  10. 10

    How to print multidimensional array using PHP?

  11. 11

    How to access data in multidimensional array using jquery?

  12. 12

    How to retrieve a multidimensional array using json object?

  13. 13

    how to do multidimensional array intersection using lodash?

  14. 14

    how to echo multidimensional array using foreach?

  15. 15

    Using underscore in scala map function

  16. 16

    How would I apply the python map function to an multidimensional array

  17. 17

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

  18. 18

    How do you clone an array of objects using underscore?

  19. 19

    How to sort an array of objects depending on a custom sort order using underscore

  20. 20

    How to iterate an array synchronously using lodash, underscore or bluebird

  21. 21

    how to combine two array of Objects in javascript without using Underscore or Lodash?

  22. 22

    Map multidimensional array with join as proc

  23. 23

    Using foreach() with a multidimensional array

  24. 24

    how to map an array using React

  25. 25

    Array map, flatten, and unset on a single multidimensional array

  26. 26

    Convert a JSON to map using underscore in angular

  27. 27

    How to filter this multidimensional array?

  28. 28

    How to sort multidimensional array

  29. 29

    How to Group a multidimensional array

HotTag

Archive