请注意,下面的代码在控制台中显示了数组,而不是在代码段输出中
var nodes = ["maria", "mary", "marks", "michael"];
function insert_word(split_nodes) {
var rest = [];
for (var i = 0; i < split_nodes.length; i++) {
//console.log(current);
var word = split_nodes[i];
var letters = word.split("");
var current = rest;
console.log(current);
for (var j = 0; j < letters.length; j++) {
var character = letters[j];
var position = current[character];
if (position == null) {
current = current[character] = j == letters.length - 1 ? 0 : {};
} else {
current = current[character];
}
}
}
}
insert_word(nodes);
上面输出这个
M :{a : {r :{i :{a :0},
k :0,
y :
},
},
i :{c :{h :{a :{e :{l :0}}}}}}}
但我想输出这个:
M :{ar:{ia:0,
k :0,
y :0
},
ichael :0
}
谁能帮我从我的代码中找出这个输出?我怎么能用这段代码做后缀?