【问题标题】:Checking array object existence检查数组对象是否存在
【发布时间】:2016-07-13 16:05:51
【问题描述】:

所以,我正在尝试检查最近推送的数组对象是否存在。 我正在使用 angularJS 开发 Web 应用程序。

我有一个定义为

的数组
vm.data.detail

所以我有一个表单可以让用户对模块进行 CRUD。该模块是关于产品库存。在我的角度控制器中有一个按钮可以运行 addProduct() 函数。

添加产品功能:

function addProduct() {

  //to check whether the array is empty.
  if (vm.data.detail.length == 0) {
    vm.data.detail.push({
      product: vm.data.product_id.selected,
      current_qty: vm.data.product_id.selected.qty,
      new_qty: Number(vm.data.product_id.selected.qty) - Number(1),
      difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)),
      remarks: ''
    });
    console.log("Product just been added");
  }

  //if the array is not empty
  else {
    for (var i = 0; i < vm.data.detail.length; i++) {

      //to check whether the selected product is already inside the array
      if (vm.data.product_id.selected.name == vm.data.detail[i].product.name) {
        console.log("same product selected");
        //data
      }

      //if there is no selected product inside the array, then add it
      else {
        console.log("different product has just been selected");
        vm.data.detail.push({
          product: vm.data.product_id.selected,
          current_qty: vm.data.product_id.selected.qty,
          new_qty: 0,
          difference: 0,
          remarks: ''
        });
      }
    }
  }
}

当数组只包含一个产品时,上面的代码运行良好。当我尝试将另一个产品 B 添加到产品中时,会出现此问题。这是条件:

  1. 产品 A 已经在数组中。
  2. 选择产品 B,然后将其添加到数组中。现在该数组包含 2 个产品。
  3. 当我测试添加一个新产品 B 时,我不知道为什么数组仍然被一个新产品 B 推送。所以现在,该数组包含 3 个产品(1 个产品 A 和 2 个产品 B) .

我想要的是,当我尝试添加第二个产品 B 时,数组不会被新产品 B 推送。

我在这里缺少什么?已经处理了几个小时,无法弄清楚我必须为“验证”添加什么。

请注意,数组推送的对象已经正确。我只是不知道如何放置if else。看起来里面的逻辑还是少了点什么,但是我不知道少了什么

非常感谢您提供的帮助。

【问题讨论】:

  • 我看不到 PHP 为什么是标签?
  • 对不起我的坏先生,我认为是点击错误:)
  • 看起来比它需要的复杂得多。需要查看视图才能了解它是如何使用的
  • 恐怕如果我添加了视图,它只会使线程混乱先生。但基本上所有必要的东西都已经包含在函数中了。代码中的逻辑看起来缺少什么,但我找不到它的正确逻辑是什么

标签: arrays angularjs


【解决方案1】:

你犯了一个简单的错误,你在数组里面检查else,你必须把它移到外面。

提示:

您可以使用Array.prototype.find() 方法来检查元素是否存在于数组中,这比执行传统的for-loop要好得多。

您可能已经在文档中注意到,find方法不兼容IE和Opera浏览器,所以如果您需要这种兼容性,可以使用Array.prototype.filter() .

以下是代码,包含两个版本(带有 find 和 filter)以及必要的修改:

function addProduct() {
  //to check whether the array is empty.
  if (!vm.data.detail.length) { // you can simply use (!length) instead of comparing with 0
    vm.data.detail.push({
      product: vm.data.product_id.selected,
      current_qty: vm.data.product_id.selected.qty,
      new_qty: Number(vm.data.product_id.selected.qty) - Number(1),
      difference: Number(vm.data.product_id.selected.qty) - (Number(vm.data.product_id.selected.qty) - Number(1)),
      remarks: ''
    });
    console.log("Product just been added");
  }

  //if the array is not empty
  else {
    // if there's no obj with the same name inside the array, it returns undefined, otherwise it returns the object.
    var obj = vm.data.detail.find(function(value) {
      return value.product.name == vm.data.product_id.selected.name;
    });

    /* Using FILTER:
    var obj = vm.data.detail.filter(function(value) {
      return value.product.name == vm.data.product_id.selected.name;
    })[0];
    */

    // Now you can test, if the object exists
    if (obj) {
      console.log("same product selected");
    }
    //if there is no selected product inside the array, then add it
    else {
      console.log("different product has just been selected");
      vm.data.detail.push({
        product: vm.data.product_id.selected,
        current_qty: vm.data.product_id.selected.qty,
        new_qty: 0,
        difference: 0,
        remarks: ''
      });
    }
  }
}

希望对你有帮助!

【讨论】:

  • 先生。你摇滚!谢谢!
【解决方案2】:

这是一个基本的逻辑错误。你在做

for each element {
    if element is different from given {
        add given to array
    }
}

你需要做的是

var allElementsDifferentFromGiven = true
for each element {
    if element is same as given {
        allElementsDifferentFromGiven = false
        break
    }
}
if (allElementsDifferentFromGiven) {
    add given to array
}

但是 JavaScript 数组有方法可以做到这一点:

if (array.every(function(element) {
    return true if element is different given
})) {
    add given to array
}

【讨论】:

    【解决方案3】:

    我认为问题在这里: 因为 Detail 对象上有 product 属性,而 product 没有 name 属性,所以它不匹配条件并进入 else 条件并推入名称列表。

    //to check whether the selected product is already inside the array
       if(vm.data.product_id.selected.name == vm.data.detail[i].product.name){
    

    应该是

    //to check whether the selected product is already inside the array
       if(vm.data.product_id.selected.name == vm.data.detail[i].product){
            console.log("same product selected");
            //data
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多