如何将对象值从单独的行合并为一行?
如果在“ OUT” 之后没有匹配的“ IN” 行,则“ OUT” 日期应在1小时后自动关闭。合并线条之前,需要检查汽车和用户是否匹配。
$firstNameArray = User::select('user_first_name')->pluck('user_first_name');
return response()->json($firstNameArray);
结果应如下所示:
var carData = [
{date:"2018-06-08 13:20:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-08 14:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car2", user:"User2"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car3", user:"User4"},
{date:"2018-06-08 14:35:00", type:"IN", car:"Car2", user:"User2"},
{date:"2018-06-09 11:12:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-09 12:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-10 17:12:00", type:"OUT", car:"Car1", user:"User3"},
{date:"2018-06-10 18:13:00", type:"IN", car:"Car1", user:"User3"},
{date:"2018-06-10 19:12:00", type:"OUT", car:"Car2", user:"User1"},
{date:"2018-06-10 20:13:00", type:"IN", car:"Car2", user:"User1"}
];
答案 0 :(得分:0)
我认为,这可能是您的解决方案:
var carData = [
{date:"2018-06-08 13:20:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-08 14:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car2", user:"User2"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car3", user:"User4"},
{date:"2018-06-08 14:35:00", type:"IN", car:"Car2", user:"User2"},
{date:"2018-06-09 11:12:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-09 12:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-10 17:12:00", type:"OUT", car:"Car1", user:"User3"},
{date:"2018-06-10 18:13:00", type:"IN", car:"Car1", user:"User3"},
{date:"2018-06-10 19:12:00", type:"OUT", car:"Car2", user:"User1"},
{date:"2018-06-10 20:13:00", type:"IN", car:"Car2", user:"User1"}
];
var carDataCombined = [];
function addOneHour(date){
date = new Date(date);
date.setHours(date.getHours() + 1);
var hours = (date.getHours() < 10) ? '0' + date.getHours() : date.getHours();
var minutes = (date.getMinutes() < 10) ? '0' + date.getMinutes() : date.getMinutes();
var seconds = (date.getSeconds() < 10) ? '0' + date.getSeconds() : date.getSeconds();
return date.toISOString().substring(0, 10) + ' ' + hours + ':' + minutes + ':' + seconds;
}
for(var i = 0; i < carData.length; i++){
if(carData[i].type === 'OUT'){
var dateIn;
if(carData[i+1] && carData[i+1].type === 'IN' && (carData[i].car === carData[i+1].car && carData[i].user === carData[i+1].user)){
dateIn = carData[i+1].date;
} else {
dateIn = addOneHour(carData[i].date)
}
carDataCombined.push({dateOut: carData[i].date, dateIn: dateIn, car: carData[i].car, user: carData[i].user});
}
}
console.log(carDataCombined)
答案 1 :(得分:0)
下面的逻辑应该起作用
$.each
类型的OUT
循环carData OUT
开始以其他日期形成新日期,并向其中添加1小时。
var carData = [{
date: "2018-06-08 13:20:00",
type: "OUT",
car: "Car1",
user: "User1"
},
{
date: "2018-06-08 14:13:00",
type: "IN",
car: "Car1",
user: "User1"
},
{
date: "2018-06-08 14:20:00",
type: "OUT",
car: "Car2",
user: "User2"
},
{
date: "2018-06-08 14:20:00",
type: "OUT",
car: "Car3",
user: "User4"
},
{
date: "2018-06-08 14:35:00",
type: "IN",
car: "Car2",
user: "User2"
},
{
date: "2018-06-09 11:12:00",
type: "OUT",
car: "Car1",
user: "User1"
},
{
date: "2018-06-09 12:13:00",
type: "IN",
car: "Car1",
user: "User1"
},
{
date: "2018-06-10 17:12:00",
type: "OUT",
car: "Car1",
user: "User3"
},
{
date: "2018-06-10 18:13:00",
type: "IN",
car: "Car1",
user: "User3"
},
{
date: "2018-06-10 19:12:00",
type: "OUT",
car: "Car2",
user: "User1"
},
{
date: "2018-06-10 20:13:00",
type: "IN",
car: "Car2",
user: "User1"
}
];
var carDataCombined;
var carDataCombinedArray = [];
var i = 0;
$(function() {
$(carData).each(function(key, item) {
if (item.type == "IN") return;
var currentItem = item;
var nextItem = key !== carData.length - 1 ? carData[key + 1] : null
var dateOut = item.date;
var dateIn = nextItem && nextItem.type == "IN" ? nextItem.date : null;
var car = item.car;
var user = item.user;
if (!dateIn) {
var d = new Date(dateOut);
dateIn = (
d.getFullYear() + "-" + ("00" + d.getMonth()).slice(-2) + "-" + ("00" + d.getDate()).slice(-2) + " " +
("00" + (d.getHours() + 1)).slice(-2) + ":" + ("00" + d.getMinutes()).slice(-2) + ":" + ("00" + d.getSeconds()).slice(-2));
}
carDataCombinedArray.push({
"dateOut": dateOut,
"dateIn": dateIn,
"car": car,
"user": user,
});
carDataCombined = JSON.stringify(carDataCombinedArray);
});
console.log(carDataCombined);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 2 :(得分:0)
我来晚了,其他人都快了!
算法:
您可能更喜欢J. Sadi类似的答案:我消耗了输入数组。
let carData = [
{date:"2018-06-08 13:20:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-08 14:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car2", user:"User2"},
{date:"2018-06-08 14:20:00", type:"OUT", car:"Car3", user:"User4"},
{date:"2018-06-08 14:35:00", type:"IN", car:"Car2", user:"User2"},
{date:"2018-06-09 11:12:00", type:"OUT", car:"Car1", user:"User1"},
{date:"2018-06-09 12:13:00", type:"IN", car:"Car1", user:"User1"},
{date:"2018-06-10 17:12:00", type:"OUT", car:"Car1", user:"User3"},
{date:"2018-06-10 18:13:00", type:"IN", car:"Car1", user:"User3"},
{date:"2018-06-10 19:12:00", type:"OUT", car:"Car2", user:"User1"},
{date:"2018-06-10 20:13:00", type:"IN", car:"Car2", user:"User1"}
];
// can be simplified with momentjs functions...
function add1Hour(strDate) {
let timestamp = Date.parse(strDate.replace(' ', 'T'));
let isoData = new Date(timestamp + 3600 * 1000).toISOString();
return isoData.replace(/(.*)T(.*)\..*/, '$1 $2');
}
let carDataCombined = [];
while (carData.length > 0) {
let data = carData.shift();
if (data.type === 'OUT') {
let nextData = carData.find((otherData) => (otherData.car === data.car && otherData.user === data.user))
let dateIn;
if (nextData === undefined || nextData.type === 'OUT')
dateIn = add1Hour(data.date);
else
dateIn = nextData.date
carDataCombined.push({ dateOut: data.date, dateIn: dateIn, car: data.car, user: data.user });
}
}
console.log(carDataCombined);