I use the following code to find if there is duplicate value in property path
:
return new Promise((resolve, reject) => {
var data = [];
Parser.parse()
//configObj is the value from the screen shot
.then((configObj) => {
configObj.forEach((providers) => {
_.each(providers, (provider) => {
_.each(provider, (entry) => {
for (var prop in entry) {
if (_inArray(entry[prop].path, data)) {
return reject(new Error(`Duplicate path ${entry[prop].path}`));
}
data.push(entry[prop].path);
}
})
})
})
resolve("validObject");
}, err => {
reject(err);
});
For example, this is the object configObj
and as you can see I need to find duplicate in object 'replace'
and object 'save'
for the value in field 'path'
; if they have the same value, it rejects with error; otherwise it resolves the promise. Is there is more elegant way to write it?
In this example, the path is different (test2 & command1) but it can also be the same.
update
[
{
"providers": [
{
"replace": {
"path": "command1",
"function": "updateResources",
"method": "post"
},
"save": {
"path": "test2",
"function": "updateResources",
"method": "post"
}
}
]
}
]