我有一个来自服务器的JSON对象。
如果我跑
console.log(msg);
我从控制台得到了这个 screenshot
如何从Object获取密钥或所有密钥? 我试过了
console.log(msg.Object.key)
但抱怨
财产'对象'类型' {}'
上不存在
任何帮助将不胜感激
编辑:
listener.subscribe((msg:any) => {
console.log((msg.data[0]));
这是有效的,我有输出
对象{键:" IL_IMAGER",值:" 2.7872460627869"}
但我只想要密钥
我试过了:
console.log((msg.data[0].key));
但是
错误类型错误:无法读取属性'键'未定义的
解决了问题。 这是因为我在服务器中有两个对象,其中一个没有'值' 。
所以这将是答案:
console.log((msg.data[0].key));
答案 0 :(得分:0)
查看您的屏幕截图,您有这样的数据
var arr = [{"id":"X","price":"5","size":"5"},
{"id":"Y","price":"10","size":"5"},
{"id":"X","price":"5","size":"5"},
{"id":"Y","price":"10","size":"5"},
{"id":"Z","price":"5","size":"5"},
{"id":"X","price":"5","size":"5"},
{"id":"X","price":"5","size":"5"},
{"id":"Z","price":"5","size":"5"},
{"id":"Q","price":"11","size":"5"}];
var test = arr.map((data) => {
return data.id;
});
console.log(test);

在Angular中使用
let keys = yourArray.map((item) => {
return item.key;
});