2016-07-13 193 views
0

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

我具有如

vm.data.detail 

所以我有让用户CRUD的模块形式,其定义的阵列。该模块是关于产品库存。有一个按钮可以在我的角度控制器中运行addProduct()函数。

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.

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

请注意,阵列推送的对象已经是正确的。我只是不知道该怎么说。看起来像里面的逻辑仍然缺乏的东西,但我不知道什么是缺少的

非常感谢你给予的帮助。

+0

我看不到PHP,为什么标签? – RiggsFolly

+0

对不起,先生,我错误地认为:) – Vinfoster0701

+0

看起来远比它需要更复杂。需要查看视图才能看到这是如何使用的 – charlietfl

回答

0

你commiting一个简单的错误,你正在做的else检查数组里面,你必须将它移动到外面。

提示:

您可以使用Array.prototype.find()的方法来检查,如果在数组中,远好于执行for循环传统存在的元素。

正如你可以在文档已经注意到,找到方法有IE浏览器和Opera浏览器不兼容,所以如果你需要这种兼容性,可以使用Array.prototype.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: '' 
     }); 
    } 
    } 
} 

我希望它能帮助!

+1

Sir。你摇滚!谢谢! – Vinfoster0701

0

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

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 
} 
-1

我觉得问题就在这里: 因为产品是Detail对象的属性,产品没有名称属性,它不符合条件并转到其他条件并推入名称列表。

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