如何使用 javascript/angularjs 比较两个对象数组?
How to compare two object array using javascript/angularjs?
我有 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 则它将更改为 "late" 的字符串。同样对于 finish_time,如果 B 上的值低于 A,则字符串将为 "too early".
对我来说,这真的只是一个 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]);
}
根据我的理解,基本上您想根据 A
中存在的值搜索数组 B
中的值,并计算开始时间是早、晚还是 on-time .
https://whosebug.com/users/1148564/joseph-serido 的解决方案将完美运行,但需要 O(n^2) 时间。在 Javascript 中,您可以通过键名访问对象。如果您将 A 和 B 作为对象,您可以利用这一行为并使您的代码 运行 只需一个循环。
var A_Times = {}; //Make an Object
//Use the date as Key
A_Obj["2013-07-31"] = {start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"};
A_Obj["2013-08-03"] ={ start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"};
var B_Times = {};
B_Times["2013-07-31"] = {start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"};
B_Times["2013-08-03"] ={start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"};
var A_Days = Object.keys(A_Times); //Get all the days in A
for(var i=0; i<A_Times_Days.length; i++){
var day = A_Times_Days[i];
console.log(day); //Log the Key here
var A_Data = A_Times[day];
var B_Data = B_Times[day];
console.log(A_Data);
console.log(B_Data);
//Compute punctuality here based on A_Data and B_Data
}
但是,如果您的用例仅涉及几天的数据,那么这种增加的复杂性可能不值得。 trade-off 由您决定。
希望对您有所帮助。
嗨,这是我想出来的,我不知道该说些什么。
我认为代码会说明一切:p
var 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"}
];
var 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"}
];
function getResult()
{
var results = [];
for(var i = 0; i < A.length; i++)
{
var objA = A[i];
for(var j = 0; j < B.length; j++)
{
var objB = B[j];
if(objB.date === objA.date)
{
var o = {};
o.date = objA.date;
//if start_time of A is less than start_time of B
if(Date.parse(objA.start_time) < Date.parse(objB.start_time))
o.start_time = "late";
else
o.start_time = "on time";
//if end_time of A is less than end_time of B
if(Date.parse(objA.finish_time) < Date.parse(objB.finish_time))
o.finish_time = "too early";
else
o.finish_time = "on time";
results.push(o);
}
}
}
if(results.length !== 0)
return results;
return null;
}
P.S。
它将仅输出 A 的日期等于 B
的日期的对象
我有 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 则它将更改为 "late" 的字符串。同样对于 finish_time,如果 B 上的值低于 A,则字符串将为 "too early".
对我来说,这真的只是一个 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]);
}
根据我的理解,基本上您想根据 A
中存在的值搜索数组 B
中的值,并计算开始时间是早、晚还是 on-time .
https://whosebug.com/users/1148564/joseph-serido 的解决方案将完美运行,但需要 O(n^2) 时间。在 Javascript 中,您可以通过键名访问对象。如果您将 A 和 B 作为对象,您可以利用这一行为并使您的代码 运行 只需一个循环。
var A_Times = {}; //Make an Object
//Use the date as Key
A_Obj["2013-07-31"] = {start_time:"2013-07-31 17:30:00+10", finish_time:"2013-07-31 20:30:00+10"};
A_Obj["2013-08-03"] ={ start_time:"2013-08-03 17:00:00+10", finish_time:"2013-08-03 20:00:00+10"};
var B_Times = {};
B_Times["2013-07-31"] = {start_time:"2013-07-31 17:37:49+10", finish_time:"2013-07-31 20:32:04+10"};
B_Times["2013-08-03"] ={start_time:"2013-08-03 16:57:34+10", finish_time:"2013-08-03 20:00:57+10"};
var A_Days = Object.keys(A_Times); //Get all the days in A
for(var i=0; i<A_Times_Days.length; i++){
var day = A_Times_Days[i];
console.log(day); //Log the Key here
var A_Data = A_Times[day];
var B_Data = B_Times[day];
console.log(A_Data);
console.log(B_Data);
//Compute punctuality here based on A_Data and B_Data
}
但是,如果您的用例仅涉及几天的数据,那么这种增加的复杂性可能不值得。 trade-off 由您决定。 希望对您有所帮助。
嗨,这是我想出来的,我不知道该说些什么。 我认为代码会说明一切:p
var 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"}
];
var 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"}
];
function getResult()
{
var results = [];
for(var i = 0; i < A.length; i++)
{
var objA = A[i];
for(var j = 0; j < B.length; j++)
{
var objB = B[j];
if(objB.date === objA.date)
{
var o = {};
o.date = objA.date;
//if start_time of A is less than start_time of B
if(Date.parse(objA.start_time) < Date.parse(objB.start_time))
o.start_time = "late";
else
o.start_time = "on time";
//if end_time of A is less than end_time of B
if(Date.parse(objA.finish_time) < Date.parse(objB.finish_time))
o.finish_time = "too early";
else
o.finish_time = "on time";
results.push(o);
}
}
}
if(results.length !== 0)
return results;
return null;
}
P.S。 它将仅输出 A 的日期等于 B
的日期的对象