Object.defineProperty-设置对象的键
将[{“name”:“刘备”,phone:“17783453234”}]转化为[{“刘备”:“17783453234”}]
handleFamilyNumber () {
let obj = [];
this.family_number.forEach((item, index, arr) => {
let it = {};
Object.defineProperty(it, item.name.toString(), {
value: item.phone,
writable: true,
configurable: true,
enumerable: true
});
obj.push(it);
})
this.family_number = obj;
}
Object.keys-获取对象的键
将[{“刘备”:“17783453234”}]转化为[{“name”:“刘备”,phone:“17783453234”}]
resolveFamilyNumber (val) {
let that = this
this.family_number = [];
let param = JSON.parse(val);
if (Array.isArray(param)) {
param.forEach((item, index, arr) => {
if (this.$utils.isNotBlank(Object.keys(item))) {
Object.keys(item).forEach((it, idx) => {
const obj = {}
obj.name = it
obj.phone = item[it]
that.family_number.push(obj);
})
}
})
}
}