【问题标题】:Removing empty object keys in an array of objects删除对象数组中的空对象键
【发布时间】:2015-12-24 17:43:36
【问题描述】:

我正在尝试创建一个可以汇总另一个数组的数组。我已经收到了一些非常好的想法here,它们都在发挥作用,但它们都产生了另一个我似乎无法解决的障碍。

基于@kooiinc 的answer,我当前的代码如下所示:

var grants = [
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
    { id: "p_2", location: "loc_2", type: "B", funds: "2000" },
    { id: "p_3", location: "loc_3", type: "C", funds:  "500" },
    { id: "p_2", location: "_ibid", type: "D", funds: "1000" },
    { id: "p_2", location: "_ibid", type: "E", funds: "3000" }
];
var projects = [];
grants.map(
function (v) {
    if (!(v.id in this)) {
        this[v.id] = v;
        projects.push(v);
    } else {
        var current = this[v.id];
        current.type = [v.type].concat(current.type);
        current.funds = [v.funds].concat(current.funds);
    }
}, {});

... 它给出了以下期望的结果(typefunds 加入子数组,其余部分保持不变):

[
    { "id": "p_1", "location": "loc_1", "type": "A", "funds": "5000" },
    { "id": "p_2", "location": "loc_2", "type": ["E","D","B"], "funds": ["3000","1000","2000"] },
    { "id": "p_3", "location": "loc_3", "type": "C", "funds": "500" }
]

但是,如果某些对象有一些未定义的键值,则结果将在数组中包含空值。比如像这样(看type数组):

[
    { "id": "p_1", "location": "loc_1", "type": "A", "funds": "5000" },
    { "id": "p_2", "location": "loc_2", "type": ["E",null,null], "funds": ["3000","1000","2000"] },
    { "id": "p_3", "location": "loc_3", "type": "C", "funds": "500" }
]

(这里有一个fiddle 有同样的东西。)

我试图找到一种快速删除这些的方法(例如herehere),但由于某种原因,通常的方法(递归删除所有未定义/空的键)似乎都不起作用,无论我将它们放在代码中的哪个位置。他们不会给出错误,他们只是不会删除任何东西。

是否可能已经以某种方式排除了映射中未定义的键?

更新:所以一些对象键不会有任何值,只有[null,null,null],而其他的会有一些但不是全部像["E",null,null]。想法是删除所有空项,如果没有任何内容,则也删除对象键。

【问题讨论】:

    标签: javascript arrays object key


    【解决方案1】:

    试试这个方法:

    grants.map(
     function (v) {
        if (!(v.id in this)) {
            // => initialize if empty
            v.type = v.type || [];
            v.funds = v.funds || [];
            this[v.id] = v;
            projects.push(v);
        } else {
            var current = this[v.id];
            current.type = v.type ? [v.type].concat(current.type) : current.type;
            current.funds = v.funds ? [v.funds].concat(current.funds) : current.funds;
        }
     }, {});
    

    现在空值不会显示在结果中。

    【讨论】:

    • 是的,我也想过这个,但这似乎并没有删除所有的空值,见这里:jsfiddle.net/eebgasw6/1
    • 我明白了,有一个非常简单的解决方案。查看编辑后的答案
    【解决方案2】:

    我认为您可以测试typefunds 属性的出现,并且只有在存在时才插入或更新元素。

    a.type && a.funds && ...
    

    var grants = [
            { id: "p_1", location: "loc_1", type: "A", funds: "5000" },
            { id: "p_2", location: "loc_2", funds: "2000" },
            { id: "p_3", location: "loc_3", type: "C", funds: "500" },
            { id: "p_2", location: "_ibid", funds: "1000" },
            { id: "p_2", location: "_ibid", type: "E", funds: "3000" }
        ],
        project = [];
    
    grants.forEach(function (a) {
        a.type && a.funds && !project.some(function (b, i) {
            if (a.id === b.id) {
                project[i].type.push(a.type);
                project[i].funds.push(a.funds);
                return true;
            }
        }) && project.push({ id: a.id, location: a.location, type: [a.type], funds: [a.funds] });
    });
    document.write('<pre>' + JSON.stringify(project, 0, 4) + '</pre>');

    【讨论】:

    • 这样的问题是我无法加入我想加入的人(例如typefunds)。
    猜你喜欢
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2016-11-26
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多