javascript - iterate through array to reformat -


I am trying to get some array data in a particular format so that I can use it. But I can not get it right

I have the format right now

  // format [date, id, var data = [["2014-04-01", "1", "100" ], ["2014-04-04", "1", "200"], ["2014-04-03", "1", "150"], ["2014-04-04", "1" , "3"], ["2014-04-01", "2", "200"], ["2014-04-02", "2", "600"], ["2014-04-03" , "3", "99"], ["03", "2", "15"], ["2014-04-04", "2", "25"], ["2014-04-01" 2014-04-02 "," 3 "," 85 "], [" 2014-04-03 "," 3 "," 555 "], [" 2014-04-04 "," 3 "," 0 " ]];   

I need to get it in the format:

  var reformatted = [['Date', '1', '2' , '3'], ['2014-04-01', 100, 200, 99], ['2014-04-02', 200, 600, 85], ['2014-04-03', 150, 15 , 555], ['2014-04-04', 5, 25, 0]]; var graph = [["Date"]]; // The array of the first element of the // array of the array To be populated with the second element always drawn in the array, it will be a line of graph for {var i = 0; i & lt; data.length; i ++) {if (graph [0]. Indexoff ( Data [i] [1]) & lt; 0) {graph [0]. Push (data [i] [1]); }}   

It puts me in a very good place 1: "1" 2: "2" 3: "3"

  Aare [1]] 0: Array [4] 0: "Date" ex> 

But I have been stumped how to get the remaining data in the proper format. Any ideas?

Trying it is not a good result.

logic:

First run it again initial array Find exclusive dates.

  [['Date'], ['2014-04-01'], ['2014-04-02'], ['2014-04-03'], ['2014 -04-04 ']]   

Convert the generated orange again, such as to find the unique numbers again and again through the initial array. Also create a list of unique numbers. ['2014-04-01'], ['2014 - 2014'] [[2014 '04-02'], ['2014-04-03'], ['2014-04-04']]

Now repeat from above, and for each item, the number to match the list and the initial array where the date and number are found Keep the matches in the above array Keep the tap if not found . You should get the following: I did it in php but not in javascript.

  [['date', '1', '2', '3'], ['2014-04-01', zero, 100, 200], '[2014-04 0, 0, 0, 0, 0] > 

Good luck!

Extra

in php:

  $ originalData = array (array ("2014-04-01", '1', '200'), array ("2014-04-02", '1', '300'), array ("2014 array (" 2014-04-04 ", '1', '200') , Array ("2014-04-01", '2', '400'), array ("2014-04-02", '2', '100'), array ("2014-04-03", " Array '(' 2014-04-02, '' 3 ',' 600 '), array (' 2 ',' 100 '"2014-04-" 3 ", '300'), array (" 2014-04-04 ", '3', '900')); Second phase will result:  
  $ array ('2014-04-01'), array ('2014-04-02'), array ('2014', 'array' ('date', '1', '2', '3'), array ('2014-04-01' 04-03 '), array (' 2014-04-04 '),);   

The list of numbers will be:

  $ numbers = array ( '1', '2', '3');   

I I will also do the third step:

  $ i = 0; foreign currency ($ graphData $ as graphDataItem) {if ($ graphDataItem [0]! = 'Date') {// First index Ignore $ j = 1; // 0's date column foreign currency ($ numbers $ numeral) {foreach ($ originalData as $ originalDataItem) {// Set the value to set (unless! Isset ($ graphData [$ i] [$ j])) $ graph data [$ i] [$ j] = null; If $ $ ($ $) = $ $ graph data [0] & amp; // check date match $ originaldatetime [1] == $ number) $ originalDataItem [2]; break; }} $ j ++; }} $ I ++; }   

The resulting $ graphData will be:

  array (0 = & gt; array (0 = & gt; 'date' 1 = & gt; 1 '2 =' 2 '3 = & gt; 3') 1 => Array (0 = & gt; '2014-04-01' 1 => '200' 2 => '400' 3 = & gt; '200') 2 => array (0 = & gt; '2014-04-02' 1 => '300' 2 => '100' 3 = & gt; 600 '3 = & gt; array (0 = & gt;' 2014-04-03 '1 =>' 400 '2 =>' 200 '3 =>' 300 ') = 4 = & Array (0 = & gt; '2014-04-04' 1 => '200' 2 => '100' 3 => '900'))   

You will get results in $ graph data above. However, for large sets it will be heavy on the processor.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -