zoukankan      html  css  js  c++  java
  • js对象数组去重

    <script>
    var array = [{
    greeting: "Hello",
    nickName: "Aziz"
    }, {
    greeting: "Hello",
    nickName: "Aziz"
    }, {
    greeting: "Hello",
    nickName: "test"
    }];
    let test = removeDuplicates(array, "nickName");
    console.log(test);
    function removeDuplicates(myArr, prop) {
    return myArr.filter((obj, pos, arr)=>{
    return arr.map(mapObj =>
    mapObj[prop]).indexOf(obj[prop]) === pos;
    });
    }

    var data = [{"name":"Lenovo Thinkpad 41A4298","website":"google"},
    {"name":"Lenovo Thinkpad 41A2222","website":"google"},
    {"name":"Lenovo Thinkpad 41Awww33","website":"yahoo"},
    {"name":"Lenovo Thinkpad 41A424448","website":"google"},
    {"name":"Lenovo Thinkpad 41A429rr8","website":"ebay"},
    {"name":"Lenovo Thinkpad 41A429ff8","website":"ebay"},
    {"name":"Lenovo Thinkpad 41A429ss8","website":"rediff"},
    {"name":"Lenovo Thinkpad 41A429sg8","website":"yahoo"}];

    var uniqueNames = [];
    for(i = 0; i< data.length; i++){
    if(uniqueNames.indexOf(data[i].website) === -1){
    uniqueNames.push(data[i].website);
    }
    }

    for(i = 0; i< uniqueNames.length; i++){
    alert(uniqueNames[i]);
    }

    </script>

  • 相关阅读:
    awt
    登录校验 简单实现
    事务隔离级别
    事务的四大特性(ACID)
    多线程简单了解
    Eureka bug
    什么是存储过程
    filter和servlet的区别
    说说你对多线程锁机制的理解
    session的生命周期,session何时创建,何时销毁,session销毁的方式
  • 原文地址:https://www.cnblogs.com/coce/p/8562882.html
Copyright © 2011-2022 走看看