Javascript 比较JSON值并获取新的JSON
我有以下JSONJavascript 比较JSON值并获取新的JSON,javascript,json,angular,typescript,Javascript,Json,Angular,Typescript,我有以下JSON [ { "Key": "file/ERROR-FILE1-123.xlsx", }, { "Key": "file/PROCESS-FILE1-123.xlsx", }, { "Key": "file/PROCESS-FILE2-111.xlsx", }, { "Key": "file/SUCCESS-FILE2-111.xlsx", }, { "Key": "file/PROCESS-FILE3-
[
{
"Key": "file/ERROR-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE2-111.xlsx",
},
{
"Key": "file/SUCCESS-FILE2-111.xlsx",
},
{
"Key": "file/PROCESS-FILE3-121.xlsx",
},
]
我想在这里实现的是首先检查JSON键字符串的最后一部分,如果它是相同的,那么我将检查进程和错误,并显示错误文件
例如,在我的JSON中,键[0]
是“file/ERROR-FILE1-123.xlsx”,而键[1]
是“file/PROCESS-FILE1-123.xlsx”,因此对于这两个键
,FILE1-123.xlsx是相同的,因此将过滤错误文件并将其添加到新的JSON中。与其他JSON相同如果最后一部分相同,则将优先考虑成功和错误,并仅将这些文件添加到新JSON中,但如果流程键为single,则表示没有错误或成功可用,则只显示流程文件
所以我期望的新JSON应该是这样的。请帮助我如何实现这一点,因为我对UI技术还处于学习阶段,是一名全新的用户。如果有更好的方法来实现这一点,请分享
[
{
"Key": "file/ERROR-FILE1-123.xlsx",
},
{
"Key": "file/SUCCESS-FILE2-111.xlsx",
},
{
"Key": "file/PROCESS-FILE3-121.xlsx",
},
]
我们将在数组上循环并构建一个新的数组,保留代码和每个键的类型 在每个值上,我们都要检查我们正在构建的数组上的值,并替换该值或推送一个新值 在
数组的最后。reduce
我们将有一个包含代码、键和类型的对象。我们使用Array.map
对其进行变异,以获得所需的输出
//为可以获取的每种类型的文件指定一个数值
//目的是能够比较所有类型,并
//选择要保留哪一个以防冲突
函数getTypeValue(类型){
返回({
过程:1,
成功:2,
错误:3,
})[类型];
}
常数arr=[{
键:“file/ERROR-FILE1-123.xlsx”,
},
{
键:“file/PROCESS-FILE1-123.xlsx”,
},
{
键:“file/PROCESS-FILE2-111.xlsx”,
},
{
键:“file/SUCCESS-FILE2-111.xlsx”,
},
{
键:“file/PROCESS-FILE3-121.xlsx”,
},
];
const ret=对象值(arr.reduce((tmp{
钥匙
}) => {
//使用正则表达式提取感兴趣的值
常数[,,
类型
密码
]=/(错误|进程|成功)-文件[0-9]{1}-([0-9]*).xlsx/.exec(键);
const typeValue=getTypeValue(类型);
if(tmp[代码]){
//检查新条目是否应替换已存储的条目
if(tmp[code].typeValue ({
钥匙
}));
控制台日志(ret)代码>我们将在数组上循环并构建一个新的数组,保留代码和每个键的类型
在每个值上,我们都要检查我们正在构建的数组上的值,并替换该值或推送一个新值
在数组的最后。reduce
我们将有一个包含代码、键和类型的对象。我们使用Array.map
对其进行变异,以获得所需的输出
//为可以获取的每种类型的文件指定一个数值
//目的是能够比较所有类型,并
//选择要保留哪一个以防冲突
函数getTypeValue(类型){
返回({
过程:1,
成功:2,
错误:3,
})[类型];
}
常数arr=[{
键:“file/ERROR-FILE1-123.xlsx”,
},
{
键:“file/PROCESS-FILE1-123.xlsx”,
},
{
键:“file/PROCESS-FILE2-111.xlsx”,
},
{
键:“file/SUCCESS-FILE2-111.xlsx”,
},
{
键:“file/PROCESS-FILE3-121.xlsx”,
},
];
const ret=对象值(arr.reduce((tmp{
钥匙
}) => {
//使用正则表达式提取感兴趣的值
常数[,,
类型
密码
]=/(错误|进程|成功)-文件[0-9]{1}-([0-9]*).xlsx/.exec(键);
const typeValue=getTypeValue(类型);
if(tmp[代码]){
//检查新条目是否应替换已存储的条目
if(tmp[code].typeValue ({
钥匙
}));
控制台日志(ret)代码>您可以将状态从字符串中分离出来,按文件名分组并获得优先的非进程对象
函数getParts{
返回(s.match(/^(.*)(ERROR | PROCESS | SUCCESS)-(.*)$/)| |[])。切片(1);
}
var data=[{Key:“errorbyte.xlsx”},{Key:“file/ERROR-FILE1-123.xlsx”},{Key:“file/PROCESS-FILE1-123.xlsx”},{Key:“file/PROCESS-FILE2-111.xlsx”},{Key:“file/SUCCESS-FILE2-111.xlsx”},{Key:“file/PROCESS FILE3-121.xlsx”},
结果=对象值(数据减少((r,o)=>{
var[左、州、右]=获取部件(o.Key),
文件=左+右;
if(left==未定义)返回r;
如果(!r[file]| | r[file].state==='PROCESS')r[file]={o,state};
返回r;
}, {}))
.map({o})=>o);
控制台日志(结果)代码>
.as console wrapper{max height:100%!important;top:0;}
您可以将状态与字符串分开,按文件名分组,并获得优先的非进程对象
函数getParts{
返回(s.match(/^(.*)(ERROR | PROCESS | SUCCESS)-(.*)$/)| |[])。切片(1);
}
var data=[{Key:“errorbyte.xlsx”},{Key:“file/ERROR-FILE1-123.xlsx”},{Key:“file/PROCESS-FILE1-123.xlsx”},{Key:“file/PROCESS-FILE2-111.xlsx”},{Key:“file/SUCCESS-FILE2-111.xlsx”},{Key:“file/PROCESS FILE3-121.xlsx”},
结果=对象值(数据减少((r,o)=>{
var[左、州、右]=获取部件(o.Key),
文件=左+右;
if(left==未定义)返回r;
如果(!r[file]| | r[file].state==='PROCESS')r[file]={o,state};
返回r;
}, {}))
.map({o})=>o);
控制台日志(结果)代码>
.as控制台包装{最大高度:100%!重要;顶部:0;}
KeywordString=[
{
键:“file/ERROR-FILE1-123.xlsx”
},
{
键:“文件/专业”
KeywordString = [
{
Key: "file/ERROR-FILE1-123.xlsx"
},
{
Key: "file/PROCESS-FILE1-123.xlsx"
},
{
Key: "file/PROCESS-FILE2-111.xlsx"
},
{
Key: "file/SUCCESS-FILE2-111.xlsx"
},
{
Key: "file/PROCESS-FILE3-121.xlsx"
}
];
newJSON = [];
constructor() {
let map = new Object();
for (var index = 0; index < this.KeywordString.length; index++) {
if (
map[
this.KeywordString[index].Key.substr(
this.KeywordString[index].Key.length - 15
)
]
) {
} else {
map[
this.KeywordString[index].Key.substr(
this.KeywordString[index].Key.length - 15
)
] = true;
this.newJSON.push(this.KeywordString[index]);
}
}
console.log(this.newJSON);
}
}
let arr = [
{
"Key": "file/ERROR-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE2-111.xlsx",
},
{
"Key": "file/SUCCESS-FILE2-111.xlsx",
},
{
"Key": "file/PROCESS-FILE3-121.xlsx",
},
];
const filterBy = (keep, remove1, remove2) => {
let values = arr.map(item => item.Key);
let keeps = values.filter(item => item.includes(keep));
let names = keeps.map(item => item.replace(`file/${keep}-FILE`,""));
names.forEach(name => {
arr = arr.filter(item => item.Key !==`file/${remove1}-FILE${name}`);
if(remove2)
arr = arr.filter(item => item.Key !==`file/${remove2}-FILE${name}`);
});
}
filterBy("ERROR", "SUCCESS", "PROCESS");
filterBy("SUCCESS", "PROCESS");
//arr <<-- This is output;
// Sample Files Array
var files = [
{
"Key": "file/ERROR-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE1-123.xlsx",
},
{
"Key": "file/PROCESS-FILE2-111.xlsx",
},
{
"Key": "file/SUCCESS-FILE2-111.xlsx",
},
{
"Key": "file/PROCESS-FILE3-121.xlsx",
},
]
// This will contain final processed output
var final_list = []
// Creating priority for every status
var priority = {
"SUCCESS":1,
"ERROR":2,
"PROCESS":3
}
// Temporary object for storing status on the basis of filenames
var temp = {}
// Logic
for(var i=0;i<files.length;i++){
let file = files[i];
// splitting filename "file/PROCESS-FILE1-123.xlsx"
let re = file.Key.split('/')[1].split('-');
status = re[0]; // "PROCESS"
filename = re[1]+"-"+re[2]; // "FILE1-123.xlsx"
if(!(filename in temp)){
temp[filename] = status;
}else{
if(priority[status] < priority[temp[filename]]){
temp[filename] = status
}
}
}
// Making of final object
for(key in temp){
final_list.push({"Key":"files/"+temp[key]+"-"+key})
}
console.log(final_list);