我有一个javscript数组变量,每个数组元素都有对象值
{"ID":10075,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":1}
和整个JSON.stringify值如下...
"[{"ID":10075,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":1},
{"ID":10076,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":14,"GROUPID":1},
{"ID":-1,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":0,"GROUPID":0},
{"ID":10077,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":2},
{"ID":-1,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":0,"GROUPID":0},
{"ID":10078,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":92,"GROUPID":3}]"
如何在不同数组中存储数组值,基于ID值为-1 我的输出应该是: -
具有2个记录的第一个数组,其id为: - 10075,10076
第二个数组有1个记录,其id为: - 10077
第三个数组有1个记录,其id为: - 10078
答案 0 :(得分:2)
看起来你想要GROUPID不等于0的那些
function arrayParse(value){
var result = [];
value.forEach(function (v) {
if (v.GROUPID) {
if(!result[v.GROUPID])
result[v.GROUPID] = [];
result[v.GROUPID].push(v.ID);
}
});
return result;
}
答案 1 :(得分:1)
您可以将项目分隔的项目与ID === -1
分组。此提议适用于实际组的结果集和组数组。
您可以使用Array#forEach
thisArg
var array = [{ ID: 10075, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 11, GROUPID: 1 }, { ID: 10076, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 14, GROUPID: 1 }, { ID: -1, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 0, GROUPID: 0 }, { ID: 10077, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 11, GROUPID: 2 }, { ID: -1, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 0, GROUPID: 0 }, { ID: 10078, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 92, GROUPID: 3 }],
grouped = [];
array.forEach(function (o) {
if (!this.group || o.ID === -1) {
this.group = [];
grouped.push(this.group);
}
if (o.ID !== -1) {
this.group.push(o);
}
}, { group: null });
console.log(grouped);

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

或Array#reduce
回调中的group
变量的封闭。
var array = [{ ID: 10075, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 11, GROUPID: 1 }, { ID: 10076, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 14, GROUPID: 1 }, { ID: -1, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 0, GROUPID: 0 }, { ID: 10077, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 11, GROUPID: 2 }, { ID: -1, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 0, GROUPID: 0 }, { ID: 10078, SPECIALIZATIONID: "17", PRESPCIALIZATIONID: 92, GROUPID: 3 }],
grouped = array.reduce(function (group) {
return function (r, o) {
if (!group || o.ID === -1) {
group = [];
r.push(group);
}
if (o.ID !== -1) {
group.push(o);
};
return r;
};
}(), []);
document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');
console.log(grouped);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
结果在两种情况下都是一个新数组,其中包含给定数据的分组项的数组。不包含ID === -1
的项目。
答案 2 :(得分:1)
您可以使用Array.prototype.filter()创建一个函数,该函数将返回按组ID过滤的数组。
var array = [{"ID":10075,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":1},
{"ID":10076,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":14,"GROUPID":1},
{"ID":-1,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":0,"GROUPID":0},
{"ID":10077,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":11,"GROUPID":2},
{"ID":-1,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":0,"GROUPID":0},
{"ID":10078,"SPECIALIZATIONID":"17","PRESPCIALIZATIONID":92,"GROUPID":3}];
function getGroup(groupId){
return array.filter(function(object){
return object.GROUPID === groupId;
});
}
var group0 = getGroup(0);
var group1 = getGroup(1);
var group2 = getGroup(2);
console.log(group2);
&#13;