当属性匹配时,如何比较两个对象和更新值?
我有两个阵列。一种是模式,其中包含12个月且第二的模式是从API获取的。 模式:
[
{
total: 0,
month_name: "Jan",
},
{
total: 0,
month_name: "Feb",
},
{
total: 0,
month_name: "Mar",
},
{
total: 0,
month_name: "Apr",
},
...
]
提取:
[
{
"total": 4,
"month_name": "Mar"
},
{
"total": 1,
"month_name": "Apr"
}
]
我想将获取的数组与模式进行比较,查找匹配的“ month_name”并更新“总计”。 获取的阵列仅在超过0时才包含几个月的对象。
I have two arrays. One is the pattern, which contains 12 months and second is fetched from api.
Pattern:
[
{
total: 0,
month_name: "Jan",
},
{
total: 0,
month_name: "Feb",
},
{
total: 0,
month_name: "Mar",
},
{
total: 0,
month_name: "Apr",
},
...
]
fetched:
[
{
"total": 4,
"month_name": "Mar"
},
{
"total": 1,
"month_name": "Apr"
}
]
I want to compare fetched array to pattern, find matching "month_name" and update "total".
Fetched array contains objects with months only when they are above 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议制作一个查找表(
totalbymonth
),然后您可以通过查找state
来循环,然后通过查找inter
intotalbymonth
。I'd suggest making a lookup table (
totalByMonth
) then you can just loop over thestate
and update each one by looking up thetotal
intotalByMonth
.您可以尝试一下:
You can try this :