简体   繁体   English

array.push()重复相同的值

[英]array.push() repeats same value

This is an object I tried to create to manage a list, but somethings wrong somewhere, whenever I used the list.addItem(); 这是我尝试创建的用于管理列表的对象,但是每当我使用list.addItem();时,某些地方list.addItem(); for the second or third time, all the values(array) in the list.list; 第二次或第三次, list.list;中的所有值(数组) list.list; changes to the finally added value(array). 更改为最终增加的值(数组)。

var list = {
  list : [],
  eligible: function(item){
    var status = true;
    var dw = (item.w*item.h*item.l)/169;
    if(dw > item.m){
      status = true;
    }else{
      status = false;
    }
    return status;
  },
  addItem : function(item){
    if(this.eligible(item)){
      this.list.push(item);
      console.log(this.list);
      this.refresh();
      alertify.success('Item Added to List');
    }else{
      alertify.warning('Item not eligible for Freight Calculation');
    }
  },
  removeItem : function(item){
    if(this.inList(item)){
      var itemIndex = this.list.indexOf(item);
      if(itemIndex > -1){
        this.list.splice(itemIndex,1);
        alertify.success('Item Removed from List');
        this.refresh();
      }
    }else{
      alertify.error('Item NOT in List');
    }
  },
  inList: function(item){
    var bool = false;
    if (this.list.filter(function(e) { return e.id === item.id; }).length > 0) {
      bool = true;
    }else{
      bool = false;
    }
    return bool;
  },
  findItem: function (id) {
    for (var i = 0; i < this.list.length; i++) {
      if (this.list[i].id === id) {
        return this.list[i];
      }
    }
  },
  refresh: function(){
    if(this.list.length > 0){
      $('.items .table tbody').empty();
      var itemNo = 0;
      $.each( this.list, function( key, item ) {
        $('.items .table tbody').append('</tr><tr><td>' + ++itemNo + '</td><td>' + item.qty + '</td><td>' + item.m + '</td><td>' + item.h + '</td><td>' + item.w + '</td><td>' + item.l + '</td><td><button data-item=\"' + item.id + '\" class=\"btn btn-remove btn-block\"><i class=\"far fa-minus-square\"></i></button></td><td>Price</td></tr>')
      });
    }else{
      $('.items .table tbody').html('<tr><td colspan=\"8\" style=\"text-align:center;\"> No Items Added.</td></tr>')
    }
  }
};

I can't find whats wrong, maybe it's because I've been trying this all day. 我找不到错在哪里,也许是因为我整天都在尝试。 Btw I'm new to programming. 顺便说一句,我是编程新手。

UPDATE: This is how i call the addItem: 更新:这就是我所谓的addItem:

    var id=0; //just for reference     
    $('.btn-add-item').click(function(){
        var item = [];
        item.id = ++id;
        item.qty = parseInt($('input[name=qty]').val());
        item.m = parseFloat($('input[name=weight]').val()).toFixed(3);
        item.w = parseFloat($('input[name=width]').val()).toFixed(2);
        item.h = parseFloat($('input[name=height]').val()).toFixed(2);
        item.l = parseFloat($('input[name=length]').val()).toFixed(2);
        item.country = $('#countrySelect').val();
        list.addItem(item);
    });

In your click handler you are assigning each input element's value to the variable item as if item were an Object. 在单击处理程序中,您正在将每个输入元素的值分配给变量item ,就好像item是一个Object一样。 Unfortunately, you've initialized item as an Array. 不幸的是,您已将item初始化为Array。 You should initialize item as an Object. 您应该将item初始化为Object。 Then, your list Array will contain a list of Objects. 然后,您的list数组将包含一个对象list

Change 更改

var item = [];

To

var item = {};

Since you are new to programming, and Javascript is sort special in an odd way with this code, please let me explain why there was no error thrown to let you know this. 由于您是编程的新手,并且此代码对Javascript的使用有些奇怪,请让我解释一下为什么没有错误提示您。

In JavaScript, Arrays are actually Objects. 在JavaScript中,数组实际上是对象。 So assigning a value like you have ( item.blah ) actually places that property on the item Array Object as a property, but doesn't know your intent is to add the value to the list of Array elements. 因此,像您一样分配一个值( item.blah )实际上将该属性作为属性放置在Array对象项上,但是不知道您的意图是将该值添加到Array元素列表中。 Javascript carries out what it believes is your intent. JavaScript会执行您认为的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM