2015-12-15 46 views
1

內的特定對象我有這個js對象去搶另一個的對象

{ 
    "2015": { 
    "11": { 
     "10": { 
     "Family & Friends\nGames": { 
      "c": 2 
     }, 
     "Collectible\nGames": { 
      "c": 1 
     }, 
     "Logic Games\n": { 
      "c": 1 
     }, 
     "Kids Games\n": { 
      "c": 1 
     } 
     },  
    }, 
    "Family & Friends\nGames": { 
     "c": 9 
    }, 
    "Collectible\nGames": { 
     "c": 2 
    }, 
    "Logic Games\n": { 
     "c": 4 
    }, 
    "Kids Games\n": { 
     "c": 6 
    }, 
    "Classic Games\n": { 
     "c": 3 
    }, 
    "Preschool\nGames": { 
     "c": 5 
    } 
    }, 
    "meta": { 
    "id": [ 
     "[CLY]2", 
     "[CLY]7", 
     "[CLY]6", 
     "[CLY]1", 
     "[CLY]4", 
     "[CLY]3" 
    ], 
    "segments": [ 
     "id", 
     "title" 
    ], 
    "title": [ 
     "Family & Friends\nGames", 
     "Collectible\nGames", 
     "Logic Games\n", 
     "Kids Games\n", 
     "Classic Games\n", 
     "Preschool\nGames" 
    ] 
    } 
} 

我試着減去右上方的「元」,「標題」下匹配的密鑰是相同的對象。我完成了這個使用紅寶石用下面的代碼

 results = JSON.parse(resp.body) 
    data = results["2015"] 
    title = results["meta"]["title"] 
    alg = data.slice(*title).to_a 
    info = alg.sort_by { |k,v| v["c"] } 

所以我試圖將對象轉換成一個數組,並從那裏片中的所有匹配其他數組名冠軍和最後排序基於其「C信息的內容「價值。

這一切似乎都不適用於我,所以我試圖從內部使用[]和密鑰的名稱獲取數據的數據,但由於數組「標題」下的每個名稱都有\ n讓我有添加一個額外的\來訪問它。我想這是動態的,而不是有每次我有不同的名稱,時間修改爲「標題」下

我需要的是這樣的https://jsfiddle.net/7chntms2/6/

感謝您的幫助

+1

紅寶石或JavaScript? –

+0

我想要JS @NinaScholz – Andy

+0

你怎麼從'[「Classic Games \\ n」,{「c」=> 14}],'14? –

回答

0

例小提琴:https://jsfiddle.net/wv2kbcyn/

基本上,如果是JSON對象的名稱是數據:

var meta = data["meta"]; 
var titles = meta["title"]; 
var logicgames = titles[2]; 

您還可以循環標題:

for(var key in titles) 
{ 
    var title = titles[key]; 
    console.log(title); // "Family & Friends\nGames", ... 
} 

所以titles現在是一個數組。

+0

不是我真的想抓住匹配「title」中的名字的密鑰 – Andy

+0

@Andy在'for-loop'中使用'if'? – Piwwoli

+0

你能幫忙把obj轉換成數組嗎? – Andy

1

這是你想要實現的嗎?

var obj = { 
 
    "2015": { 
 
    "11": { 
 
     "10": { 
 
     "Family & Friends\nGames": { 
 
      "c": 2 
 
     }, 
 
     "Collectible\nGames": { 
 
      "c": 1 
 
     }, 
 
     "Logic Games\n": { 
 
      "c": 1 
 
     }, 
 
     "Kids Games\n": { 
 
      "c": 1 
 
     } 
 
     }, 
 
    }, 
 
    "Family & Friends\nGames": { 
 
     "c": 9 
 
    }, 
 
    "Collectible\nGames": { 
 
     "c": 2 
 
    }, 
 
    "Logic Games\n": { 
 
     "c": 4 
 
    }, 
 
    "Kids Games\n": { 
 
     "c": 6 
 
    }, 
 
    "Classic Games\n": { 
 
     "c": 3 
 
    }, 
 
    "Preschool\nGames": { 
 
     "c": 5 
 
    } 
 
    }, 
 
    "meta": { 
 
    "id": [ 
 
     "[CLY]2", 
 
     "[CLY]7", 
 
     "[CLY]6", 
 
     "[CLY]1", 
 
     "[CLY]4", 
 
     "[CLY]3" 
 
    ], 
 
    "segments": [ 
 
     "id", 
 
     "title" 
 
    ], 
 
    "title": [ 
 
     "Family & Friends\nGames", 
 
     "Collectible\nGames", 
 
     "Logic Games\n", 
 
     "Kids Games\n", 
 
     "Classic Games\n", 
 
     "Preschool\nGames" 
 
    ] 
 
    } 
 
} 
 

 
var keys = obj["meta"]["title"]; 
 
var newObj = []; 
 

 
for (var i = 0; i < keys.length; i++) { 
 
    newObj.push({ 
 
    name: keys[i], 
 
    content: obj["2015"][keys[i]] 
 
    }); 
 
} 
 

 
newObj.sort(function(a, b) { 
 
    if (a.content.c < b.content.c) return -1; 
 
    if (a.content.c > b.content.c) return 1; 
 
    return 0; 
 
}); 
 

 
console.log(newObj);

+0

問題是「c」值不再存在,也許這就是爲什麼當我嘗試運行代碼給我這個錯誤'Uncaught TypeError:Can not read property'c'undefined '@Rajesh – Andy

+0

你可以創建一個我們可以模擬的小提琴嗎?還發布你所得到的錯誤,並解釋*問題是,「C」值不再存在*? – Rajesh

+0

此錯誤表示c之前未定義的內容。就像你正在做'obj.key.c'一樣檢查是否存在'obj'和'obj.key'。還是小提琴會好很多,你會得到更好的答案。 – Rajesh

相關問題