如何在javascript中对对象进行foreach? [英] How to make a foreach of an object in javascript?

查看:71
本文介绍了如何在javascript中对对象进行foreach?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用JavaScript和jQuery在Qlik Sense中构建混搭.我进行了一些数据选择,并将它们放在可变对象中:

I am currently building a mashup in Qlik Sense in JavaScript and jQuery. I have made some data selection and I have put them in a variable object :

var CurrentSelec = app1.selectionState().selections;
console.log(CurrentSelec);`
console.log(typeof CurrentSelec);

这是我在浏览器控制台上看到的:

This is what I get on my browser console :

我正在尝试在Foreach中显示qSelected值:

I'm trying to show the qSelected value in a Foreach :

我已经尝试过使用Javascript:

I have tried with Javascript :

for(var index in CurrentSelec) { 
    console.log(index.qSelected);
}

我已经尝试过使用jQuery:

I have tried with jQuery:

$.each(CurrentSelec, function(i, index) {
  console.log(index.qSelected)
});

但是我的浏览器控制台未显示我的 foreach 的日志.

But my browser console do not show the log of my foreach.

请问您知道如何正确地对对象进行 foreach 并将其内容显示在浏览器控制台上吗?

Do you know how to properly make a foreach of an object and show its content on a browser console, please ?

致谢.

推荐答案

您可以使用Object.keys()方法,该方法返回对象中键的数组.然后,我们通过Array.forEach()方法运行它.例如

You can use the Object.keys() method, which returns an array of the keys in an object. Then we run that through an Array.forEach() method. for example

Object.keys(lunch).forEach(function (item) {
    console.log(item); // key
    console.log(lunch[item]); // value
});

这篇关于如何在javascript中对对象进行foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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