/* 键值对处理 */
let obj = {
"a": "11",
"b": "12",
"c": "13",
"d": "14",
"e": "15"
}
/* 获取全部的key */
let keys = Object.keys(obj)
console.log(keys)
* 打印的keys */
['a', 'b', 'c', 'd', 'e']
/* 获取全部的values */
let values = Object.values(obj)
console.log(values)
/* 打印的values */
['11', '12', '13', '14', '15']
根据key值获取value
/* 根据key 获取value 两种方法*/
let str = obj['a']
let str2 = eval("obj." + "a")
console.log(str, str2)
/* 打印的值 */
11 11
根据value获取key
let obj = {
"a": "11",
"b": "12",
"c": "13",
"d": "14",
"e": "15"
}
/* 通过value寻找key */
/* 封装一个findKey函数 */
findKey (obj,value, compare = (a, b) => a === b) {
Object.keys(obj).find(k => {
compare(obj[k], value)
})
return Object.keys(obj).find(k => compare(obj[k], value))
},
/* 调用findKey函数 */
var key = this.findKey(obj, '11');
console.log(key)
/* 打印的key值 */
a
组装成对象
/* 键值对处理 */
let obj = {
"a": "11",
"b": "12",
"c": "13",
"d": "14",
"e": "15"
}
/* 获取全部的key */
let keys = Object.keys(obj)
console.log(keys)
* 打印的keys */
['a', 'b', 'c', 'd', 'e']
/* 获取全部的values */
let values = Object.values(obj)
console.log(values)
/* 打印的values */
['11', '12', '13', '14', '15']
let arr = []
keys.forEach( (item, index) => {
let params = {
label: item,
value: values[index]
}
arr.push(params)
})
打印结果