迭代json数组以仅获取一次jquery值

迭代json数组以仅获取一次jquery值,jquery,json,Jquery,Json,这是我的示例json对象(它可以有这么多值,但格式是这样的)我如何迭代此示例以每个id只获取一次nature的值我想有一个计数器,其中我想计算有多少nature具有值1和值0(自然的唯一价值是1和0)对于每个id。id是唯一的识别主体 我想要的是能够计算出每个id示例的性质 ID:2015-07-002 自然0:0 自然1:1这就是你要找的 代码 [{ "id": "2015-07-002", "class": "7", "nature": "1", "reso

这是我的示例json对象(它可以有这么多值,但格式是这样的)我如何迭代此示例以每个id只获取一次nature的值我想有一个计数器,其中我想计算有多少nature具有
值1
值0
(自然的唯一价值是
1
0
)对于每个
id
id
是唯一的识别主体

我想要的是能够计算出每个id示例的性质

ID:2015-07-002
自然0:0

自然1:1

这就是你要找的

代码

[{
    "id": "2015-07-002",
    "class": "7",
    "nature": "1",
    "resolution": "res1",
    "mstatus": "1",
    "cstatus": null,
    "astatus": null
}, {
    "id": "2015-07-002",
    "class": "7",
    "nature": "1",
    "resolution": "res1",
    "mstatus": "2",
    "cstatus": null,
    "astatus": null
}]

这就是你要找的

代码

[{
    "id": "2015-07-002",
    "class": "7",
    "nature": "1",
    "resolution": "res1",
    "mstatus": "1",
    "cstatus": null,
    "astatus": null
}, {
    "id": "2015-07-002",
    "class": "7",
    "nature": "1",
    "resolution": "res1",
    "mstatus": "2",
    "cstatus": null,
    "astatus": null
}]

创建一个键为id的对象,该值是一个带有0和1计数器的对象

另外,
$(数据)。each()
用于迭代jQuery元素的集合。要迭代数组,请使用
$。each()


创建一个键为id的对象,该值是一个带有0和1计数器的对象

另外,
$(数据)。each()
用于迭代jQuery元素的集合。要迭代数组,请使用
$。each()


在数组中添加id,然后在迭代数组时检查该数组

var counters = {}
$.each(data, function() {
    var id = this.id;
    if (!counters[id]) {
        counters[id] = {"0" : 0, "1", 0};
    }
    counters[id][this.nature]++;
}
for (id in counters) {
    console.log(id);
    console.log('  Nature0 ' + counters[id]['0']);
    console.log('  Nature1 ' + counters[id]['1']);
}

在数组中添加id,然后在迭代数组时检查该数组

var counters = {}
$.each(data, function() {
    var id = this.id;
    if (!counters[id]) {
        counters[id] = {"0" : 0, "1", 0};
    }
    counters[id][this.nature]++;
}
for (id in counters) {
    console.log(id);
    console.log('  Nature0 ' + counters[id]['0']);
    console.log('  Nature1 ' + counters[id]['1']);
}

你试过什么了吗?等等,有点长了,我现在在拉小提琴好的,没问题,我可以帮你,但我不想给你做作业:)我现在在工作,你试过什么了吗?等等,有点长了,我现在在拉小提琴好的,没问题,我可以帮你,但我不想给你做作业:)我现在在工作