IONIC 2,循环通过对象

时间:2017-06-13 02:27:27

标签: angular cordova ionic2

我有这个对象:

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

但它不起作用。

1 个答案:

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