0

how to push more than one element at one index of a array in javascript?

like i have

arr1["2018-05-20","2018-05-21"];
arr2[5,4];

i want resulted 4th array to be like:

arr4[["2018-05-20",5],["2018-05-21",4]];

tried pushing like this:

arr1.push("2018-05-20","2018-05-21");
arr1.push(5,4);

and then finally as:

arr4.push(arr1);

But the result is not as expected. Please someone help.

Actually i want to use this in zingChart as :

Options Data Create an options object, and add a values array of arrays.

Calendar Values In each array, provide the calendar dates with corresponding number values in the following format.

 options: {
  values: [
    ['YYYY-MM-DD', val1],
    ['YYYY-MM-DD', val2],
    ...,
    ['YYYY-MM-DD', valN]
  ]
}
Deepak Verma
  • 373
  • 7
  • 19
  • There can be only one element at a specified index of an `array`, in all the languages i know. But this element can be an object (even another array) that can contain other things inside it – Kaddath May 23 '18 at 07:51
  • You cannot do this: **_push more than one element at one index of a array_** – Randy Casburn May 23 '18 at 07:53
  • 2
    I think this is a case of [the XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Seblor May 23 '18 at 07:55
  • You have 2 expected results now with different formats. You might want to choose one. :) – Eddie May 23 '18 at 08:08
  • @Eddie it looks like your answer is what i wanted, but zingChart is not working when i passed the result as values. – Deepak Verma May 23 '18 at 08:13
  • 1
    @DeepakVerma I saw that you provided a sample output for zingChart but your initial or given arrays does not match the output. Where `YYYY-MM-DD` and `valX` from? – Eddie May 23 '18 at 08:15
  • @Eddie i showed here just an example, in my code i am using the values in correct format like ```2018-05-23, 5``` – Deepak Verma May 23 '18 at 08:20
  • But you only need 2 values for that, the `date` and the `value`, your code you have 3 arrays. – Eddie May 23 '18 at 08:21
  • @NinaScholz that 3rd value is an extra information that i want to provide please if you can see this docs: [link](https://www.zingchart.com/docs/chart-types/calendar-charts/) – Deepak Verma May 23 '18 at 08:24
  • Maybe it is not working because you are only passing the year and not the whole date. – Eddie May 23 '18 at 08:28
  • @Eddie edited with exactly what i'm try to do – Deepak Verma May 23 '18 at 08:37
  • @DeepakVerma That is the same with my answer, just remove the 3rd array – Eddie May 23 '18 at 08:38
  • it looks now like a duplicate of https://stackoverflow.com/questions/46767385/merge-two-of-one-dimensional-array-into-two-dimensional-array-javascript – Nina Scholz May 23 '18 at 08:45

2 Answers2

0

Your question is not correct at all, since you cannot push more than one element at the same index of an array. Your result is a multidimensional array:

[["2018-05-20",5],["2018-05-21",4]]
  • You have to create a multidimensional array collecting all your data (arrAll)
  • Then you create another multidimensional array (arrNew) re-arranging previous data

Try the following:

// Your Arrays
  var arr1 = ["2018-05-20","2018-05-21"];
  var arr2 = [5, 4];
  //var arr3 = [100, 20];
  var arrAll = [arr1, arr2]; 
  //var arrAll = [arr1, arr2, arr3]; 
  
  // New Array definition
  var arrNew = new Array;
  for (var j = 0; j < arr1.length; j++) {
    var arrTemp = new Array
    for (var i = 0; i < arrAll.length; i++) {
      arrTemp[i] = arrAll[i][j];
      if (i === arrAll.length - 1) {
        arrNew.push(arrTemp)
      }
    }
  }
  
  //New Array
  Logger.log(arrNew)
-1

Assuming the you want a multidimensional array, you can put all the input variables into an array. Use reduce and forEach to group the array based on index.

let arr1 = ["2018-05-20","2018-05-21"];
let arr2 = [5,4];

let arr4 = [arr1, arr2].reduce((c, v) => {
  v.forEach((o, i) => {
    c[i] = c[i] || [];
    c[i].push(o);
  });
  return c;
}, []);

console.log(arr4);
Eddie
  • 26,593
  • 6
  • 36
  • 58