将Json数组分组到Jquery对象

时间:2016-08-01 12:47:25

标签: jquery json

我有这样的JSON数组。

{"records":[{"ColorId":"3","ColorName":"Red","GenderId":"3","GenderName":"Female","SizeId":"1","SizeName":"N\/A"},{"ColorId":"2","ColorName":"Green","GenderId":"2","GenderName":"Male","SizeId":"1","SizeName":"N\/A"},{"ColorId":"3","ColorName":"Red","GenderId":"2","GenderName":"Male","SizeId":"1","SizeName":"N\/A"}]}

我需要将这些变量分成具有不同

的单独对象
  

颜色:ColorId,ColorName

     

性别:GenderId,Gen​​derName

     

大小:SizeId,SizeName

1 个答案:

答案 0 :(得分:1)

这只是loop的简单recods,并在自己的结构中创建数据。甚至不需要使用任何jQuery函数。

for( var i = 0, l = data.records.length; i < l; i++ ) {
    color.push({ColorId: data.records[i].ColorId, ColorName: data.records[i].ColorName});
    gender.push({GenderId: data.records[i].GenderId, GenderName: data.records[i].GenderName});
    size.push({SizeId: data.records[i].SizeId, SizeName: data.records[i].SizeName});
}

<强> Wokring example.