<script>
// 这个数组按照 F_ItemClassNo 来进行分组
let list=[
{
"F_ItemGroupNo":"IG168",
"F_ItemGroupName":null,
"F_ItemClassNo":"IC0072",
"F_ItemClassName":"物理类",
"F_ItemNo":"TI221000250",
"F_ItemName":"DSC",
"F_MethodNo":"MS005866",
"F_MethodName":"GB/T 19466.1-2004",
},
{
"F_ItemGroupNo":"IG167",
"F_ItemGroupName":null,
"F_ItemClassNo":"IC0072",
"F_ItemClassName":"物理类",
"F_ItemNo":"TI221000242",
"F_ItemName":"长延时",
"F_MethodNo":"MS005858",
"F_MethodName":"/",
},
{
"F_ItemGroupNo":"IG170",
"F_ItemGroupName":null,
"F_ItemClassNo":"IC0074",
"F_ItemClassName":"化学类",
"F_ItemNo":"TI221000277",
"F_ItemName":"熔点",
"F_MethodNo":"MS005908",
"F_MethodName":"/",
},
{
"F_ItemGroupNo":"IG170",
"F_ItemGroupName":null,
"F_ItemClassNo":"IC0074",
"F_ItemClassName":"化学类",
"F_ItemNo":"TI221000280",
"F_ItemName":"电导率",
"F_MethodNo":"MS005911",
"F_MethodName":"GB/T 11007-2008",
},
{
"F_ItemGroupNo":"IG170",
"F_ItemGroupName":null,
"F_ItemClassNo":"IC0074",
"F_ItemClassName":"化学类",
"F_ItemNo":"TI221000283",
"F_ItemName":"含水率(水分)",
"F_MethodNo":"MS005914",
"F_MethodName":"GB/T 29249-2012",
}
]
// 这个数组按照 year 来进行分组
let arr=[
{
'name':'a1',
'id':1,
'year':2012
},
{
'name':'a2',
'id':2,
'year':2012
},
{
'name':'a3',
'id':3,
'year':2013
},
{
'name':'a4',
'id':4,
'year':2014
},
{
'name':'a5',
'id':5,
'year':2015
},
{
'name':'a6',
'id':6,
'year':2015
},
{
'name':'a7',
'id':7,
'year':2015
},
{
'name':'a8',
'id':8,
'year':2018
},
{
'name':'a9',
'id':9,
'year':2018
},
{
'name':'a10',
'id':10,
'year':2015
},
]
// 第一种方法
let obj ={}
list.forEach((item)=>{
if(!obj[item.F_ItemClassNo]){
obj[item.F_ItemClassNo]=[]
}
obj[item.F_ItemClassNo].push(item)
})
console.log('第一种方法的第一个数组==按照==F_ItemClassNo',obj)
let new_Two={}
arr.forEach((item)=>{
if(!new_Two[item.year]){
new_Two[item.year]=[]
}
new_Two[item.year].push(item)
})
console.log('第一种方法的第一个数组==按照==year',new_Two)
// 第二种方法
function dealWithData(data){
let c = [];
let d = {};
data.forEach(element => {
if(!d[element.F_ItemClassNo]){
c.push({
F_ItemClassNo: element.F_ItemClassNo,
allData: [element]
});
d[element.F_ItemClassNo] = element;
}else{
c.forEach(ele => {
if(ele.F_ItemClassNo == element.F_ItemClassNo){
ele.allData.push(element);
}
});
}
});
return c;
}
function dealWithData_Two(data){
let c = [];
let d = {};
data.forEach(element => {
if(!d[element.year]){
c.push({
year: element.year,
allData: [element]
});
d[element.year] = element;
}else{
c.forEach(ele => {
if(ele.year == element.year){
ele.allData.push(element);
}
});
}
});
return c;
}
console.log('第二种方法的第一个数组==按照==F_ItemClassNo',dealWithData(list))
console.log('第二种方法的第一个数组==按照==year',dealWithData_Two(arr))
</script>
数组按照某一个属性进行分类
最新推荐文章于 2023-05-04 16:23:26 发布