我有这个对象:
{"": "Select job type", F: "Full-time", P: "Part-time", C: "Contract", T: "Temporary}
我希望能够遍历它并获得密钥和值。我打算写一个看起来像
的函数getJobType(letter){
let jobType = JSON.parse(localStorage.getItem('jobType'))
//if my letter matches the key in the object then i want to return the value
}
我试过了:
Object.keys(JSON.parse(jobType)).forEach(key=> {
console.log(jobType[key]) ;
});
和
jobType.forEach( (value, key, index) => {
console.log("This is the value", value)
console.log("from the key", key)
})
但它不起作用。
答案 0 :(得分:0)
你缺少"在临时"结束时。下面的代码适用于迭代。您还可以使用过滤方法过滤掉值。
var jobTypes = {"": "Select job type", F: "Full-time", P: "Part-time", C: "Contract", T: "Temporary"};
Object.keys(jobTypes).forEach(function(key) {
console.log(jobTypes[key])
});