当属性匹配时,如何比较两个对象和更新值?

发布于 2025-02-10 00:44:28 字数 531 浏览 2 评论 0原文

我有两个阵列。一种是模式,其中包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2025-02-17 00:44:28

我建议制作一个查找表(totalbymonth),然后您可以通过查找state来循环,然后通过查找inter in totalbymonth

const state = [
  {total: 0, month_name: "Jan"},
  {total: 0, month_name: "Feb"},
  {total: 0, month_name: "Mar"},
  {total: 0, month_name: "Apr"}
];

const fetched = [
  {total: 4, month_name: "Mar"},
  {total: 1, month_name: "Apr"}
];

//build totalByMonth object
const totalByMonth = {};
for (let f of fetched) {
  totalByMonth[f.month_name] = f.total;
}

//update state
for (let s of state) {
  const total = totalByMonth[s.month_name];
  if (total) s.total = total;
}

console.log(state);

I'd suggest making a lookup table (totalByMonth) then you can just loop over the state and update each one by looking up the total in totalByMonth.

const state = [
  {total: 0, month_name: "Jan"},
  {total: 0, month_name: "Feb"},
  {total: 0, month_name: "Mar"},
  {total: 0, month_name: "Apr"}
];

const fetched = [
  {total: 4, month_name: "Mar"},
  {total: 1, month_name: "Apr"}
];

//build totalByMonth object
const totalByMonth = {};
for (let f of fetched) {
  totalByMonth[f.month_name] = f.total;
}

//update state
for (let s of state) {
  const total = totalByMonth[s.month_name];
  if (total) s.total = total;
}

console.log(state);

寒尘 2025-02-17 00:44:28

您可以尝试一下:

let result = months.map(month => {
    let matching_result = fetched.filter(f => f.month_name == month.month_name);
    return matching_result[0] ? {...month, total: matching_result[0].total}: month;
});

console.log(result);

//Output
// [
// {total: 0, month_name: 'Jan'},
// {total: 0, month_name: 'Feb'},
// {total: 4, month_name: 'Mar'},
// {total: 1, month_name: 'Apr'},
// ]

You can try this :

let result = months.map(month => {
    let matching_result = fetched.filter(f => f.month_name == month.month_name);
    return matching_result[0] ? {...month, total: matching_result[0].total}: month;
});

console.log(result);

//Output
// [
// {total: 0, month_name: 'Jan'},
// {total: 0, month_name: 'Feb'},
// {total: 4, month_name: 'Mar'},
// {total: 1, month_name: 'Apr'},
// ]
少女净妖师 2025-02-17 00:44:28
let pattern=[{total:0,month_name:"Jan"},{total:0,month_name:"Feb"},{total:0,month_name:"Mar"},{total:0,month_name:"Apr"}]

let fetched=[{total:4,month_name:"Mar"},{total:1,month_name:"Apr"}];

function updateTotal(pattern,fetched){
    fetched.forEach((e) => {
      let index = pattern.findIndex(p => p.month_name === e.month_name)
      if(index > -1){
        pattern[index].total = e.total
      }
    } )
 }

updateTotal(pattern,fetched)
console.log(pattern)

let pattern=[{total:0,month_name:"Jan"},{total:0,month_name:"Feb"},{total:0,month_name:"Mar"},{total:0,month_name:"Apr"}]

let fetched=[{total:4,month_name:"Mar"},{total:1,month_name:"Apr"}];

function updateTotal(pattern,fetched){
    fetched.forEach((e) => {
      let index = pattern.findIndex(p => p.month_name === e.month_name)
      if(index > -1){
        pattern[index].total = e.total
      }
    } )
 }

updateTotal(pattern,fetched)
console.log(pattern)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文