代码之家  ›  专栏  ›  技术社区  ›  chris cozzens

即使包含元素,IndexOf(elem)始终返回-1

  •  0
  • chris cozzens  · 技术社区  · 8 年前

    我试图循环浏览以下格式的列表(origPlan),并计算列表中每个项目的数量,然后将其转换为最终结果,如下所示:

    finalPlan =[{"first":2},{"second":2}]
    

    我的逻辑如下,但在if语句中它总是返回-1,每次我都在执行else语句,我缺少什么?如果不包含元素,是否有替代indexOf函数返回-1的方法?我想我可能无法正确访问finalPlan中的项目,但我不确定如何继续。

    var origPlan = {"first","second","first","second"}
    var finalPlan = [];
    
    //loop through each element of my array 
    for(var i =0; i<origPlan.length; i++){
    
      //check to see if the element has been added to my new array already
      if(finalPlan.indexOf(origPlan[i]) == -1){
    
      //if not, lets add it here with quantity of 1 
       finalPlan.push({
          variantId: origPlan[i],
          quantity: 1
         });
      }
    
      //other wise I will increase quantity here: logic not included for simplicity
      else{console.log("duplicated");}
    }
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Akxe    8 年前

    问题是, {} != {} ,因为他们的参考文献不同。对一个人来说,它们可能是同一个对象,但在内部,它们指的是不同的对象。

    您可能需要使用一些数组函数,如 find , map , filter .

    在这个例子中,最好是利用, Array.reduce

    var origPlan = ["first", "second", "first", "second"]
    var finalPlan = origPlan.reduce((acc, element) => {
        if(acc[element]){
            acc[element]++
        }else{
            acc[element] = 1
        }
        return acc
    }, {})
    console.log(finalPlan)
        2
  •  1
  •   Ele    8 年前

    您需要使用 findIndex 功能并检查每个对象的键。

    假设中每个对象只有一个键 finalPlan

    finalPlan.findIndex((f) => Object.keys(f)[0] === origPlan[i]) != -1
    

    重要事项: 您需要检查您的逻辑/条件。

    这就是我在您的方法中所做的修复:

    finalPlan.findIndex((f) => Object.keys(f)[0] === origPlan[i]) === -1
                                                                  ^
    

    var origPlan = [
      "first",
      "second",
      "third",
      "fourth"
    ]
    
    var finalPlan =[{"first":2},{"second":2}]
    
    //loop through each element of my array 
    for (var i = 0; i < origPlan.length; i++) {
    
      //check to see if the element has been added to my new array already
      if (finalPlan.findIndex((f) => Object.keys(f)[0] === origPlan[i]) === -1) {
    
        //if not, lets add it here with quantity of 1 
        finalPlan.push({
          variantId: origPlan[i],
          quantity: 1
        });
      }
    
      //other wise I will increase quantity here: logic not included for simplicity
      else {
        console.log("duplicated");
      }
    }
    
    console.log(finalPlan)
        3
  •  0
  •   VHS    8 年前

    您在 if 循环不太正确。由于数组finalPlan是一个复杂对象的数组,而origPlan是一个字符串数组,因此比较总是会失败并返回-1。请尝试以下if条件:

    var origPlan = ["first","second","first","second"]
    var finalPlan = [];
    
    //loop through each element of my array 
    for(var i =0; i<origPlan.length; i++){
    
      //check to see if the element has been added to my new array already
      if (!finalPlan.some(e => e.variantId === origPlan[i])) {
    
      //if not, lets add it here with quantity of 1 
       finalPlan.push({
          variantId: origPlan[i],
          quantity: 1
         });
      }
    
      //other wise I will increase quantity here: logic not included for simplicity
      else{
        console.log('duplicated');
       }
    }