打印对象的键和值 [英] Printing object's keys and values

查看:86
本文介绍了打印对象的键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从javascript对象打印一个键:值对。我可以在我的数组中使用不同的键,因此无法将其硬编码到对象[0] .key1

I want to print a key: value pair from javascript object. I can have different keys in my array so cannot hardcode it to object[0].key1

var filters = [{"user":"abc"},{"application":"xyz"}];
console.log(Object.keys(filters[0])[0]); // prints user
var term = (Object.keys(filters[0])[0]);
console.log(filters[0].term); // prints undefined

如何打印密钥的值

推荐答案

这是在过滤器[0]上寻找 term 属性

This is looking for a term property on filters[0]:

console.log(filters[0].term);

你真正想要做的是使用<$ c的 $ c> term (在您的示例中将是user)作为属性标识符:

What you actually want to do is use the value of term (in your example that will be "user") as the property identifier:

console.log(filters[0][term]);

这篇关于打印对象的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆