js对象数组 根据某个共同字段 分组
我希望的是将下面的对象数组:[{"id":"1001","name":"值1","value":"111"},{"id":"1001","name":"值1","valu...
我希望的是将下面的对象数组:
[
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
根据id转换成下面这种形式:
[
{
"id": "1001",
"name": "值1",
"data": [
{"id": "1001", "name": "值1", "value": "111"},
{ "id": "1001", "name": "值1", "value": "11111"}
]
},
{
"id": "1002",
"name": "值2",
"data": [
{ "id": "1002", "name": "值2", "value": "25462" },
{ "id": "1002", "name": "值2", "value": "23131"},
{"id": "1002", "name": "值2","value": "2315432" }
]
},
{
"id": "1003",
"name": "值3",
"data": [
{"id": "1003", "name": "值3", "value": "333333" }
]
}
]
js基础不好,请各路大神不吝赐教,求代码和思路!!
呃,因为小弟分不多,谅解~ 展开
[
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
根据id转换成下面这种形式:
[
{
"id": "1001",
"name": "值1",
"data": [
{"id": "1001", "name": "值1", "value": "111"},
{ "id": "1001", "name": "值1", "value": "11111"}
]
},
{
"id": "1002",
"name": "值2",
"data": [
{ "id": "1002", "name": "值2", "value": "25462" },
{ "id": "1002", "name": "值2", "value": "23131"},
{"id": "1002", "name": "值2","value": "2315432" }
]
},
{
"id": "1003",
"name": "值3",
"data": [
{"id": "1003", "name": "值3", "value": "333333" }
]
}
]
js基础不好,请各路大神不吝赐教,求代码和思路!!
呃,因为小弟分不多,谅解~ 展开
展开全部
1、打开任一浏览器,按F12,进入开发者模式,找到控制台。本文以chrome浏览器为例,打开浏览器之后,打开任一标签页,找到Console控制台。
2、在控制台中,就可以书写js代码了。首先在此处定义一个js对象。直接使用 var obj1 = {}; 定义即可。
3、在控制台中,给js对象赋值,因为js是弱类型的语言,上面定义的对象并不需要指定字段类型,所以,在定义之后,就可以直接给它赋值了。
4、相同的方式,再定义另外一个js对象。直接使用 var obj2 = {}; 定义即可 。
5、在控制台中,给obj2对象赋值,而且,使用与obj1不相同的字段结构。
6、在控制台中,定义一个数组,定义方式如下:var arr = new Array()。
7、在控制台中,使用arr.push(obj1),就可以将对象1存放到js数组中。arr.push(obj2),就可以将对象2存放到js数组中。注意,obj1、obj2两个对象的字段是不相同的,但是,也是可以存放到同一个js数组中的,因为js是弱类型的。最后,直接使用arr,就可以看到当前数组中的对象值。
展开全部
var arr = [
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
];
var map = {},
dest = [];
for(var i = 0; i < arr.length; i++){
var ai = arr[i];
if(!map[ai.id]){
dest.push({
id: ai.id,
name: ai.name,
data: [ai]
});
map[ai.id] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.id == ai.id){
dj.data.push(ai);
break;
}
}
}
}
console.log(dest);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var arr = [
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
var objArr = []; //定义一个空数组
var len = arr.length;
for (var i = 0;i < len; i++){
var Id = arr[i].id;
var Name = arr[i].name;
var Value = arr[i].value;
if(!objArr[Id]){ //objArr[Id]未定义或不存在
objArr[id] = {};
}
if(!objArr[Id][Name]){ //objArr[Id][Name]未定义或不存在
objArr[Id][Name] = {};
objArr[Id][Name].Value = [];
}
objArr[Id][Name].Value.push(Value);
}
console.log(objArr);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var oldArr=[
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
;
var newArr=new Array();
var hasSame=false;
for(var i=0;i<oldArr.length;i++)
{
if(newArr.length>0)
{
for(var j=0;j<newArr.length;j++)
{
if(oldArr[i].id==newArr[j].id)
{
newArr[j].data.push(oldArr[i]);
hasSame=true;
}
}
}
if(!hasSame) {
var json={};
json.id=oldArr[i].id;
json.name=oldArr[i].name;
json.data=new Array();
json.data.push(oldArr[i]);
newArr.push(json);
}
hasSame=false;
}
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
]
;
var newArr=new Array();
var hasSame=false;
for(var i=0;i<oldArr.length;i++)
{
if(newArr.length>0)
{
for(var j=0;j<newArr.length;j++)
{
if(oldArr[i].id==newArr[j].id)
{
newArr[j].data.push(oldArr[i]);
hasSame=true;
}
}
}
if(!hasSame) {
var json={};
json.id=oldArr[i].id;
json.name=oldArr[i].name;
json.data=new Array();
json.data.push(oldArr[i]);
newArr.push(json);
}
hasSame=false;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询