0

I have 2 sets of nested arrays:

var values = [[100, 87.5, 87.5, 87.5, 100, 100],
[87.5, 100, 100, 100, 87.5, 87.5],
[75, 75, 75, 75, 75, 75],
[50, 50, 50, 62.5, 62.5, 62.5],
[62.5, 62.5, 62.5, 50, 37.5, 50],
[0, 0, 0, 0, 0, 0]];

var date = [["2015", "2004", "2015", "2015", "2015", "2015"],
["2015", "2004", "2015", "2015", "2015", "2015"],
["2015", "2004", "2015", "2015", "2015", "2015"],
["2015", "2004", "2015", "2015", "2015", "2015"],
["2015", "2004", "2015", "2015", "2015", "2015"],
["2015", "2004", "2015", "2015", "2015", "2015"]];

What I'm trying to do is combine each array in Values and Date:

Get this :[100, 87.5, 87.5, 87.5, 100, 100]
Get this :["2015", "2004", "2015", "2015", "2015", "2015"]

Then combine like this:

[{y: 100, d:2015},{y:87.5, d: 2004},{y:87.5, d:2015}},{y:87.5, d:2015}},{y:100, d:2015}},{y:100, d:2015}]

example: https://jsfiddle.net/zidski/5808pgs4/3/

 var result = values.map(function (n, i) {
            return ({ y: n, d: values[i] });
        });

But the whole array gets added.

5
  • 1
    please add the wanted result as well. Commented Sep 18, 2017 at 14:27
  • 1
    @NinaScholz - "Then combine like this" - I think the OP did. Commented Sep 18, 2017 at 14:27
  • It sounds like you need to use a flatMap style function, and maybe a reduce. reactivex.io/learnrx Commented Sep 18, 2017 at 14:29
  • The "then combine like this" isn't clear, it doesn't match up with the data in the arrays of arrays, and it's not clear how that mapping is supposed to get flattened. Commented Sep 18, 2017 at 14:31
  • you need two nested Array#map calls. Like grid.map((row, y) => row.map((item, x) => ... )) Commented Sep 18, 2017 at 14:32

5 Answers 5

1

You need to iterate over the inner array as well.

var date = [  ["2015", "2004", "2015", "2015", "2015", "2015"],  ["2015", "2004", "2015", "2015", "2015", "2015"],  ["2015", "2004", "2015", "2015", "2015", "2015"],  ["2015", "2004", "2015", "2015", "2015", "2015"],  ["2015", "2004", "2015", "2015", "2015", "2015"],  ["2015", "2004", "2015", "2015", "2015", "2015"]];

var values = [  [100, 87.5, 87.5, 87.5, 100, 100],  [87.5, 100, 100, 100, 87.5, 87.5],  [75, 75, 75, 75, 75, 75],  [50, 50, 50, 62.5, 62.5, 62.5],  [62.5, 62.5, 62.5, 50, 37.5, 50],  [0, 0, 0, 0, 0, 0]];

var result = values
  // iterate over the values array
  .map(function(arr, i1) {
    // iterate over the inner array
    // in case you just want the first element as in your example then 
    // remove the first map method and replace `arr` with `values[0]` and `date[i1][i2]`  with `date[0][i2]` 
    return arr.map(function(n, i2) {
      // generate the required array object based on index
      return {
        y: n,
        // get the period from 2d array date using index
        periods: date[i1][i2] 
      }
    });
  });



document.getElementById("data").innerHTML = JSON.stringify(result, null, 3);
<pre id="data"></pre>

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

Comments

1

You could use a nested mapping for the values.

var values = [[100, 87.5, 87.5, 87.5, 100, 100], [87.5, 100, 100, 100, 87.5, 87.5], [75, 75, 75, 75, 75, 75], [50, 50, 50, 62.5, 62.5, 62.5], [62.5, 62.5, 62.5, 50, 37.5, 50], [0, 0, 0, 0, 0, 0]],
    date = [["2015", "2004", "2015", "2015", "2015", "2015"], ["2015", "2004", "2015", "2015", "2015", "2015"], ["2015", "2004", "2015", "2015", "2015", "2015"], ["2015", "2004", "2015", "2015", "2015", "2015"], ["2015", "2004", "2015", "2015", "2015", "2015"], ["2015", "2004", "2015", "2015", "2015", "2015"]],
    result = values.map(function (a, i) {
        return a.map(function (b, j) {
            return { d: date[i][j], y: b };
        });
    });
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

4 Comments

Except that's not what the example output is. (Because as you pointed out, the example output is unclear.)
@T.J.Crowder, some say so, others no.
(Geez, people, I woudn't downvote the answer because it's a bit speculative...)
@NinaScholz that's a nifty trick you did there with that css rule. Haven't seen that before :)
0

You can flatten both of the arrays and then it's easy (assuming they are both of the same length):

var date = [["2015", "2004", "2015", "2015", "2015", "2015"],
    ["2015", "2004", "2015", "2015", "2015", "2015"],
    ["2015", "2004", "2015", "2015", "2015", "2015"],
    ["2015", "2004", "2015", "2015", "2015", "2015"],
    ["2015", "2004", "2015", "2015", "2015", "2015"],
    ["2015", "2004", "2015", "2015", "2015", "2015"]];
    
    var values = [[100, 87.5, 87.5, 87.5, 100, 100],
    [87.5, 100, 100, 100, 87.5, 87.5],
    [75, 75, 75, 75, 75, 75],
    [50, 50, 50, 62.5, 62.5, 62.5],
    [62.5, 62.5, 62.5, 50, 37.5, 50],
    [0, 0, 0, 0, 0, 0]];
    
    var flatValues = [].concat.apply([], values);
    var flatDates = [].concat.apply([], date);
    
     var result = flatValues.map(function (n, i) {
                return ({ y: n, periods: flatDates[i] });
            });
            
    
    
    document.getElementById("data").innerHTML = JSON.stringify(result);
<pre id="data"></pre>

Comments

0

Probably what you want is this:

 //the concat will flatten the arrays.
 var result = [].concat.apply([],dates.map(function (subDates, i) {
      //Go throught the sub dates
      return subDates.map(function(date, j){
         //Create the object
         return { y: date, periods: values[i][j] };

     })
 }));

console.log(result);

//Output:
//[{"y":"2015","periods":100},{"y":"2004","periods":87.5},{"y":"2015","periods":87.5},{"y":"2015","periods":87.5},{"y":"2015","periods":100},...]

Comments

0

You might need to map it 2 times...

var resultData = date.map(function(item,indexDate){
  return item.map(function(year, indexYear){
     return {y:year,d:values[indexDate][indexYear]};
   }); 
});

you might also want to flatten the array look at this plunkr

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.