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

Javascript 编辑表单中onChange的JSX字典问题,javascript,dictionary,hashmap,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 inpu

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

我正在寻找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 
}

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

// 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;

尝试一些代码,然后发布一个更具体的问题。这样看来,您所做的唯一一件事就是创建一个空对象,因此我无法以我认为能够使用dictionary[name]++的方式递增。您是否遇到错误?当您使用
字典[name]++
时会发生什么?在上面的例子中,
dictionary['john']+
将计数增加到2。他没有这样做,因为在他的代码中,每次调用func时,他都会将dictionary初始化回一个空对象