Javascript 在键上连接数组
我有两个阵列:Javascript 在键上连接数组,javascript,arrays,Javascript,Arrays,我有两个阵列: applesToday= [ {product: "apple", period: 1433113200000, qty: -5}, {product: "apple", period: 1435705200000, qty: -6 }, {product: "apple", period: 1438383600000, qty: -8} ] applesYesterday = [ {product: "apple", period: 1433113200000,
applesToday=
[ {product: "apple", period: 1433113200000, qty: -5},
{product: "apple", period: 1435705200000, qty: -6 },
{product: "apple", period: 1438383600000, qty: -8}
]
applesYesterday =
[ {product: "apple", period: 1433113200000, qty: 10},
{product: "apple", period: 1435705200000, qty: 12 },
{product: "apple", period: 1438383600000, qty: 16}
]
如何在“期间”字段中将它们组合为:
self.applesTodayYesterday=
[ {product: "apple", period: 1433113200000, qty: -5, qty1: 10},
{product: "apple", period: 1435705200000, qty: -6, qty1: 12 },
{product: "apple", period: 1438383600000, qty: -8, qty1: 16}
]
我试着用谷歌搜索左键,右键,但找不到任何东西。。。有好的图书馆吗
期间始终是某个月的第一天,是每个数组的唯一键
编辑:
目的
我正在使用knockout JS生成一个当前库存表,并希望对数据进行预处理,以便每一行都是数组中的一个对象。预格式化的数据将仅由knockout用于打印到DOM中
时期是指从树上摘苹果的时间
[product| period | quantity | daily change | weekly change ] [field 1 | field 2 ]
[apple | apr-15 | 10 | -1 | -12 ] [8% | 20% ]
[apple | may-15 | 12 | -3 | +14 ] [1% | 11% ]
[TOTAL APPLES | 22 | -4 | +2 ] [4% | 43% ]
[carrot | apr-15 | 14 | -7 | -12 ] [8% | 20% ]
[carrot | may-15 | 12 | +1 | +14 ] [1% | 11% ]
[TOTAL CARROTS | 26 | -6 | +2 ] [4% | 43% ]
制作一个关键点设置为昨天苹果时段的对象,然后使用该对象为今天的苹果设置一个新的qty1值
var qtyMap = {};
applesYesterday.forEach(function(a){
qtyMap[a.period] = a.qty;
});
applesToday.forEach(function(a){
a.qty1 = qtyMap[a.period];
});
不,拉链坏了。在整个阵列中会有很多不同的时期为什么不合并数量?例如,期间“143131200000”的数量为15。为什么要把数量分开?如果你想把它们分开,制作一个数量数组并把它们放在那里。你想通过这样做来实现什么?您是否在跟踪某个时期内生产的苹果数量?要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题因堆栈溢出而脱离主题,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作。查看您的编辑,句点在哪里起作用?如果句点在其中一个数组中重复会怎样?(在我推荐类似的东西之前,仍在等待这些信息)@dandavis“period”是日期的毫秒值。它代表天。像这样的一组昨天/今天的数据不太可能重复几天。这在OP的帖子中也没有显示。