Convert multi-dimensional array into single array (Javascript)

tmr

I have an object array (coming from an XLSX.js parser, so its length and contents vary) representing grants that have been given to projects.

Simplified, it looks something like this:

var grants = [
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
    { id: "p_2", location: "loc_2", type: "B", funds: "2000" },
    { id: "p_3", location: "loc_3", type: "C", funds:  "500" },
    { id: "p_2", location: "_ibid", type: "D", funds: "1000" },
    { id: "p_2", location: "_ibid", type: "E", funds: "3000" }
];

I need to merge these into a new array that will look like this:

var projects = [
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
    { id: "p_2", location: "loc_2", type: ["B", "D", "E"], funds: ["2000", "1000", "3000"] },
    { id: "p_3", location: "loc_3", type: "C", funds: "500" }
];

... so that when the id is the same, it will merge the objects and combine some of their key values (in the example type and funds) into a simple sub-array. The other keys (location) in these merged objects inherit the values from the first instance and ignore the rest.

After several failed attempts and a lot of searching online, I got an idea from this answer to loop through grants like this:

var res = {};

$.each(grants, function (key, value) {
    if (!res[value.id]) {
        res[value.id] = value;    
    } else {
        res[value.id].type = [res[value.id].type, value.type];
        res[value.id].funds = [res[value.id].funds, value.funds];
    }
});

var projects = []
projects = $.map( res, function (value) { return value; } );

It actually works perfectly, EXCEPT that as I need an array, I removed .join(',') from the ends (from the answer mentioned above), which in turn has created the problem I can't seem to solve now. The sub-arrays become nested in each other somehow if there is at least three items in them! I sort of understand why (the loop), but I wonder if there is a way to convert all these little multi-dimensional arrays inside the objects into sigle arrays (like: type: ["B", "D", "E"] )?

var grants = [
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
    { id: "p_2", location: "loc_2", type: "B", funds: "2000" },
    { id: "p_3", location: "loc_3", type: "C", funds:  "500" },
    { id: "p_2", location: "_ibid", type: "D", funds: "1000" },
    { id: "p_2", location: "_ibid", type: "E", funds: "3000" }
];

var res = {};

$.each(grants, function (key, value) {
    if (!res[value.id]) {
        res[value.id] = value;    
    } else {
        res[value.id].type = [res[value.id].type, value.type];
        res[value.id].funds = [res[value.id].funds, value.funds];
    }
});

var projects = []
projects = $.map( res, function (value) { return value; } );


$("pre").html(JSON.stringify(projects,null,2));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="json"></pre>

KooiInc

Would this be an idea?

var grants = [
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
    { id: "p_2", location: "loc_2", type: "B", funds: "2000" },
    { id: "p_3", location: "loc_3", type: "C", funds:  "500" },
    { id: "p_2", location: "_ibid", type: "D", funds: "1000" },
    { id: "p_2", location: "_ibid", type: "E", funds: "3000" }
];
var joined = [];

// map and push to joined
grants.map(
  function (v) {
    if (!(v.id in this)) {
      this[v.id] = v;
      joined.push(v);
    } else {
      var current = this[v.id];
      current.type = [v.type].concat(current.type);
      current.funds = [v.funds].concat(current.funds);
    }
  }, {}
);

// show it
document.querySelector('#result').textContent =
   JSON.stringify(joined, null, ' ');
<pre id="result"></pre>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert a deep multi dimensional array to a single dimensional array - Javascript

From Dev

How to convert a deep multi dimensional array to a single dimensional array - Javascript

From Dev

Convert multi-dimensional array into single array (Javascript)

From Dev

How to convert multi dimensional array to single dimensional array in php ?

From Dev

Convert Array of Objects to Multi Dimensional Array in JavaScript

From Dev

Convert object to multi-dimensional array - JavaScript

From Dev

Convert Multi dimensional array PHP

From Dev

Split Multi Dimensional array into single array :PHP

From Dev

Split Multi Dimensional array into single array :PHP

From Dev

Transform multi dimensional array to single array

From Dev

Javascript - Convert string into a multi-dimensional array and then display the output

From Dev

Incrementing Multi Dimensional Array in JavaScript

From Dev

Javascript multi dimensional array issue

From Dev

Multi-dimensional array indexing using a single dimensional array in Python

From Dev

Single dynamic allocation for multi-dimensional array

From Java

Convert dataframe to Multi-dimensional array - R

From Dev

php convert a string into multi dimensional array

From Dev

Convert a Multi-Dimensional Array to HTML Table

From Dev

convert a string to multi dimensional array in php

From Dev

Convert multi dimensional array to organized arrays in PHP

From Dev

PHP How to split multi dimensional array into single array?

From Dev

Multi dimensional array cumulative sum in Javascript

From Dev

Sorting multi dimensional array by 2 columns - JavaScript

From Dev

Javascript - Combining multi-dimensional array with index

From Dev

Creating multi dimensional array with map method: javascript

From Dev

How to create javascript multi dimensional array?

From Dev

Multi-dimensional Javascript open array

From Dev

How to implement multi dimensional array in javascript

From Dev

Transpose convert column values of multi dimensional array into flat array

Related Related

  1. 1

    How to convert a deep multi dimensional array to a single dimensional array - Javascript

  2. 2

    How to convert a deep multi dimensional array to a single dimensional array - Javascript

  3. 3

    Convert multi-dimensional array into single array (Javascript)

  4. 4

    How to convert multi dimensional array to single dimensional array in php ?

  5. 5

    Convert Array of Objects to Multi Dimensional Array in JavaScript

  6. 6

    Convert object to multi-dimensional array - JavaScript

  7. 7

    Convert Multi dimensional array PHP

  8. 8

    Split Multi Dimensional array into single array :PHP

  9. 9

    Split Multi Dimensional array into single array :PHP

  10. 10

    Transform multi dimensional array to single array

  11. 11

    Javascript - Convert string into a multi-dimensional array and then display the output

  12. 12

    Incrementing Multi Dimensional Array in JavaScript

  13. 13

    Javascript multi dimensional array issue

  14. 14

    Multi-dimensional array indexing using a single dimensional array in Python

  15. 15

    Single dynamic allocation for multi-dimensional array

  16. 16

    Convert dataframe to Multi-dimensional array - R

  17. 17

    php convert a string into multi dimensional array

  18. 18

    Convert a Multi-Dimensional Array to HTML Table

  19. 19

    convert a string to multi dimensional array in php

  20. 20

    Convert multi dimensional array to organized arrays in PHP

  21. 21

    PHP How to split multi dimensional array into single array?

  22. 22

    Multi dimensional array cumulative sum in Javascript

  23. 23

    Sorting multi dimensional array by 2 columns - JavaScript

  24. 24

    Javascript - Combining multi-dimensional array with index

  25. 25

    Creating multi dimensional array with map method: javascript

  26. 26

    How to create javascript multi dimensional array?

  27. 27

    Multi-dimensional Javascript open array

  28. 28

    How to implement multi dimensional array in javascript

  29. 29

    Transpose convert column values of multi dimensional array into flat array

HotTag

Archive