2016-08-26 59 views
1

我得到的角度的物體看起來像這樣:角 - 如何使角對象數組

quiz.js:129 m {$promise: Promise, $resolved: false} 
    439:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000059790188_large.jpg?itok=62PzCown" 
    679: "" 
    1379:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000000301688_medium.jpg?itok=poGpHb5c" 
    1529:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000011483027_large.jpg?itok=LJ3f0c-X" 
    2022:"https://www.mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/208458_thumbnail.jpg?itok=6AG_2XS3" 
    $promise: Promise 
    $resolved: true__proto__: Object 

,我需要做的陣列進行數據的,但不知道如何做這個。

+0

在您的數據上添加循環並逐個添加到一個數組中 –

+0

請您詳細說明一下嗎?我不確定上面給出的代碼是否是報告的異常或您想要轉換爲數組的所需對象。 –

+1

參考這個答案:http://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – PerfectPixel

回答

1

你有什麼試過?這是做這件事的一種方式,雖然你得到的錯誤似乎並不指出這是問題。

var arr = Object.keys(obj) 
    .map(function(key) { return obj[key] }); 
0

你可以lodash _.values(obj)這將創建此對象的值的數組。

Here是文檔,這裏是相同的fiddle

我希望這可以幫助,謝謝。

var obj = { 
    439:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000059790188_large.jpg?itok=62PzCown", 
    679: "", 
    1379:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000000301688_medium.jpg?itok=poGpHb5c", 
    1529:"https://mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/istock_000011483027_large.jpg?itok=LJ3f0c-X", 
    2022:"https://www.mysite.no/sites/default/files/styles/quiz_large/public/fields/question-image/208458_thumbnail.jpg?itok=6AG_2XS3" 
    } 
    console.log(_.values(obj));