Take note that the code displayed below showcases the array in the console, rather than in the snippet output
var names = ["maria", "mary", "marks", "michael"];
function add_suffix(names) {
var suffixes = [];
for (var i = 0; i < names.length; i++) {
//console.log(current);
var name = names[i];
var letters = name.split("");
var current = suffixes;
console.log(current);
for (var j = 0; j < letters.length; j++) {
var letter = letters[j];
var pos = current[letter];
if (pos == null) {
current = current[letter] = j == letters.length - 1 ? 0 : {};
} else {
current = current[letter];
}
}
}
}
add_suffix(names);
The above code produces this output:
M :{a : {r :{i :{a :0},
k :0,
y :
},
},
i :{c :{h :{a :{e :{l :0}}}}}}}
However, I am aiming to achieve this specific output:
M :{ar:{ia:0,
k :0,
y :0
},
ichael :0
}
Is there anyone who can assist me in obtaining this desired output from my code? How can I modify it to achieve the intended result?