splice objects from array and move those elements to a different element within array without mutating original

huangapple go评论53阅读模式
英文:

splice objects from array and move those elements to a different element within array without mutating original

问题

这段代码大致实现了我需要的功能,但它改变了原始数组,这不是我想要的。我知道这是因为我在使用 splicepush

这个对象以每周的每一天作为键,每个键都有店铺的开放和关闭时间。

一切都正常,直到 friday 键,它只有 open 时间对象,而 close 时间在 saturday 键上(因为店铺在第二天关闭),saturdaysunday 也是同样的情况。

我想将每个对象移动到其相应的日期键,但我希望在不改变原始对象的情况下实现这一点。

我需要做什么才能实现这个目标?

英文:

This code does kinda what I need but it mutates the original array and that is not what I want. I know this happens because I'm using splice and push.
This object has every day of the week as key and each key has the opening and closing time of the store.

Everything is normal until the friday key which has only the open time object and the close one is on the saturday key(because the store closes the next day), same thing happens for saturday and sunday.
What I do is move each object to its corresponding day key but I want to achieve this without mutating the original object.

What am I missing to achieve that?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const days = {
  &quot;monday&quot;: [{
      &quot;day&quot;: &quot;monday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 43200
    },
    {
      &quot;day&quot;: &quot;monday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 75600
    }
  ],
  &quot;tuesday&quot;: [{
      &quot;day&quot;: &quot;tuesday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 43200
    },
    {
      &quot;day&quot;: &quot;tuesday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 75600
    }
  ],
  &quot;wednesday&quot;: [{
      &quot;day&quot;: &quot;wednesday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 43200
    },
    {
      &quot;day&quot;: &quot;wednesday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 75600
    }
  ],
  &quot;thursday&quot;: [{
      &quot;day&quot;: &quot;thursday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 43200
    },
    {
      &quot;day&quot;: &quot;thursday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 75600
    }
  ],
  &quot;friday&quot;: [{
    &quot;day&quot;: &quot;friday&quot;,
    &quot;type&quot;: &quot;open&quot;,
    &quot;value&quot;: 36000
  }],
  &quot;saturday&quot;: [{
      &quot;day&quot;: &quot;friday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 3600
    },
    {
      &quot;day&quot;: &quot;saturday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 36000
    }
  ],
  &quot;sunday&quot;: [{
      &quot;day&quot;: &quot;saturday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 3600
    },
    {
      &quot;day&quot;: &quot;sunday&quot;,
      &quot;type&quot;: &quot;open&quot;,
      &quot;value&quot;: 43200
    },
    {
      &quot;day&quot;: &quot;sunday&quot;,
      &quot;type&quot;: &quot;close&quot;,
      &quot;value&quot;: 75600
    }
  ]
}

const result = Object.entries(days).map(([k, v], i) =&gt; {
  if (k == &quot;friday&quot;) {
    days[k].push(days.saturday.splice(0, 1));
    //console.log(days[k])
  } else if (k == &quot;saturday&quot;) {
    days[k].push(days.sunday.splice(0, 1));
    //console.log(days[k])
  }
  return {
    [k]: v
  }
})

console.log(result)

<!-- end snippet -->

答案1

得分: 1

你可以取出所有内部对象并使用它们的 day 属性从头开始重建整个对象:

const days = {"monday": [{"day": "monday","type": "open","value": 43200},{"day": "monday","type": "close","value": 75600}],"tuesday": [{"day": "tuesday","type": "open","value": 43200},{"day": "tuesday","type": "close","value": 75600}],"wednesday": [{"day": "wednesday","type": "open","value": 43200},{"day": "wednesday","type": "close","value": 75600}],"thursday": [{"day": "thursday","type": "open","value": 43200},{"day": "thursday","type": "close","value": 75600}],"friday": [{"day": "friday","type": "open","value": 36000}],"saturday": [{"day": "friday","type": "close","value": 3600},{"day": "saturday","type": "open","value": 36000}],"sunday": [{"day": "saturday","type": "close","value": 3600},{"day": "sunday","type": "open","value": 43200},{"day": "sunday","type": "close","value": 75600}]};

const result = Object.values(days).flat().reduce((acc, o) => {
    (acc[o.day] ??= []).push(o);
    return acc;
}, {});

console.log(result);

希望对你有所帮助!

英文:

You could take all of the inner objects and use their day properties to rebuild the overall object from scratch:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const days = {&quot;monday&quot;: [{&quot;day&quot;: &quot;monday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 43200},{&quot;day&quot;: &quot;monday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 75600}],&quot;tuesday&quot;: [{&quot;day&quot;: &quot;tuesday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 43200},{&quot;day&quot;: &quot;tuesday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 75600}],&quot;wednesday&quot;: [{&quot;day&quot;: &quot;wednesday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 43200},{&quot;day&quot;: &quot;wednesday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 75600}],&quot;thursday&quot;: [{&quot;day&quot;: &quot;thursday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 43200},{&quot;day&quot;: &quot;thursday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 75600}],&quot;friday&quot;: [{&quot;day&quot;: &quot;friday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 36000}],&quot;saturday&quot;: [{&quot;day&quot;: &quot;friday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 3600},{&quot;day&quot;: &quot;saturday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 36000}],&quot;sunday&quot;: [{&quot;day&quot;: &quot;saturday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 3600},{&quot;day&quot;: &quot;sunday&quot;,&quot;type&quot;: &quot;open&quot;,&quot;value&quot;: 43200},{&quot;day&quot;: &quot;sunday&quot;,&quot;type&quot;: &quot;close&quot;,&quot;value&quot;: 75600}]};

const result = Object.values(days).flat().reduce((acc, o) =&gt; {
    (acc[o.day] ??= []).push(o);
    return acc;
}, {});

console.log(result);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月16日 04:47:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定