I have an object/output in a very specific format detail below:
result =
AB: [ 0:{Name: "Tom", team:"Spirit", position: "Defender", score: 22 }
1:{Name: "Paul", team:"Vikings", position: "center" }
2:{Name: "Jim", team:"United", position: "Wing", }
3:{Name: "Greg", team:"Crusaders", position: "Fullback", score: 54}
4:{Name: "Tim", team:"Vikings", position: "Fullback", score: 77 }
]
CD: [0:{...},1:{...},2:{...},3:{...},4:{...}]
EF: [0:{...},1:{...},2:{...},3:{...},4:{...}]
GH: [0:{...},1:{...},2:{...},3:{...},4:{...}]
The result has nested arrays. On some outputs the property of score is not there, but I need it to be - if its not there I need to add it by default as score:"" if it is there then just leave it alone.
I have been able to add the property of score but only at the top level of the result object e.g.
...
GH: [0:{...},1:{...},2:{...},3:{...},4:{...}]
score:""
by
if(result.hasOwnProperty("score")) {
alert('Item has already that property');
} else {
result.score = "" ;
}
I can target a specific path by(below) but I want to apply it to all:
if(finalResult['AB'][1].hasOwnProperty("durabilityScore")) {
alert('Item has already that property');
} else {
finalResult['AB'][1].durabilityScore = 'value';
}
I am using Lodash if that helps. Thanks
if..elseblock:result.score = result.score || ''Object.keys(result).forEach((key) => result[key].forEach((obj) => obj.score = obj.score || ''; ))