如何从我的json数组中删除dulicate值

时间:2017-09-06 07:53:11

标签: javascript angularjs

{
  "GetAllcar": [{
    "cars_id": "1",
    "user_id": "7",
    "car_name": "Maruti Suzuki Swift",
    "car_brand": "Maruti Suzuki",
    "price": "50000",
    "year": "2017",
    "kilometer": "8047",
    "fuel_type": "Diesel",
    "transmission": "Manual",
    "color": "Grey",
    "insurance": "Not Available",
    "seller_type": "1st",
    "available_at": "Salt Lake, Kolkata",
    "reg_city": "Kolkata",
    "image": "BPYZGCY_1158520_1_8695497.jpeg",
    "ucarengine": "2967",
    "ucarpower": "262",
    "ucarmilage": "137"
  }, {
    "cars_id": "22",
    "user_id": "7",
    "car_name": "Maruti Suzuki Swift",
    "car_brand": "Maruti Suzuki",
    "price": "555000",
    "year": "2015",
    "kilometer": "54854",
    "fuel_type": "Diesel",
    "transmission": "Manual",
    "color": "White",
    "insurance": "Comprehensive",
    "seller_type": "First User",
    "available_at": "Kolkata",
    "reg_city": "Kolkata",
    "image": "Suzuki-Swift-Widescreen.jpg",
    "ucarengine": "1248",
    "ucarpower": "74",
    "ucarmilage": "23"
  }, {
    "cars_id": "24",
    "user_id": "7",
    "car_name": "Ferrari 488",
    "car_brand": "Ferrari",
    "price": "9999999",
    "year": "2016",
    "kilometer": "3605",
    "fuel_type": "Diesel",
    "transmission": "Automatic",
    "color": "Blu Tour De France",
    "insurance": "Comprehensive",
    "seller_type": "First User",
    "available_at": "New Town, Kolkata",
    "reg_city": "Kolkata",
    "image": "Ferrari-488-Right-Front-Three-Quarter-61997 .jpg",
    "ucarengine": "3902",
    "ucarpower": "660",
    "ucarmilage": "8"
  }, {
    "cars_id": "25",
    "user_id": "7",
    "car_name": "Lamborghini Huracan",
    "car_brand": "Lamborghini",
    "price": "8800000",
    "year": "2015",
    "kilometer": "5607",
    "fuel_type": "Diesel",
    "transmission": "Automatic",
    "color": "Bianco Canopus",
    "insurance": "Not Available",
    "seller_type": "First User",
    "available_at": "Mumbai",
    "reg_city": "Mumbai",
    "image": "Lamborghini-Hurcan-Right-Side-30064 .jpg",
    "ucarengine": "5204",
    "ucarpower": "631",
    "ucarmilage": "8"
  }, {
    "cars_id": "26",
    "user_id": "7",
    "car_name": "BMW Z4",
    "car_brand": "BMW",
    "price": "4850000",
    "year": "2013",
    "kilometer": "69045",
    "fuel_type": "Petrol",
    "transmission": "Manual",
    "color": "Sparking Brown",
    "insurance": "Comprehensive",
    "seller_type": "First User",
    "available_at": "Salt Lake, Kolkata",
    "reg_city": "Kolkata",
    "image": "BMW-Z4-Left-Front-Three-Quarter-21982_l .jpg",
    "ucarengine": "2979",
    "ucarpower": "302",
    "ucarmilage": "10"
  }, {
    "cars_id": "28",
    "user_id": "9",
    "car_name": "Alto 800",
    "car_brand": "Maruti Suzuki",
    "price": "80000",
    "year": "2016",
    "kilometer": "15090",
    "fuel_type": "Diesel",
    "transmission": "Manual",
    "color": "Red",
    "insurance": "Not Available",
    "seller_type": "First User",
    "available_at": "Chennai",
    "reg_city": "Chennai",
    "image": "1_Maruti-Alto-800-2016.jpg",
    "ucarengine": "1500",
    "ucarpower": "565",
    "ucarmilage": "19"
  }, {
    "cars_id": "29",
    "user_id": "9",
    "car_name": "Bugatti Veyron",
    "car_brand": "Bugatti",
    "price": "10000000",
    "year": "2016",
    "kilometer": "8",
    "fuel_type": "Petrol",
    "transmission": "Automatic",
    "color": "Titanium Grey Metallic",
    "insurance": "Comprehensive",
    "seller_type": "First User",
    "available_at": "Bangalore",
    "reg_city": "Bangalore",
    "image": "Bugatti-Veyron-Right-Front-Three-Quarter-52786_ol.jpg",
    "ucarengine": "7993",
    "ucarpower": "987",
    "ucarmilage": "4"
  }]
}

以上是我的 Json 回复。我想删除fuel_type重复值。我怎样才能做到这一点。提前致谢

1 个答案:

答案 0 :(得分:0)

Array.prototype.removeRepeatAttr = function() {
    var tmp = {}, a = this.slice(); 
    for(var i = j = 0; i < a.length; i++) {
        if(!tmp[a[i].id]) {
            tmp[a[i].id] = !0;
            j++;
        }
        else {
            this.splice(j, 1);
        }
    };
}