ps:最后补充了高级的
题目:
需求:
现在需要实现以下两个函数:
1、getOrdersByStatus(status: string)
该函数接受一个参数status,返回一个包含所有状态为status的订单的数组。其中,订单的每个对象应该包含以下属性:
id
name
address
status
log: 只包含状态为status的日志对象
按照订单的id从小到大排序
2、getOrderStatusHistory(id: number)
该函数接受一个参数id,返回一个字符串,表示编号为id的订单的状态历史记录。该字符串应该按照状态更新时间从旧到新排序,并以如下格式返回:(输入订单号显示时间和 所有状态
[time] status
其中,time为格式为YYYY-MM-DD HH:mm:ss的时间字符串,status为订单状态。
例:[2023-02-16 12:30:00] created
有一个包含多个物流订单的数组**orders**,每个订单包含以下属性:
- **id**:订单编号(数字类型)
- **name**:收货人姓名(字符串类型)
- **address**:收货地址(字符串类型)
- **status**:订单状态,有以下几种:**created**(已创建)、**packed**(已打包)、**shipped**(已发货)、**delivered**(已送达)(字符串类型)
- **log**:订单日志,是一个包含多个对象的数组,每个对象包含以下属性:
- - **status**:订单状态(同上)
- **time**:订单状态更新时间(字符串类型)
模板数据:
const orders = [
{
id: 1,
name: 'John Smith',
address: '123 Main St',
status: 'created',
log: [
{
status: 'created',
time: '2023-02-16 12:30:00'
}
]
},
{
id: 2,
name: 'Jane Doe',
address: '456 Second St',
status: 'packed',
log: [
{
status: 'created',
time: '2023-02-16 10:00:00'
},
{
status: 'packed',
time: '2023-02-16 12:30:00'
}
]
},
{
id: 3,
name: 'Bob Johnson',
address: '789 Third St',
status: 'shipped',
log: [
{
status: 'created',
time: '2023-02-15 10:00:00'
},
{
status: 'packed',
time: '2023-02-15 12:00:00'
},
{
status: 'shipped',
time: '2023-02-16 9:00:00'
}
]
}
];
解决方式:
没用map join啥的,用了很多浅拷贝,防止改变原数组影响别的功能。(不会深拷贝呜)
// 找到当前状态
function getOrdersByStatus(status) {
// 存放结果数组对象
let arr = [];
orders.forEach((item) => {
let { status: statusNow, log } = item;
// 为了不改变原数组
const obj = { ...item };
let objLog;
// 找到目前状态为status的订单
if (statusNow === status) {
log.forEach((e) => {
const { status: statusAll } = e;
// 将历史中状态一致的取出来
if (statusAll === status) {
// 为了不改变原数组
objLog = { ...e };
obj.log = { ...objLog };
arr.push(obj);
}
});
}
});
//排序arr
arr.sort(function (a, b) {
if (a.id > b.id) return 1;
else if (a.id < b.id) return -1;
else return 0;
});
return arr;
}
const arrResult = getOrdersByStatus("shipped");
console.log(arrResult);
// console.log(orders);
// 获得时间戳
function change(time) {
return +new Date(time);
}
// 获得时间
function getTime(time) {
return new Date(time);
}
// 补0
function addZero(num) {
if (num >= 0 && num < 10) {
return "0" + num;
}
return num;
}
// 查看id历史状态
function getOrderStatusHistory(id) {
// 排序后的时间数组
let arrResult = [];
// 未排序的时间和状态数组
let arrTime;
// 查找当前订单
orders.forEach((item) => {
const { id: searchId, log } = item;
if (searchId == id) {
arrTime = [...log];
}
});
arrTime.sort(function (a, b) {
if (change(a.time) > change(b.time)) return 1;
else if (change(a.time) < change(b.time)) return -1;
else return 0;
});
console.log(arrTime);
arrTime.forEach((item) => {
const { status, time } = item;
const y = getTime(time).getFullYear();
const mo = addZero(getTime(time).getMonth() + 1);
const d = addZero(getTime(time).getDate());
const h = addZero(getTime(time).getHours());
const mi = addZero(getTime(time).getMinutes());
const s = addZero(getTime(time).getSeconds());
const str = `[${y}-${mo}-${d} ${h}:${mi}:${s}] ${status}`;
arrResult.push(str);
});
return arrResult;
}
// [2023-02-16 12:30:00] created
const arrTime = getOrderStatusHistory(3);
console.log(arrTime);
补充: