如何删除具有相同值的jQuery? [英] How do I remove jQuery with the same value?

查看:72
本文介绍了如何删除具有相同值的jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,但我不能. JSON有数据.我正在某处进行循环推送.但是再次添加了相同的值.我想解决这个问题.只有一个可以删除具有相同值的名称.

My question is very simple, but I could not. JSON has a data. I'm doing push with loop somewhere. But the same values ​​are added again. I want to solve this. There will be only one to remove names with the same value.

摘要;

thelist = [{"name": "test", "name": "test2", "name": "test"}]

我想删除相同的名字.只有一个.

I want to delete the same names. There will only be one.

控制台示例图片;

示例jQuery代码;

     var $el = this;
     var groupedItem = _.groupBy(itemList, function(t) { return t.unitPriceCurrency });

    $.each(groupedItem, function( key, value ) {
        var obj = JSON.stringify(value);
        var stringify = JSON.parse(obj);
        var amount = 0;
        var tax = 0;

        for (var i = 0; i < stringify.length; i++) {
            amount += stringify[i]['unitPrice'] * stringify[i]['quantity'];
            tax += stringify[i]['unitPrice'] * stringify[i]['quantity'] * stringify[i]['taxRate'] / 100;
        }

        var totalAmount = Math.round(amount * 100) / 100;
        var withTaxTotal = totalAmount + tax;

        for(let i = 0; i < totalLayout.length; i++) {                                
            totalLayout =  $.grep(totalLayout, function(value) {
                return totalLayout[i]["name"] != value.name;
            });
        }

        totalLayout.push(
            {"name": key + ' Amount'},
            {"name": key + ' Tax'},
            {"name": key + ' Total Amount'}
        );

        $el.model.set(key + ' Amount', totalAmount);
        $el.model.set(key + ' Tax', Math.round(tax * 100) / 100);
        $el.model.set(key + ' Total Amount', withTaxTotal);
    });

推荐答案

在这里尝试使用filterfindIndex

  thelist = [{"name": "test"}, {"name": "test2"}, {"name": "test"}]

  Array.prototype.distinctBy= function(key){
     return this.filter((value, index, self)=> {
      return self.findIndex((v, i)=> v[key] === value[key]) === index
     });
  
  }

  console.log(thelist.distinctBy("name"))

这篇关于如何删除具有相同值的jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆