2017-10-15 76 views
0

我有以下結構。這是JSON.parse結果:JavaScript - 檢查json中的值

[{"model": "game.bowlinggame", "pk": 2587, "fields": {"Frame": 1, "FrameRow": 1, "Result": 10, "StrikeSpare": 2, "StrikeSpareInfo": "X", "Time": "2017-10-15T17:20:53.994Z", "GameId": 42, "StateOfGame": 1}}, {"model": "game.bowlinggame", "pk": 2588, "fields": {"Frame": 1, "FrameRow": 2, "Result": null, "StrikeSpare": null, "StrikeSpareInfo": null, "Time": "2017-10-15T17:20:53.996Z", "GameId": 42, "StateOfGame": 1}}] 

我想檢查的「StateOfGame」 == 2的值。我試過,但它是錯誤的:

 for (var i = 0; i < game_frame.length; i++) { 
      var obj = game_frame[i]; 
      for (var key in obj){ 
       console.log(key['fields']['StateOfGame']); 
       if (i['fields']['StateOfGame'] == 2){ 
        do_something; 
       } 
      } 
     } 

任何想法,我該怎麼辦這個?

+0

'obj.fields.StateOfGame == 2' – awd

回答

-1

你不需要內循環。

該數組的每個元素是一個具有屬性fields這與屬性的對象StateOfGame

for (var i = 0; i < game_frame.length; i++) { 
    if(game_frame[i].fields.StateOfGame === 2){ 
     do_something(); 
    } 
} 
0

可以使用陣列的filter方法和篩選特定值的對象,請發現下面的代碼。

注:我已經修改了第二個數組對象有StateOfGame屬性爲2

var array = [{ 
 
    "model": "game.bowlinggame", 
 
    "pk": 2587, 
 
    "fields": { 
 
    "Frame": 1, 
 
    "FrameRow": 1, 
 
    "Result": 10, 
 
    "StrikeSpare": 2, 
 
    "StrikeSpareInfo": "X", 
 
    "Time": "2017-10-15T17:20:53.994Z", 
 
    "GameId": 42, 
 
    "StateOfGame": 1 
 
    } 
 
}, { 
 
    "model": "game.bowlinggame", 
 
    "pk": 2588, 
 
    "fields": { 
 
    "Frame": 1, 
 
    "FrameRow": 2, 
 
    "Result": null, 
 
    "StrikeSpare": null, 
 
    "StrikeSpareInfo": null, 
 
    "Time": "2017-10-15T17:20:53.996Z", 
 
    "GameId": 42, 
 
    "StateOfGame": 2 
 
    } 
 
}]; 
 

 
var output = array.filter(function(x) { 
 
    return x.fields.StateOfGame === 2; 
 
}); 
 
console.log(output);
.as-console { 
 
    height: 100%; 
 
} 
 

 
.as-console-wrapper { 
 
    max-height: 100% !important; 
 
    top: 0; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

@sretko這是否回答幫助你嗎? –

0

在原來的JSON結構,沒有你的遊戲有一個StateOfGame 2.我調整了它並縮短了一下代碼:

const data = [{"model": "game.bowlinggame", "pk": 2587, "fields": {"Frame": 1, "FrameRow": 1, "Result": 10, "StrikeSpare": 2, "StrikeSpareInfo": "X", "Time": "2017-10-15T17:20:53.994Z", "GameId": 42, "StateOfGame": 1}}, {"model": "game.bowlinggame", "pk": 2588, "fields": {"Frame": 1, "FrameRow": 2, "Result": null, "StrikeSpare": null, "StrikeSpareInfo": null, "Time": "2017-10-15T17:20:53.996Z", "GameId": 42, "StateOfGame": 2}}]; 
 

 
const filtered = data.filter(item => { 
 
    return item.fields && item.fields.StateOfGame === 2; 
 
}); 
 
console.log(filtered);

0

雖然有時通過對象有用的循環,你一般不需要。

for (var i = 0; i < game_frame.length; i++) { 
    if(game_frame[i]['fields']['StateOfGame'] == 2){ 
     do_something; 
    } 
} 

您可以(以及允許的情況下,應該)通過它們的名稱訪問對象成員,而不是通過循環對象。

如果你絕對必須通過一個對象(有有些情況下是有用的,這個人是不是)循環,你需要做這樣的事情

for (var i = 0; i < game_frame.length; i++) { 
    for(var field in game_frame[i]['fields']){ 
     if(field == 'StateOfGame' && game_frame[i]['fields'][field] == 2){ 
     do_something; 
     } 
    } 
} 
1

,如果你想要做的事,而StateOfGame == 2 ,你可以做這樣的事情:

// using lodash each 
_.each(jsonData,data => { 
    if(data.fields.StateOfGame === 2){ 
    // do something 
    } 
}); 
// using javascript forEach 
jsonData.forEach(data => { 
    if(data.fields.StateOfGame === 2){ 
     //do something 
    } 
});