I have this code
let defCores = el.histories;
let cores = [];
for(let i = 0; i < defCores.length; i++){
cores.push({
new_core: defCores[i].new_core.name,
new_color: defCores[i].new_core.color,
old_core: defCores[i].old_core.name,
old_color: defCores[i].old_core.color,
})
}
// using `cores` in my template to show (append data)
let myCores = '';
cores.forEach(core => {
myCores += "<div style='width: 100%; display: inline-block; clear:both; margin-bottom: 5px;'>"+
"<div class='coreColor' style='display: inline-block; width: 20px; height: 15px; background: "+core.old_color+";'></div>"+
"<div class='coreName' style='margin-left: 5px; display: inline-block; width: 25%;'>" + core.old_core + "</div>"+
"<div class='coreName' style='display: inline-block;'><i style='color: red;' class='fas fa-arrow-right'></i> <i style='color: seagreen;' class='fas fa-arrow-left'></i></div>"+
"<div class='coreName' style='margin-left: 5px; display: inline-block; width: 25%;'>" + core.new_core + "</div>"+
"<div class='coreColor' style='width: 20px; display: inline-block; height: 15px; background: "+core.new_color+";'></div>"+
"</div>";
});
But my data some have let defCores = el.histories; array some are empty. like:
Because of this I get this error:
Uncaught (in promise) TypeError: Cannot read property 'name' of null
which comes from this line: new_core: defCores[i].new_core.name,
Now I'm seeking for solution to avoid this error and if my histories array have value return it if not just ignore it (show nothing)
PS: I thought
for(let i = 0; i < defCores.length; i++){would prevent such issue on empty histories but apparently not!
Any idea?

new_corevalue ofnulland you can't read thenameproperty of anull.?to the end of the property, likedefCores[i[?.new_core?.nameThis will is a newer JS operator that only "goes on" to the next selector if the current one doesn't return undefined