Javascript findIndex提供递归结果

时间:2017-01-03 20:56:54

标签: javascript

我有一个表,每个单元格中有两个下拉列表,当用户更改默认值时,我想记录更改,将它们传递给查看(带有确认按钮),并将它们记录在数据库中。

我使用changed_element记录更改的元素(列表是列表:type,row,column)和changed_value来记录新值。如果用户更多次更改相同的下拉列表,我的诅咒只想记录最后一个值,因此changed_element不允许重复。

这是我的代码:

...
var changed_element=[]
var changed_value=[];
var index=-1

document.getElementById('tabella').addEventListener('change', function(event){
  var elem = event.target; //detect the element changed
  var element=[elem[elem.value].getAttribute('type'), 
    elem[elem.value].getAttribute('primo'), 
    elem[elem.value].getAttribute('secondo')] //records the coordinates (strings, not numbers)
  index=changed_element.findIndex(element_exist); //controls if already exist
  console.log('index', index)
  console.log('element', element)

  if (index==-1) { //new element
    changed_element.push(element);
    changed_value.push(elem[elem.value].text);
  } else { //already exist, so update the value
    changed_value[index]=elem[elem.value].text;
  };

  function element_exist(array) {
    console.log('array', array)
    return array==element; //return the index of the element if already in list or -1
  };
});

但它不起作用...索引总是-1

编辑:现在我明白了下面发生了什么:为元素调用该函数一次,直到找到double / edit

首先更改console.logs给出:

index -1

element Array [ "whenever", "something", "someotherthing" ]

在第二次更改时(在相同的下拉列表中)给出:

array Array [ "whenever", "something", "someotherthing" ]

index -1 

element Array [ "whenever", "something", "someotherthing" ]

但是数组和元素是相同的,所以索引应该是0 ...在第三次更改时(在同一个下拉列表中)给出:

array Array [ "whenever", "something", "someotherthing" ]

array Array [ "whenever", "something", "someotherthing" ] //it add one more and so on

index -1

element Array [ "whenever", "something", "someotherthing" ]

为什么它调用函数element_exist的两倍?

现在我改变了另一个下拉列表,它给出了:

array Array [ "whenever", "something", "someotherthing" ]

array Array [ "whenever", "something", "someotherthing" ]

array Array [ "whenever", "something", "someotherthing" ]

index -1

element Array [ "whenever new", "something new", "someotherthing new" ]

因此元素不同,索引是正确的-1,但函数element_exist被调用三次。

1 个答案:

答案 0 :(得分:0)

所以,这是我的解决方案:

...
var changed_element=[]
var changed_value=[];

document.getElementById('tabella').addEventListener('change', function(event){
    var elem = event.target; //detect the element changed
    var element=[elem[elem.value].getAttribute('type'), 
        elem[elem.value].getAttribute('primo'), 
        elem[elem.value].getAttribute('secondo')] //records the coordinates (strings, not numbers)
    var index=element_exist(changed_element, element); //controls if already exist

    if (index==-1) { //new element
        changed_element.push(element);
        changed_value.push(elem[elem.value].text);
    } else { //already exist, so update the value
        changed_value[index]=elem[elem.value].text;
    };

    function element_exist(changed_element, element) {
        for (i=0; i<changed_element.length; i++) {
            if (element[0]==changed_element[i][0] && element[1]==changed_element[i][1] && element[2]==changed_element[i][2]) {
                return i;
            };
        };
        return -1
    };
};

它不使用findIndex,这更好,因为许多浏览器不支持它。不幸的是,它仅适用于len(element)=3,但对我来说没关系