准备数据
data(){
return {
obj:{
'001':'a',
'002':'b',
'003':'c',
'004':'d'
}
}
},
遍历对象获取所有的key和value
function forEachValue(obj,fn){
Object.keys(obj).forEach(function (key) { return fn(obj[key], key); })
}
根据key获取对应的value
let _this = this
function findValue(key) {
for (let k in _this.obj){
if (k===key){
return _this.obj[k]
}
}
}
console.log(findValue('001'))
深层获取对象的key
let obj = {
a:{
b:{
c:{f:"aa"}
},
d:{
e:{g:"bb"},
h:{i:"cc"}
},
j:{
k:"dd"
}
}
};
let a = [];
function getKey(obj,isKey) {
if (typeof obj !== 'object' || obj == null) {
return
}
for (let key in obj){
typeof obj[key] === 'object' ? getKey(obj[key]) : '';
a.push(key)
}
根据value获取对应的key
function findkey(obj,value,fn = (a, b) => a === b){
return Object.keys(obj).find(k => fn(obj[k],value))
}
console.log(findkey(this.obj, 'a'));