在对象的数组中删除空的对象键 [英] Removing empty object keys in an array of objects

查看:125
本文介绍了在对象的数组中删除空的对象键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个类的总结另一个数组的数组。我已经收到了一些非常好的想法<一个href=\"http://stackoverflow.com/questions/32796640/convert-multi-dimensional-array-into-single-array-javascript\">here,所有的人都工作,但他们产生的另一个障碍,我再次,似乎无法来解决。

I'm trying to create an array that sort of summarises another array. I've already received some really good ideas here, all of them are working, but all of them generate another obstacle that I, again, can't seem to solve.

根据@ kooiinc的答案,我目前的code是这样的:

Based on @kooiinc's answer, my current code looks like this:

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);
    }
}, {});

...其中给出了如下的期望的结果(键入基金连接成子阵,其余推不变):

... which gives the following desired result (type and funds joined into sub-arrays, the rest are pushed unchanged):

[
    { "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" }
]

然而,如果一些对象具有一些不确定的密钥值,其结果将在阵列的空值。例如像这样的(看键入数组):

[
    { "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" }
]

(这里有一个拨弄,提供同样的事情。)

(Here's a fiddle with the same thing.)

我试图找到后删除这些(如<一的快捷方式href=\"http://stackoverflow.com/questions/6722873/sorting-and-removing-keys-with-null-values\">here或<一个href=\"http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript\">here)但没有一个常用方法某种原因(递归移除未定义/ null的所有键)似乎工作,无论在哪里我把它们放在我的code的,他们不给错误的,他们只是不会删除任何东西。

I've tried to find a quick way of removing these afterwards (like here or here) but for some reason none of the usual methods (of recursively removing all keys that are undefined/null) seem to work, regardless of where I put them in my code. They don't give errors, they just won't remove anything.

难道也许可能已经排除在映射未定义的钥匙不知何故?

Is it perhaps possible to already exclude the undefined keys in the mapping somehow?

更新:因此,一些对象键将没有的任何的价值,只是 [NULL,NULL,NULL] 而其他人则有一些但不是所有像 [E,NULL,NULL] 。这样做是为了消除所有的空项,如果什么都不剩,然后删除对象键以及

Update: So some of the object keys won't have any values, just [null,null,null] whilst others will have a few but not all like ["E",null,null]. The idea is to remove all the null items and if there's nothing left then remove the object key as well.

推荐答案

尝试这种方式:

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;
    }
 }, {});

现在空值将在结果中显示出来。

Now empty values will not show up in the result.

这篇关于在对象的数组中删除空的对象键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆