用字符串表示,例如:data
。我想在数组中插入所有新字符,尊重我给出的例子答案应该是:
characters = ["d","a","t"];
我做了以下事情:
const balanced = string => {
var characters = [];
var characters_Counter = 0;
for (var i = 0; i < string.length; i++) {
if (string.charAt(i) != characters){
characters[characters_Counter] = string.charAt(i);
characters_Counter++;
}
console.log(characters[i]);
}
};
在if
中,我想比较string.charAt(i)
与characters[]
中的所有元素,如果它是一个新字符,则做if
的内容。问题是:我被困住了,我想不出一个方法来做这个比较。
使用includes()
方法检查元素(字符)是否在数组内
const balanced = string => {
var characters = [];
var characters_Counter = 0;
for (var i = 0; i < string.length; i++) {
if (!characters.includes(string.charAt(i))) { // <------ here
characters[characters_Counter] = string.charAt(i);
characters_Counter++;
console.log(characters[i]);
}
}
};
balanced("data");