this.getEachList:全部属性
this.getKeyList:要展示的属性
/** vue **/
this.getEachList = [{
addr: '地址',
age: '20',
idno: '证件号码',
name: '张三',
sex: '男',
note: '提示信息'}]
// 要展示的内容
this.getKeyList= [{
key: "姓名",
attr: "name"
}, {
key: "性别",
attr: "sex"
}];
this.infoList = [];//最后要获取的数组
for (var i = 0; i < this.getKeyList.length; i++) {
let key = this.getKeyList[i].key;
let attr = this.getKeyList[i].attr;
for (var j = 0; j < this.getEachList.length; j++) {
let map = {};
map["left"] = key;
map["right"] = this.getEachList[j][attr];
this.infoList.push(map);
}
}
let info = delObj(this.infoList);
// info = [{left:'姓名',right:'张三'},{left:'性别',right:'男'}]
// js 删除数组中重复的对象
function delObj(obj) {
var uniques = [];
var stringify = {};
for (var i = 0; i < obj.length; i++) {
var keys = Object.keys(obj[i]);
keys.sort(function(a, b) {
return (Number(a) - Number(b));
});
var str = '';
for (var j = 0; j < keys.length; j++) {
str += JSON.stringify(keys[j]);
str += JSON.stringify(obj[i][keys[j]]);
}
if (!stringify.hasOwnProperty(str)) {
uniques.push(obj[i]);
stringify[str] = true;
}
}
uniques = uniques;
return uniques;
}