如何检查字符串的字符是否已经是数组的元素?

How can I check if a character of a string is already a element of an array?

使用字符串,例如: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");