编辑表单中onChange的JSX字典问题

时间:2020-01-13 18:04:01

标签: javascript dictionary hashmap

我有一个带有输入字段的表单,用户可以在其中输入任何名称。我需要检查此名称是否已经存在,但是在编辑字段中,它需要忽略其原始名称作为重复名称。

我正在寻求js方法的帮助

const isUnique = input => {
  const dictionary = {};
  // dictionary[iGetExistingNames] = 1
  // so now the dictionary = {'a':1, 'b':1, 'c':1}

  // how do I add new inputs as a key and set value to +1 and when backspace reduce the count?

  // return bool 
}

1 个答案:

答案 0 :(得分:0)

您将需要检查您的dictionary是否包含输入。如果不是,则添加它,否则它是重复的并增加计数。

// Say you have a dictionary with the key named john
const dictionary = {'john': 1}

// Check for the key using
dictionary['john']  // This should return 1.

// Add new keys using
dictionary[input] = 0;
相关问题