Airea
Airea

Reputation: 179

How can I add an object in the loop when it checks that does not exist?

I'm trying to create an array of objects from the data I receive from an API. On the one hand, I receive the values in one array, and on the other, I receive the months each value is from in another array. I created an object for each month that appears in the response. However, I want to add the month that does not appear and add its value as 0.

Here is an example of the response of the API call which I saved in a variable and the code I've done to create the objects:

const data = [
    {
        "name": "1.0 CNG",
        "production": [
            139,
            174,
            112,
            121,
            67,
            105,
            0,
            121,
            92,
            98,
            91
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    },
    {
        "name": "1.0 MPI",
        "production": [
            116,
            124,
            94,
            130,
            54,
            55,
            0,
            71,
            42,
            48,
            41
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    },
    {
        "name": "1.0 TSI (95CV/115CV)",
        "production": [
            628,
            1699,
            1867,
            1539,
            941,
            1260,
            0,
            1449,
            1119,
            1178,
            1096
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    },
    {
        "name": "1.5 TSI 110KW",
        "production": [
            92,
            124,
            80,
            86,
            48,
            75,
            0,
            86,
            66,
            70,
            65
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    },
    {
        "name": "Diesel",
        "production": [
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    }]
    
    const finalData = data.map(e => {
       return e.production.map((obj, i) => {
         return {
            month: e.months[i],
            value: obj
         }
       })
    })

I wanted to add the following inside the second map but I've realized that due to the nature of the loop, that depends on the length of the array (in this case 11), so there will be always a month out:

e.production.map((obj, i) => {
  let count = 1;
  if (count == i) {
    count++;
    return {
     month: e.months[i],
     value: obj
    }
  } else {
    const month = count;
    count++;
    return {
    month: month,
    value:0
  }
 }
})

What I could do so in the end I have an array like this but with, for example in this case, with month: 1, value: 0?

    [[{
  month: 2,
  value: 139
}, {
  month: 3,
  value: 174
}, {
  month: 4,
  value: 112
}, {
  month: 5,
  value: 121
}, {
  month: 6,
  value: 67
}, {
  month: 7,
  value: 105
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 121
}, {
  month: 10,
  value: 92
}, {
  month: 11,
  value: 98
}, {
  month: 12,
  value: 91
}], [{
  month: 2,
  value: 116
}, {
  month: 3,
  value: 124
}, {
  month: 4,
  value: 94
}, {
  month: 5,
  value: 130
}, {
  month: 6,
  value: 54
}, {
  month: 7,
  value: 55
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 71
}, {
  month: 10,
  value: 42
}, {
  month: 11,
  value: 48
}, {
  month: 12,
  value: 41
}], [{
  month: 2,
  value: 628
}, {
  month: 3,
  value: 1699
}, {
  month: 4,
  value: 1867
}, {
  month: 5,
  value: 1539
}, {
  month: 6,
  value: 941
}, {
  month: 7,
  value: 1260
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 1449
}, {
  month: 10,
  value: 1119
}, {
  month: 11,
  value: 1178
}, {
  month: 12,
  value: 1096
}], [{
  month: 2,
  value: 92
}, {
  month: 3,
  value: 124
}, {
  month: 4,
  value: 80
}, {
  month: 5,
  value: 86
}, {
  month: 6,
  value: 48
}, {
  month: 7,
  value: 75
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 86
}, {
  month: 10,
  value: 66
}, {
  month: 11,
  value: 70
}, {
  month: 12,
  value: 65
}], [{
  month: 2,
  value: 0
}, {
  month: 3,
  value: 0
}, {
  month: 4,
  value: 0
}, {
  month: 5,
  value: 0
}, {
  month: 6,
  value: 0
}, {
  month: 7,
  value: 0
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 0
}, {
  month: 10,
  value: 0
}, {
  month: 11,
  value: 0
}, {
  month: 12,
  value: 0
}]]
☁️ "Running fiddle"
[[{
  month: 2,
  value: 139
}, {
  month: 3,
  value: 174
}, {
  month: 4,
  value: 112
}, {
  month: 5,
  value: 121
}, {
  month: 6,
  value: 67
}, {
  month: 7,
  value: 105
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 121
}, {
  month: 10,
  value: 92
}, {
  month: 11,
  value: 98
}, {
  month: 12,
  value: 91
}], [{
  month: 2,
  value: 116
}, {
  month: 3,
  value: 124
}, {
  month: 4,
  value: 94
}, {
  month: 5,
  value: 130
}, {
  month: 6,
  value: 54
}, {
  month: 7,
  value: 55
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 71
}, {
  month: 10,
  value: 42
}, {
  month: 11,
  value: 48
}, {
  month: 12,
  value: 41
}], [{
  month: 2,
  value: 628
}, {
  month: 3,
  value: 1699
}, {
  month: 4,
  value: 1867
}, {
  month: 5,
  value: 1539
}, {
  month: 6,
  value: 941
}, {
  month: 7,
  value: 1260
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 1449
}, {
  month: 10,
  value: 1119
}, {
  month: 11,
  value: 1178
}, {
  month: 12,
  value: 1096
}], [{
  month: 2,
  value: 92
}, {
  month: 3,
  value: 124
}, {
  month: 4,
  value: 80
}, {
  month: 5,
  value: 86
}, {
  month: 6,
  value: 48
}, {
  month: 7,
  value: 75
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 86
}, {
  month: 10,
  value: 66
}, {
  month: 11,
  value: 70
}, {
  month: 12,
  value: 65
}], [{
  month: 2,
  value: 0
}, {
  month: 3,
  value: 0
}, {
  month: 4,
  value: 0
}, {
  month: 5,
  value: 0
}, {
  month: 6,
  value: 0
}, {
  month: 7,
  value: 0
}, {
  month: 8,
  value: 0
}, {
  month: 9,
  value: 0
}, {
  month: 10,
  value: 0
}, {
  month: 11,
  value: 0
}, {
  month: 12,
  value: 0
}]]

Thank you!

Upvotes: 0

Views: 139

Answers (2)

Mike Williamson
Mike Williamson

Reputation: 3260

I think the best thing to do is to first create the structure as you want it to look, then populate the structure.

In this case, you want to ensure that you have all twelve months. So why not first create that structure?

> const monthData = [...Array(13).keys()].slice(1).map(month => {
    return {
      month: month
    }}
  )

Now you will have an array with the objects you want:

> monthData
[
  { month: 1 },  { month: 2 },
  { month: 3 },  { month: 4 },
  { month: 5 },  { month: 6 },
  { month: 7 },  { month: 8 },
  { month: 9 },  { month: 10 },
  { month: 11 }, { month: 12 }
]

Then iterate through monthData and do a lookup for each element of the object that you have for that month. I am sorry, I would write the full thing, but I have gotten confused why you are creating both data and finalData, and where e comes from.

Upvotes: 0

dave
dave

Reputation: 64695

You could just check if each number 1 - 12 is in the months array, if not, add quantity 0, otherwise, get the month and quantity at that index:

const data = [
    {
        "name": "1.0 CNG",
        "production": [
            139,
            174,
            112,
            121,
            67,
            105,
            0,
            121,
            92,
            98,
            91
        ],
        "months": [
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12
        ]
    }
]

console.log(
data.reduce((carry, current) => {
    for (let i = 1; i <= 12; i++) {
        const index = current.months.indexOf(i);
        if (index === -1) {
            carry.push({ month: i, value: 0});
        } else {
           carry.push({ month: current.months[index], value: current.production[index] });
        }
    }
    current.months.forEach((month, index) => {
        
    });
    return carry;
}, [])
);

Upvotes: 1

Related Questions