JS中key-value存取

获取Key

在代码中,遇到需要单独提取对象的key值时
可使用 Object.keys(object) object是你需要操作的对象
Object.keys()会返回一个存储对象中所有key值的数组
获取当前对象每个key对应的值使用 object[key]

 

参考:https://www.cnblogs.com/zhichong/p/9930309.html

=================================================================

JS中key-value存取

我们来使用实际的代码测试看看结果

var mArr = ['a','b','c'];
var mObj = {};
mObj['key1'] = 1;
mObj['key2@'] = 2;
 
//遍历mObj
for(var pro in mObj){
    if(mObj.hasOwnProperty(pro)){
        console.log('key is ' + pro +' and value is' + mObj[pro]);
    }
}

console.log(Object.keys(mArr));//结果:(3) ["0", "1", "2"]
console.log(Object.keys(mObj));//结果:(2) ["key1", "key2@"]
console.log(mArr[1]);//结果:b
console.log(mObj['key2@']);//结果:2.

 

 

出处:https://blog.csdn.net/zh854663752/article/details/80839402

=================================================================

var peopleArray=[]
var peopleobj={jiangyx: "姜艳霞", yeluosen: "叶落森"}
for(let i in peopleobj){
debugger
var obj={
nickname:i,
realName:peopleobj[i]
}
peopleArray.push(obj)
}
console.log(peopleArray)

 

 

出处:https://www.cnblogs.com/web-chuanfa/p/10719052.html

posted on 2019-12-15 13:53  jack_Meng  阅读(5921)  评论(0编辑  收藏  举报

导航