Reputation: 371
I have following 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-121.xlsx",
},
]
What I want to achieve here first check the last part of the JSON key string if it is the same then I will check PROCESS and ERROR and will display the ERROR file...
for example in my JSON key[0]
is "file/ERROR-FILE1-123.xlsx" and key[1]
is "file/PROCESS-FILE1-123.xlsx" so for both the keys
-FILE1-123.xlsx is same so will filter error file and add it in new JSON. same with other JSON if last part is same then will give priority to SUCCESS and ERROR and add only these file to new JSON but if PROCESS key is single I mean no ERROR or SUCCESS is available then will display only Process file
So my expected new JSON should be like this. Kindly help how to achieve this as I'm completely new to UI technology still in a learning phase. If is there any better way to achieve this please share
[
{
"Key": "file/ERROR-FILE1-123.xlsx",
},
{
"Key": "file/SUCCESS-FILE2-111.xlsx",
},
{
"Key": "file/PROCESS-FILE3-121.xlsx",
},
]
Upvotes: 0
Views: 151
Reputation: 1030
Try using:
var jsonObj = [{"Key":"ERROR-FILE1-2020.xlsx"},{"Key":'PROCESS-FILE1-2020.xlsx'},{"Key":"errorbyte.xlsx"},{"Key":"SUCCESS-FILE2-2020.xlsx"},{"Key":"PROCESS-FILE2-2020.xlsx"},{"Key":"PROCESS-FILE3-2020.xlsx"}]
var obj = {};
jsonObj.forEach((elem) => {
let file_name = elem.Key.substring(elem.Key.indexOf("-") + 1)
let status = elem.Key.substring((elem.Key.indexOf("/") + 1), elem.Key.indexOf("-"))
if(status!== ''){
if (obj[file_name] === undefined) {
obj[file_name] = status;
}
else {
if (status === 'SUCCESS') {
obj[file_name] = status;
}
if (status === 'ERROR') {
if(obj[file_name] !== 'SUCCESS'){
obj[file_name] = status;
}
}
}}
})
var result = [];
for(key in obj){ let x = {}
x['Key'] ='file/'+obj[key]+'-'+key
result.push(x)
}
console.log(result)
Hope it helps.
Upvotes: 2
Reputation: 386560
You could separate the state from the strings, group by filename and get a priorized non process object.
function getParts(s) {
return (s.match(/^(.*)(ERROR|PROCESS|SUCCESS)-(.*)$/) || []).slice(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" }],
result = Object.values(data.reduce((r, o) => {
var [left, state, right] = getParts(o.Key),
file = left + right;
if (left === undefined) return r;
if (!r[file] || r[file].state === 'PROCESS') r[file] = { o, state };
return r;
}, {}))
.map(({ o }) => o);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 2
Reputation: 16277
Try this: https://runkit.com/embed/jmcxcv47el2f
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;
Upvotes: 0
Reputation: 1835
// 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);
Upvotes: 1
Reputation: 23515
We are going to loop on the array and build a new one, keeping the code and the type of every Key.
At every value, we are going to check what we have on the array we are building, and either replace the value or push a new one.
At the very end of the Array.reduce
we will have an object containing the code, the Key and the type. We mutate it using a Array.map
to obtain the desired output.
// Assign a numeric value to every type of file you can get
// The purpose will be to be able to compare types altogether and
// choose which one to keep in case of conflict
function getTypeValue(type) {
return ({
PROCESS: 1,
SUCCESS: 2,
ERROR: 3,
})[type];
}
const 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 ret = Object.values(arr.reduce((tmp, {
Key
}) => {
// use a regex to extract the interesting values
const [,
type,
code,
] = /(ERROR|PROCESS|SUCCESS)-FILE[0-9]{1}-([0-9]*).xlsx/.exec(Key);
const typeValue = getTypeValue(type);
if (tmp[code]) {
// Check if the new entry should replace the one already stored
if (tmp[code].typeValue < typeValue) {
tmp[code] = {
Key,
typeValue,
};
}
} else {
tmp[code] = {
Key,
typeValue,
};
}
return tmp;
}, {})).map(({
Key,
}) => ({
Key,
}));
console.log(ret);
Upvotes: 1
Reputation: 1298
please check the stackbliz and the console of the code
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);
}
}
I made it as an array of object
Upvotes: 0