javascript - 过滤掉同一键上的对象数组为空 ("")

标签 javascript arrays json

我有一个需要过滤的对象数组。它看起来像这样:

        let array = [
            {
                "id": "",
                "first_name": "Kary",
                "last_name": "Thorndale",
                "email": "kthorndale1@nifty.com",
                "gender": "Female",
                "ip_address": "172.152.36.109"
            },
            {
                "id": "",
                "first_name": "Westley",
                "last_name": "Emmott",
                "email": "wemmott2@cisco.com",
                "gender": "Male",
                "ip_address": "104.62.125.170"
            },
            {
                "id": "",
                "first_name": "Gavrielle",
                "last_name": "Danihel",
                "email": "gdanihel3@yandex.ru",
                "gender": "Female",
                "ip_address": "98.98.209.17"
            }
    ];

我只有一个条件 - 如果所有对象中的键为空,则将其从所有对象中移除。

如果速度更快或有任何需要,我可以使用 jQuery 或 loDash。

数组中的对象不应超过 15-20 个,并且最多将有 15 个这样的数组需要通过过滤器。

最佳答案

您可以计算相同键的空值并映射没有所有空属性的新对象。

var array = [{ id: "", first_name: "Kary", last_name: "Thorndale", email: "kthorndale1@nifty.com", gender: "Female", ip_address: "172.152.36.109" }, { id: "", first_name: "Westley", last_name: "Emmott", email: "wemmott2@cisco.com", gender: "Male", ip_address: "104.62.125.170" }, { id: "", first_name: "Gavrielle", last_name: "Danihel", email: "gdanihel3@yandex.ru", gender: "Female", ip_address: "98.98.209.17" }],
    keys = Array
        .from(array.reduce((m, o) => {
            Object.entries(o).forEach(([k, v]) => m.set(k, (m.get(k) || 0) + +!!v));
            return m;
        }, new Map))
        .filter(({ 1: v }) => v)
        .map(([k]) => k),
    result = array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

稍微好一点的版本带有 Set

var array = [{ id: "", first_name: "Kary", last_name: "Thorndale", email: "kthorndale1@nifty.com", gender: "Female", ip_address: "172.152.36.109" }, { id: "", first_name: "Westley", last_name: "Emmott", email: "wemmott2@cisco.com", gender: "Male", ip_address: "104.62.125.170" }, { id: "", first_name: "Gavrielle", last_name: "Danihel", email: "gdanihel3@yandex.ru", gender: "Female", ip_address: "98.98.209.17" }],
    keys = Array.from(
        array.reduce(
            (s, o) => Object.entries(o).reduce((t, [k, v]) => v ? t.add(k) : t, s),
            new Set
        )
    ),
    result = array.map(o => Object.assign(...keys.map(k => ({ [k]: o[k] }))));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 过滤掉同一键上的对象数组为空 (""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52704912/

相关文章:

javascript - 当您不知道 ID 时启用顺序选择菜单

Javascript:通过变量分配 bool 属性不起作用

javascript - 访问 parent 的属性(property)

javascript - 如何将函数作为方法添加到返回的数组?

php - 将关联数组转换为 php 中其值的简单数组

jquery - $.ajax get, mvc 3 jsonresult - 未定义结果

javascript - 使用 PHP 和 Javascript 的多次拍卖倒计时

javascript - 访问 jSON 文件中的子树

java - net.sf.json.JSONException异常

java - 具有空字段的 JSON 对象