Javascript 当循环检查不存在的对象时,如何在循环中添加该对象?

Javascript 当循环检查不存在的对象时,如何在循环中添加该对象?,javascript,Javascript,我试图从API接收的数据中创建一个对象数组。一方面,我在一个数组中接收值,另一方面,我在另一个数组中接收每个值的月份。我为响应中显示的每个月创建了一个对象。但是,我想添加未显示的月份,并将其值添加为0 下面是我保存在变量中的API调用响应示例,以及我为创建对象所做的代码: const data = [ { "name": "1.0 CNG", "production": [

我试图从API接收的数据中创建一个对象数组。一方面,我在一个数组中接收值,另一方面,我在另一个数组中接收每个值的月份。我为响应中显示的每个月创建了一个对象。但是,我想添加未显示的月份,并将其值添加为0

下面是我保存在变量中的API调用响应示例,以及我为创建对象所做的代码:

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
         }
       })
    })
我想在第二个映射中添加以下内容,但我意识到由于循环的性质,这取决于数组的长度(在本例中为11),因此总有一个月的时间:

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
  }
 }
})
最后我能做什么呢?我有一个这样的数组,但在这个例子中,有月份:1,值: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
}]]

谢谢大家!

您只需检查每个数字1-12是否在月份数组中,如果不是,则添加数量0,否则,在该索引中获取月份和数量:

const数据=[
{
“名称”:“1.0 CNG”,
“生产”:[
139,
174,
112,
121,
67,
105,
0,
121,
92,
98,
91
],
“月份”:[
2.
3.
4.
5.
6.
7.
8.
9,
10,
11,
12
]
}
]
console.log(
数据减少((进位,当前)=>{
for(设i=1;i{
});
返运;
}, [])

);我认为最好的做法是首先按照您希望的外观创建结构,然后填充结构

在这种情况下,您希望确保您拥有所有12个月的时间。那么为什么不先创建这个结构呢

>const monthData=[…数组(13).keys()].slice(1).map(月=>{
返回{
月份:月份
}}
)
现在,您将拥有一个包含所需对象的数组:

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

然后遍历
monthData
,并查找该月对象的每个元素。很抱歉,我会写完整的东西,但是我弄不明白为什么你要同时创建
数据和
最终数据,以及
e
从哪里来。

你想重复这些月,还是只对每个月进行总结?总是只遗漏一个月吗?如果是这样的话,您可以使用for循环而不是data.length+1Y来循环数据。您没有API发送您需要的格式化数据吗?发送飞蛾阵列对我来说就像是浪费交通。发送固定长度(12)数组不是更好吗?