js配列分類
1237 ワード
元のデータタイプ:let test=[
{id:「7」、city:「全部」、name:「おかゆ」}
{id:“6”、city:“全部”、name:“ご飯”}
{id:「1」、city:「重慶」、name:「鍋」}
{id:「2」、city:「江蘇」、name:「肥大粉」}
{id:「3」、city:「重慶」、name:「小面」}
{id:「4」、city:「北京」、name:「北京ダック」}
{id:「5」、city:「北京」、name:「酸味辛味粉」}
」
変換を期待:
var corect=[{ city:“全部”,info:{id:'6',name:''ご飯',{id:'7',name:''お粥'',},{city::"重慶",info:{id:'1',name:''鍋',{id:',name:',name:',name:'',name:''',name:''',name:'',name:''',name:'''',北京北京北京北京京''''',ddy'''',5'''''''''''',name','',name',''','''''',''''',name,''''',ティ:「江蘇」,info:{{id:''2',name:'肥大肠粉'}
」
方法:
{id:「7」、city:「全部」、name:「おかゆ」}
{id:“6”、city:“全部”、name:“ご飯”}
{id:「1」、city:「重慶」、name:「鍋」}
{id:「2」、city:「江蘇」、name:「肥大粉」}
{id:「3」、city:「重慶」、name:「小面」}
{id:「4」、city:「北京」、name:「北京ダック」}
{id:「5」、city:「北京」、name:「酸味辛味粉」}
」
変換を期待:
var corect=[{ city:“全部”,info:{id:'6',name:''ご飯',{id:'7',name:''お粥'',},{city::"重慶",info:{id:'1',name:''鍋',{id:',name:',name:',name:'',name:''',name:''',name:'',name:''',name:'''',北京北京北京北京京''''',ddy'''',5'''''''''''',name','',name',''','''''',''''',name,''''',ティ:「江蘇」,info:{{id:''2',name:'肥大肠粉'}
」
方法:
let list = test,
data = [];
for(var i = 0,length = test.length; i < length; i++) {
if(!data[list[i].city]) {
var arr = [];
arr.push(list[i]);
data[list[i].city] = arr;
}else {
data[list[i].city].push(list[i])
}
}