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>

  • 相关阅读:
    继承关系·
    对象第复制operator=
    关于类拷贝造函数
    静态数据成员与静态成员函数
    linux新内核的时钟机制代码
    RTC系统
    Android关机闹钟实现
    更改printk打印级别
    vncserver 配置全屏显示
    vnc里鼠标拖动终端就会产生ctrl+c终端
  • 原文地址:https://www.cnblogs.com/coce/p/8562882.html
Copyright © 2011-2022 走看看