gpt4 book ai didi

javascript - 比较变化的数组javascript

转载 作者:行者123 更新时间:2023-12-03 09:42:23 25 4
gpt4 key购买 nike

我一直在尝试解决这个问题,但没有找到解决方案。我有两个这样的数组

var oldList = [
{"date":"2015-06-30","amount":"-1 008,00","desc":"item1"},
{"date":"2015-06-29","amount":"-61,00","desc":"item2"},
{"date":"2015-06-29","amount":"-483,13","desc":"item2"},
{"date":"2015-06-29","amount":"-57,00","desc":"item2"},
{"date":"2015-06-28","amount":"-50,00","desc":"item1"}
]

var newList = [
{"date":"2015-06-30","amount":"-1 008,00","desc":"item1"},
{"date":"2015-06-30","amount":"-1 008,00","desc":"item1"},
{"date":"2015-06-29","amount":"-61,00","desc":"item2"},
{"date":"2015-06-29","amount":"-483,13","desc":"item2"},
{"date":"2015-06-29","amount":"-57,00","desc":"item2"},
{"date":"2015-06-28","amount":"-50,00","desc":"item1"}
]

newList 数组会一直更新,我需要将 newList 中的项目添加到 oldList 中,正如您所看到的,有时会有多个项目具有完全相同的数据,例如 {"date":"2015 -06-30","amount":"-1 008,00","desc":"item1"}, 在 newList 中,有人对此有任何想法吗?

编辑忘记说了

newList 也会在一段时间后删除最旧的项目,但它们应该仍然在 oldList 中。

最佳答案

如果您想跟踪每个项目的年龄,请为每个项目指定一个 id 值,该值等于自收到它们时的 epoc 以来的秒数。

var newItem = {date="2015-06-30", amount: "-1 088,00", desc: "item1"};
var currentDate = new Date();
newItem.id = currentDate.getTime();

现在:newItem = {"id": 1435689401404, "date":"2015-06-30","amount":"-1 008,00","desc":"item1"}

这允许您迭代新数组并将 id 值最低的项目(这将是最旧的项目)移动到旧数组。

关于javascript - 比较变化的数组javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31145760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com