0

I have two arrays. One contains a series of dates. The other array contains a series of data. What is the best way to combine the two arrays into one array of objects.

As an example the first element of the first array will be in the same object as the first element in the second array.

HERE ARE THE TWO ARRAYS

const datesArray = [
  "2017-01-01",
  "2017-01-02",
  "2017-01-03",
  "2017-01-04",
  "2017-01-05",
  "2017-01-06",
  "2017-01-07",
  "2017-01-08",
  "2017-01-09",
  "2017-01-10",
  "2017-01-11",
  "2017-01-12",
  "2017-01-13",
  "2017-01-14",
  "2017-01-15",
  "2017-01-16",
  "2017-01-17",
  "2017-01-18",
  "2017-01-19",
  "2017-01-20",
  "2017-01-21",
  "2017-01-22",
  "2017-01-23",
  "2017-01-24",
  "2017-01-25",
  "2017-01-26",
  "2017-01-27",
  "2017-01-28",
  "2017-01-29",
  "2017-01-30",
  "2017-01-31",
];

const snowfallArray = [
  0.98, 0, 0, 0, 0, 0, 0.35, 0.42, 0, 0.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

HERE IS MY EXPECTED OUTPUT

let combinedArray = [{
date: *first element from dates array*,
snowFall: *first element from snowfall array*
},
{
date: *second element from dates array*,
snowFall: *second element from snowfall array*
}]

EXPECTED OUTPUT EX: let combinedArray = [{date: '2017-01-01', snowFall: 0.98},{date: '2017-01-02', snowFall: 0}]

I do not even know where to begin..

Spectric
  • 30,714
  • 6
  • 20
  • 43

2 Answers2

1

Assuming the arrays are of equal lengths, then this should work:

const datesArray = [
  "2017-01-01",
  "2017-01-02",
  "2017-01-03",
  "2017-01-04",
  "2017-01-05",
  "2017-01-06",
  "2017-01-07",
  "2017-01-08",
  "2017-01-09",
  "2017-01-10",
  "2017-01-11",
  "2017-01-12",
  "2017-01-13",
  "2017-01-14",
  "2017-01-15",
  "2017-01-16",
  "2017-01-17",
  "2017-01-18",
  "2017-01-19",
  "2017-01-20",
  "2017-01-21",
  "2017-01-22",
  "2017-01-23",
  "2017-01-24",
  "2017-01-25",
  "2017-01-26",
  "2017-01-27",
  "2017-01-28",
  "2017-01-29",
  "2017-01-30",
  "2017-01-31",
];

const snowfallArray = [
  0.98, 0, 0, 0, 0, 0, 0.35, 0.42, 0, 0.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

const combined = snowfallArray.map((snowfall, index) => ({snowfall, date: datesArray[index]}));

console.log(combined);
Tomer Ariel
  • 1,397
  • 5
  • 9
0

assuming your array are the same length:

    const datesArray = [
  "2017-01-01",
  "2017-01-02",
  "2017-01-03",
  "2017-01-04",
  "2017-01-05",
  "2017-01-06",
  "2017-01-07",
  "2017-01-08",
  "2017-01-09",
  "2017-01-10",
  "2017-01-11",
  "2017-01-12",
  "2017-01-13",
  "2017-01-14",
  "2017-01-15",
  "2017-01-16",
  "2017-01-17",
  "2017-01-18",
  "2017-01-19",
  "2017-01-20",
  "2017-01-21",
  "2017-01-22",
  "2017-01-23",
  "2017-01-24",
  "2017-01-25",
  "2017-01-26",
  "2017-01-27",
  "2017-01-28",
  "2017-01-29",
  "2017-01-30",
  "2017-01-31",
];

const snowfallArray = [
  0.98, 0, 0, 0, 0, 0, 0.35, 0.42, 0, 0.14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];

const results = [];

for (let i=0; i< snowfallArray.length; i++){

    results.push({
        date: datesArray[i],
        snowFall: snowfallArray[i]
    })
}

console.log(results);
Suhail
  • 371
  • 2
  • 10