如何创建一个唯一的 json 对象数组并在字段上排序?
How to create a unique array of json objects with sort on a field?
如何在公共 id
上填充以下数组并且输出应该具有唯一且最新的历史记录编号
const input = [{
"id": 134116,
"user": "admin",
"historyno": "134116-0"
}, {
"id": 134132,
"user": "admin",
"historyno": "134132-0"
}, {
"id": 134132,
"user": "admin",
"historyno": "134132-1"
}, {
"id": 134133,
"user": "admin",
"historyno": "134133-0"
}, {
"id": 134133,
"user": "admin",
"historyno": "134133-1"
}];
let output = [];
let tempId;
for (let i = 0; i < input.length; i++) {
if (input[i].id === tempId) {
//do nothing
} else {
output.push(input[i]);
tempId = input[i].id;
}
}
console.log(output);
预期输出
[
{
"id": 134116,
"user": "admin",
"historyno": "134116-0"
},
{
"id": 134132,
"user": "admin",
"historyno": "134132-1"
},
{
"id": 134133,
"user": "admin",
"historyno": "134133-1"
}
]
将数组缩减为 Map,使用 id
作为键,然后通过将 Map.values()
迭代器传播到数组来转换回数组。
此解决方案假定数组已按历史编号预排序:
const input = [{"id":134116,"user":"admin","historyno":"134116-0"},{"id":134132,"user":"admin","historyno":"134132-0"},{"id":134132,"user":"admin","historyno":"134132-1"},{"id":134133,"user":"admin","historyno":"134133-0"},{"id":134133,"user":"admin","historyno":"134133-1"}];
const output = [...input.reduce((r, o) => r.set(o.id, o), new Map).values()];
console.log(output);
如果 historyno
大于:
,此解决方案通过仅替换映射中的当前项来处理未排序的数组
const input = [{"id":134116,"user":"admin","historyno":"134116-0"},{"id":134132,"user":"admin","historyno":"134132-0"},{"id":134132,"user":"admin","historyno":"134132-1"},{"id":134133,"user":"admin","historyno":"134133-0"},{"id":134133,"user":"admin","historyno":"134133-1"}];
const getHistoryNo = ({ historyno }) => +historyno.split('-')[1];
const output = [...input.reduce((r, o) => {
const prev = r.get(o.id);
if(!prev || getHistoryNo(o) > getHistoryNo(prev)) r.set(o.id, o);
return r;
}, new Map).values()];
console.log(output);
var resultObj = input.reduce((prev, next) => {
if(prev.hasOwnProperty(next.id)) {
const currentLatest = prev[next.id];
// Your logic to check if 'currentLatest' is the latest one or the 'next'
} else {
prev[next.id] = next;
}
return prev;
}, {});
var result = Object.values(resultObj)
可以将historyno
除以-
取第二个元素,与之前设置的相同id的值进行比较,如果当前值大于上一个则使用当前值,否则使用当前值最后一个
const input = [{"id": 134116,"user": "admin","historyno": "134116-0"}, { "id": 134132, "user": "admin","historyno": "134132-0"}, { "id": 134132,"user": "admin","historyno": "134132-1"}, { "id": 134133, "user": "admin", "historyno": "134133-0"
}, { "id": 134133,"user": "admin","historyno": "134133-1"}];
let final = input.reduce((op,inp)=>{
op[inp.id] = op[inp.id] || inp
let lastHisotry = +op[inp.id].historyno.split('-')[1]
let currentHistory = +inp.historyno.split('-')[1]
op[inp.id].historyno = currentHistory > lastHisotry ? inp.historyno : op[inp.id].historyno
return op
},{})
console.log(final);
注意:-如果你的数组已经排序,你不需要这个拆分逻辑,你可以简单地做
const input = [{"id": 134116,"user":"admin","historyno": "134116-0"}, { "id": 134132, "user":"admin","historyno": "134132-0"}, { "id": 134132,"user": "admin","historyno": "134132-1"}, { "id": 134133, "user": "admin", "historyno": "134133-0"}, { "id": 134133,"user": "admin","historyno": "134133-1"}];
let final = input.reduce((op,inp) => {
op[inp.id] = inp
return op
},{})
console.log(final);
如何在公共 id
上填充以下数组并且输出应该具有唯一且最新的历史记录编号
const input = [{
"id": 134116,
"user": "admin",
"historyno": "134116-0"
}, {
"id": 134132,
"user": "admin",
"historyno": "134132-0"
}, {
"id": 134132,
"user": "admin",
"historyno": "134132-1"
}, {
"id": 134133,
"user": "admin",
"historyno": "134133-0"
}, {
"id": 134133,
"user": "admin",
"historyno": "134133-1"
}];
let output = [];
let tempId;
for (let i = 0; i < input.length; i++) {
if (input[i].id === tempId) {
//do nothing
} else {
output.push(input[i]);
tempId = input[i].id;
}
}
console.log(output);
预期输出
[
{
"id": 134116,
"user": "admin",
"historyno": "134116-0"
},
{
"id": 134132,
"user": "admin",
"historyno": "134132-1"
},
{
"id": 134133,
"user": "admin",
"historyno": "134133-1"
}
]
将数组缩减为 Map,使用 id
作为键,然后通过将 Map.values()
迭代器传播到数组来转换回数组。
此解决方案假定数组已按历史编号预排序:
const input = [{"id":134116,"user":"admin","historyno":"134116-0"},{"id":134132,"user":"admin","historyno":"134132-0"},{"id":134132,"user":"admin","historyno":"134132-1"},{"id":134133,"user":"admin","historyno":"134133-0"},{"id":134133,"user":"admin","historyno":"134133-1"}];
const output = [...input.reduce((r, o) => r.set(o.id, o), new Map).values()];
console.log(output);
如果 historyno
大于:
const input = [{"id":134116,"user":"admin","historyno":"134116-0"},{"id":134132,"user":"admin","historyno":"134132-0"},{"id":134132,"user":"admin","historyno":"134132-1"},{"id":134133,"user":"admin","historyno":"134133-0"},{"id":134133,"user":"admin","historyno":"134133-1"}];
const getHistoryNo = ({ historyno }) => +historyno.split('-')[1];
const output = [...input.reduce((r, o) => {
const prev = r.get(o.id);
if(!prev || getHistoryNo(o) > getHistoryNo(prev)) r.set(o.id, o);
return r;
}, new Map).values()];
console.log(output);
var resultObj = input.reduce((prev, next) => {
if(prev.hasOwnProperty(next.id)) {
const currentLatest = prev[next.id];
// Your logic to check if 'currentLatest' is the latest one or the 'next'
} else {
prev[next.id] = next;
}
return prev;
}, {});
var result = Object.values(resultObj)
可以将historyno
除以-
取第二个元素,与之前设置的相同id的值进行比较,如果当前值大于上一个则使用当前值,否则使用当前值最后一个
const input = [{"id": 134116,"user": "admin","historyno": "134116-0"}, { "id": 134132, "user": "admin","historyno": "134132-0"}, { "id": 134132,"user": "admin","historyno": "134132-1"}, { "id": 134133, "user": "admin", "historyno": "134133-0"
}, { "id": 134133,"user": "admin","historyno": "134133-1"}];
let final = input.reduce((op,inp)=>{
op[inp.id] = op[inp.id] || inp
let lastHisotry = +op[inp.id].historyno.split('-')[1]
let currentHistory = +inp.historyno.split('-')[1]
op[inp.id].historyno = currentHistory > lastHisotry ? inp.historyno : op[inp.id].historyno
return op
},{})
console.log(final);
注意:-如果你的数组已经排序,你不需要这个拆分逻辑,你可以简单地做
const input = [{"id": 134116,"user":"admin","historyno": "134116-0"}, { "id": 134132, "user":"admin","historyno": "134132-0"}, { "id": 134132,"user": "admin","historyno": "134132-1"}, { "id": 134133, "user": "admin", "historyno": "134133-0"}, { "id": 134133,"user": "admin","historyno": "134133-1"}];
let final = input.reduce((op,inp) => {
op[inp.id] = inp
return op
},{})
console.log(final);