gpt4 book ai didi

javascript - 如何使用 javascript/angularjs 比较两个对象数组?

转载 作者:行者123 更新时间:2023-11-28 17:36:51 27 4
gpt4 key购买 nike

我有 2 个对象,我想将其合并为一个对象数组,但我需要首先使用 JavaScript 或 AngularJS 进行比较。

A = [
{date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"},
{date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"},
{date: "2013-09-03", start_time:"2013-09-03 17:00:00+10", finish_time:"2013-09-03 20:00:00+10"}
]

B = [
{date: "2013-07-31", start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"},
{date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"}
]

Expected output
C = [
{date: "2013-07-31", start_time:"late", finish_time:"on time"},
{date: "2013-08-03", start_time:"on time", finish_time:"on time"},
]

我将首先比较两个对象数组是否具有相同的日期,然后我将比较同一日期的开始时间,然后如果 B 上的 start_time 值超过 A 上的 start_time 那么它将更改为一个字符串,表示它是“迟到” ”。同样,对于 finish_time,如果 B 上的值低于 A,则字符串将“太早”。

最佳答案

对我来说,这实际上只是一个 JavaScript 问题,而不是 AngularJS 问题。

我认为您正在寻找这样的东西:

const A = [
{date: "2013-07-31", start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"},
{date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"},
{date: "2013-08-03", start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"}
];

const B = [
{date: "2013-07-31", start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"},
{date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"},
{date: "2013-08-03", start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 19:00:57+10"}
];


let C = [];
const maxLength = Math.min(A.length, B.length);

for (let i = 0; i < maxLength; i += 1) {
const startTimeResult = B[i].start_time > A[i].start_time ? 'late' : 'on time';
const finishTimeResult = B[i].finish_time > A[i].finish_time ? 'on time' : 'too early';
C[i] = { date: A[i].date, start_time: startTimeResult, finish_time: finishTimeResult };
console.log(C[i]);
}

https://codepen.io/joseph4tw/pen/mXGeoO?editors=1012

关于javascript - 如何使用 javascript/angularjs 比较两个对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48959185/

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