有时在我的项目中,JSON.Stringify
当我将值记录到控制台时,我正在使用an读取数据,有时我不需要这样做。。我想知道为什么吗?
在此示例中:
this._productServices.getAll().do(data => console.log(data)).subscribe(products=> this.articles= products);
当我查看控制台时,有如下所示的值:
(4) [{…}, {…}, {…}, {…}]
准确地说,有一个可读的值数组。
但是在这种情况下:
filteredSubProducts: Group[];
filterSubProductsByIdId(Id) {
this.filteredSubProducts= this.articles.filter(item => item.parentId == Id);
console.log("Products array" + this.filteredSubProducts);
}
我得到的结果为:
Products array[object Object],[object Object]
所以我需要用JSON.stringify()
几秒钟的时间来使我的值[object Object],[object Object]
可读。.我想知道为什么呢?有时我正在使用它,有时却没有。
谢谢
这是因为您正在将一个字符串"Products array"
与一个对象与.toString()
另一个字符串连接在一起。您在控制台中看到的是字符串。否则,将记录整个对象。尝试
console.log("Products array", this.filteredSubProducts);
编辑:仅删除toString()
并不能解决问题,因为之后的所有内容"string" + ...
都会首先转换为字符串。
// does not work
console.log("Products array" + this.filteredSubProducts);
这种行为称为类型强制,您可以在本文的SO答案,本文中或通过谷歌搜索更多信息来了解它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句