JavaScript / TypeScript获取对象中的邻居/兄弟姐妹值

时间:2016-07-12 01:10:27

标签: typescript

如果wood,如何获取fruit?我不知道在哪个森林里寻找。

let oranges,cherry,deep,apples,cedar,shallow

let tree = 
  mountainForest: {
    fruit:oranges
    wood:cherry
    root:deep
  },
  rainyForest: {
    fruit:apples
    wood:cedar
    root:shallow
 }

这就是我的尝试:

var result = 'someval'
Object.keys(someObj).forEach((key) => {
    // can't figure out this part
})

1 个答案:

答案 0 :(得分:2)

  

当我只有子键的值

你走了:

let someObj: any = {
    'somekey': 'someval'
}

let whatYouHave = 'someval'
Object.keys(someObj).forEach((key) => {
    if (someObj[key] == whatYouHave){
        console.log('found Key!', key); // somekey
    }
})