数组对象指定对象字段排序
需求:根据valid值来排序 ,将valid为false的排到后面,效果如下图
sortByKey(array,key){
return array.sort(function(a,b){
var x = a[key];
var y = b[key];
return((x>y)?-1:((x<y)?1:0));
})
},
this.rankList = this.sortByKey(数组对象数据,'valid');
案例一
var arr = [
{"id":"1","name":"张三","value":"1245"},
{"id":"1","name":"张三","value":"1360"},
{"id":"2","name":"李四","value":"1120"},
{"id":"2","name":"李四","value":"1362"},
{"id":"2","name":"李四","value":"1008"},
{"id":"3","name":"王五","value":"1360"},
{"id":"4","name":"赵六","value":"1986"},
{"id":"4","name":"赵六","value":"1240"}
];
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,
value: ai.value
});
map[ai.id] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.id == ai.id){
dj.value=(parseFloat(dj.value) + parseFloat(ai.value)).toString();
break;
}
}
}
};
案例二
let resData = [
{
"name": "住院医疗最高报销",
"tagName": "医疗",
"insuredAmount": "6000"
},
{
"name": "身故赔付",
"tagName": "寿险",
"insuredAmount": "36.00"
},
{
"name": "烦死了免费陈述事实",
"tagName": "寿险",
"insuredAmount": "8888.00"
},
{
"name": "你好,噜噜噜",
"tagName": "寿险",
"insuredAmount": "66.00"
},
{
"name": "120种重大疾病确诊赔付",
"tagName": "重疾",
"insuredAmount": "350000"
}
]
解决一
let tempArr = [];
let Data = [];
for (let i = 0; i < resData.length; i++) {
if (tempArr.indexOf(resData[i].tagName) === -1) {
Data.push({
tagName: resData[i].tagName,
dataInfo: [resData[i]]
});
tempArr.push(resData[i].tagName);
} else {
for (let j = 0; j < Data.length; j++) {
if (Data[j].tagName == resData[i].tagName) {
Data[j].dataInfo.push(resData[i]);
break;
}
}
}
}
解决二
let dataInfo = {};
resData.forEach((item, index) => {
let { tagName } = item;
if (!dataInfo[tagName]) {
dataInfo[tagName] = {
tagName,
child: []
}
}
dataInfo[tagName].child.push(item);
});
let list = Object.values(dataInfo); // list 转换成功的数据
案例三
解决一
handlerDatas(arr){
// arr 传过来的原数组
let tempArr = [];
let endData = [];
for (let i = 0; i < arr.length; i++) {
if (tempArr.indexOf(arr[i].school) === -1) {
endData.push({
school: arr[i].school,
children: [arr[i]]
});
tempArr.push(arr[i].school);
} else {
for (let j = 0; j < endData.length; j++) {
if (endData[j].school == arr[i].school) {
endData[j].children.push(arr[i]);
break;
}
}
}
}
console.log(endData); // 最终输出
},
解决二
handlerDatas(arr){
let obj = {};
arr.forEach((item, index) => {
let { school } = item;
if (!obj[school]) {
obj[school] = {
school,
children: []
}
}
obj[school].children.push(item);
});
let data = Object.values(obj); // 最终输出
},
扩展延伸: 将数组对象改成对象数组
let arr = [
{ name: '小明',age: 20,school: 'qinghua',},
{ name: '小红',age: 21,school: 'qinghua',},
{ name: '小白',age: 18,school: 'beida',},
{ name: '小黄',age: 19,school: 'beida',},
{ name: '小浪',age: 21,school: 'hafo',},
]
handlerDatas(arr){
let obj = {};
arr.forEach((item, index) => {
let { school } = item;
if (!obj[school]) {
obj[school] = {
school,
children: []
}
}
obj[school].children.push(item);
});
let data = Object.values(obj);
let newData = {};
data.forEach((item, i) => {
let key = item.school;
let value = item.children;
newData[key] = value;
});
console.log(newData); // 最终输出
},