• 删除数组中name属性重复的对象,展示最后一个(删除数组中重复的对象)


    调用:

    let results = deleteObject(obj);
    console.log(results);

    方法:

    function deleteObject (obj) {
    var uniques = [];
    var stringify = {};

    for (var i = 0; i < obj.length; i++) {

    var str = JSON.stringify(obj[i]['name']);

    if (!stringify.hasOwnProperty(str)) {
    uniques.push(obj[i]);
    stringify[str] = true;
    }
    }
    uniques = uniques;
    return uniques;
    }

    es6写法:

    export const deleteObject = (obj) => {
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {

    var str = JSON.stringify(obj[i]['name']);

    if (!stringify.hasOwnProperty(str)) {
    uniques.push(obj[i]);
    stringify[str] = true;
    }
    }
    uniques = uniques;
    return uniques;
    };

    function deteleObject(obj) {
        var uniques = [];
        var stringify = {};
        for (var i = 0; i < obj.length; i++) {
            var keys = Object.keys(obj[i]);
            keys.sort(function(a, b) {
                return (Number(a) - Number(b));
            });
            var str = '';
            for (var j = 0; j < keys.length; j++) {
                str += JSON.stringify(keys[j]);
                str += JSON.stringify(obj[i][keys[j]]);
            }
            if (!stringify.hasOwnProperty(str)) {
                uniques.push(obj[i]);
                stringify[str] = true;
            }
        }
        uniques = uniques;
        return uniques;
    }
  • 相关阅读:
    C语言运算符
    C语言的scanf函数
    C语言的数据、常量和变量
    js 操作样式属性
    点击图片显示或隐藏密码案例
    js 操作元素
    DOM 获取元素
    js 字符串对象
    js 数组
    js 倒计时 案例
  • 原文地址:https://www.cnblogs.com/Ao-min/p/13744552.html
Copyright © 2020-2023  润新知