2017-06-13 81 views
0

我有这样的对象:离子2,遍历对象

{"": "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

你缺少“在结束‘临时’,下面的代码工作的迭代。您还可以使用过滤器的方法来过滤出的值。

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]) 
});