数据类型
let temp1 = [
{'tel':'100000'},
{'code': '121323'},
{'name': '王五'},
{'danju': '单据'},
{'wangwu': 'wangwu'},
{'gebilaowang': 'hahaha'}
]
let temp2 = [
{key: 'tel', label: 'xxx', value: ''},
{key: 'code', label: 'xxx', value: ''},
{key: 'name', label: 'xxxx', value: ''},
{key: 'danju', label: 'hhhhh', value: ''},
{key: 'wangwu', label: 'kjhdfkh', value: ''},
{key: 'gebilaowang', label: 'skdfkjdhsf', value: ''}
]
temp1 => temp2
const temp11 = []
// for(let i = 0; i < temp1.length;i++) {
// for(let key in temp1[i]) {
// temp11.push({"key": key, "value": temp1[i][key]})
// }
// }
temp1.map(item => {
for (let key in item) {
temp11.push({"key": key, "value": item[key]})
}
})
// 补全字段
// for(let i = 0; i < temp2.length;i++) {
// if(temp2[i].key === temp11[i].key) {
// temp2[i].value = temp11[i].value
// }
// }
temp2.map((item, index) => {
if(item.key === temp11[index].key) {
item.value = temp11[index].value
}
})
temp2 => temp1
const demo1 = {...builderJson(temp2)}
const demo2 = {...builderJson1(temp2)}
// [{key:xxx,value:yyy} ...] => {xxx:yyy, ...}
function builderJson(data) {
tempp = {}
data.map(item => {
tempp[item.key] = item.value
})
console.log("111:",tempp)
return tempp
}
// [{key:xxx,value:yyy} ...] => [{xxx:yyy},{hhh:iii}, ...]
function builderJson1(data) {
tempp = []
// for(let i = 0; i < data.length;i++) {
// tempp.push({[data[i].key]: data[i].value})
// }
data.map(item => {
tempp.push({[item.key]: item.value})
})
console.log("123:",tempp)
return tempp
}