2016-10-02 95 views
0

我试图从“InputObject”下面创建“OutputArray”。我已经创建了一个函数getdata()来转换这两个对象。 当我用chrome将这些对象写入控制台时,它们显示的方式稍有不同,屏幕截图如下。在javascript中为knockout创建一个对象数组observableArray([])

OuputObect:(这是我所希望输出)

{ 
      "id" : "1", 
      "name" : "Name the color", 
      "q" : "what is the color white in HTML", 
      "a" : "255 255 255", 
      "video" : "http://aws.asdfsadf.com/something.mkv", 
      "images" : { 
       "title" : "White Image", 
       "url" : "http://aws.asdfsdf.com/image.jpg" 
       } 
     }, 
     { 
      "id" : "2", 
      "name" : "Name the color", 
      "q" : "what is the color black in HTML", 
      "a" : "0 0 0", 
      "video" : "http://aws.asdfsadf.com/something.mkv", 
      "images" : { 
       "title" : "White Image", 
       "url" : "http://aws.asdfsdf.com/image.jpg" 
     } 
} 

OutputObject应该出现在这样的镀铬:的console.log(OutputObject)

Correct output

相反,它看起来像这样: console.log(getdata(InputObject))

Incorrect output

InputObject:(这是怎样的数据由FireDB提交)

{ 
     "1" : { 
      "name" : "Name the color", 
      "q" : "what is the color white in HTML", 
      "a" : "255 255 255", 
      "video" : "http://aws.asdfsadf.com/something.mkv", 
      "images" : { 
       "title" : "White Image", 
       "url" : "http://aws.asdfsdf.com/image.jpg" 
       } 
     }, 
     "2" : { 
      "name" : "Name the color", 
      "q" : "what is the color black in HTML", 
      "a" : "0 0 0", 
      "video" : "http://aws.asdfsadf.com/something.mkv", 
      "images" : { 
       "title" : "White Image", 
       "url" : "http://aws.asdfsdf.com/image.jpg" 
       } 
     } 

    } 

的GetData()函数...

function getdata(data){ 

    var array = []; 

    for (var key in data) { 

     var arrayObject = {}; 

     // skip loop if the property is from prototype 
     if (!data.hasOwnProperty(key)) continue; 

     var obj = jsondata.questions[key]; 
     for (var prop in obj) { 
      // skip loop if the property is from prototype 
      if(!obj.hasOwnProperty(prop)) continue; 
      arrayObject[prop] = obj[prop]; 
      } 

     arrayObject["id"] = key; 
     array.push(arrayObject) 
    } 
return array; 
} 

回答

0

事实证明,两个数组都相等,它们在chrome中的显示方式不同。

我的问题是,我打电话ko.observableArray([OutputObject])

(方括号)

相反的:

ko.observableArray(的GetData(OutputObject));