简体   繁体   English

Javascript console.log返回c()而不是对象的值

[英]Javascript console.log returns c() instead of the values of the object

I'm trying to use console.log to get the value of the self.subcategories object after it is created in the subscribe function below. 我正在尝试使用console.log在以下订阅函数中创建self.subcategories对象后获取其值。 I always get c() in my console from the console.log(self.subcategories) below. 我总是从下面的console.log(self.subcategories)在控制台中获得c() I know that the data is there, as I can use it in a different piece of code. 我知道数据在那里,因为我可以在另一段代码中使用它。 I just want to be able to see it in console.log so I can get more info and know what to do with it. 我只希望能够在console.log中看到它,所以我可以获得更多信息并知道如何处理它。

function QuestionFilter(data, categories, getSubcategoriesByCategoryUrl, getQuestionsBySubcategoryUrl) {
  var self = this;
  self.categories = ko.observableArray(categories);
  self.subcategories = ko.observableArray([]);
  self.selectedCategory = ko.observable();

  self.selectedCategory.subscribe(function(category) {
    function search(nameKey, myArray){
      for (var i=0; i < myArray.length; i++) {
        if (myArray[i].parentCategory_id === nameKey) {
          self.subcategories.push(myArray[i]);
        }
      }
    }   
    search(category, categories);
    console.log(self.subcategories);
  });
};

The Knockout.js library is included and so there are a couple of references to it. 包含了Knockout.js库,因此有一些引用。 As I mentioned, everything works, I just want to be able to log the object to help me write more code. 如前所述,一切正常,我只想能够记录该对象以帮助我编写更多代码。 Any ideas? 有任何想法吗?

Have a look at the documentation (emphasis mine): 看一下文档 (重点是我的):

Reading information from an observableArray 从observableArray读取信息

Behind the scenes, an observableArray is actually an observable whose value is an array (plus, observableArray adds some additional features described below). 在后台, observableArray实际上是一个可观察值,其值是一个数组(另外, observableArray添加了下面描述的一些附加功能)。 So, you can get the underlying JavaScript array by invoking the observableArray as a function with no parameters, just like any other observable. 因此, 您可以像其他任何可观察对象一样, 通过将observableArray作为一个不带参数的函数来调用从而获得基础的JavaScript数组 Then you can read information from that underlying array. 然后,您可以从该基础数组中读取信息。 For example, 例如,

 alert('The length of the array is ' + myObservableArray().length); alert('The first element is ' + myObservableArray()[0]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM