1👍
Ok, browsing through docs for JS Array at Mozilla Developer Network, I’ve tested out the solution for my problem.
Here’s the code
const myObj = {
"firstName": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"lastName": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"editorialOffice": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"email": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"birthDate": {
"fieldType": "DATE",
"value": null,
"enabled": true,
"required": true
},
"birthPlace": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": false
},
"fatherName": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"identityCard": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"bothDocuments": false,
"pesel": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"passportNo": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": false
},
"passportExpirationDate": {
"fieldType": "DATE",
"value": null,
"enabled": true,
"required": false
},
"pressCardNumber": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"editorialOfficePhone": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"fax": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": false
},
"mobilePhone": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"position": {
"fieldType": "RADIO",
"value": null,
"enabled": true,
"required": true
},
"citizenship": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"plane": {
"fieldType": "SELECT",
"value": null,
"enabled": true,
"required": false
},
"bus": {
"fieldType": "SELECT",
"value": null,
"enabled": true,
"required": false
},
"hotel": {
"fieldType": "SELECT",
"value": null,
"enabled": true,
"required": false
},
"transmission": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": false
},
"parking": {
"fieldType": "SELECT",
"value": null,
"enabled": true,
"required": false
},
"carInfo": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": false
},
"statement": {
"fieldType": "TEXT",
"value": null,
"enabled": true,
"required": true
},
"descriptiveFields": [{
"fieldType": "RADIO",
"value": null,
"enabled": false,
"required": false
},
{
"fieldType": "TEXTAREA",
"value": null,
"enabled": false,
"required": false
},
{
"fieldType": "TEXT",
"value": null,
"enabled": false,
"required": false
}
]
}
const generalSectionPatterns = [/^(fir)/, /^(las)/, /^((?=\w+D)bir\w+)/, /^((?=\w+P)bir\w+)/, /^(fat)/, /^(cit)/, /^(ide)/, /^(pes)/, /^((?=\w+N)pas\w+)/, /^((?=\w+E)pas\w+)/]
const contactSectionPatterns = [/^(ema)/, /^(mob)/, /^(pre)/]
const positionSectionPatterns = [/^(pos)/]
const editorialSectionPatterns = [/^((?!\w+Phone)edi\w*)/, /^((?=\w+Phone)edi\w*)/, /^(fax)/]
const flightSectionPatterns = [/^(bus)/, /^(car)/, /^(par)/, /^(hot)/, /^(pla)/, /^(tra)/, /^(des)/]
const generalSectionElements = generalSectionPatterns.map(pattern => Object.keys(myObj).find(elem => pattern.test(elem)))
console.log('sorted generalSectionElements:\n', generalSectionElements)
const contactSectionElements = contactSectionPatterns.map(pattern => Object.keys(myObj).find(elem => pattern.test(elem)))
console.log('sorted contactSectionElements:\n', contactSectionElements)
const positionSectionElements = positionSectionPatterns.map(pattern => Object.keys(myObj).find(elem => pattern.test(elem)))
console.log('sorted positionSectionElements:\n', positionSectionElements)
const editorialSectionElements = editorialSectionPatterns.map(pattern => Object.keys(myObj).find(elem => pattern.test(elem)))
console.log('sorted editorialSectionElements:\n', editorialSectionElements)
const flightSectionElements = flightSectionPatterns.map(pattern => Object.keys(myObj).find(elem => pattern.test(elem)))
console.log('sorted flightSectionElements:\n', flightSectionElements)
You can live test the code at repl.it also.
Second solution is to use regex-sort:
function regexSort(list, patterns) {
function presetIndex(input) {
for (var i = 0; i < patterns.length; ++i) {
if (patterns[i].test(input)) {
return i;
}
}
return Infinity;
}
let indexes = list.map(c => ({
input: c,
index: presetIndex(c)
}));
indexes.sort(
(a, b) => a.index < b.index ? -1 : 1
);
return indexes.map(c => c.input);
};
The third is problably combining some of the Array methods with .sort(), but as I’ve written above, didn’t figured out what precisely to compare inside sort’s comparator function…
Thank you for interest in my problem anyway and giving me any reply at all!
Cheers!
Source:stackexchange.com