How to make a foreach of an object in javascript?

心已入冬 提交于 2021-02-18 19:01:06

问题


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 :

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

I have tried with Javascript :

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

I have tried with jQuery:

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

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

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

Regards.


回答1:


Your CurrentSelec variable is an array, not an ordinary object, so just use its forEach method:

CurrentSelec.forEach(function (el) {
    console.log(el.qSelected);
});



回答2:


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
});



回答3:


You iterate object using javascript for in loop.

For e.g. CurrentSelec is your object.

for(key in CurrentSelec){
    //key is your object's main query.
    //CurrentSelec is your object's nested array if has.
    console.log(key);
    console.log(CurrentSelec[key]);
}



回答4:


Try using below code and see if it works, instead of CurrentSelec use CurrentSelec[0]

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



回答5:


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

try this




回答6:


Some info is missing here (and please rename to 'currentSelec) , but do:

CurrentSelec.forEach(function(item){
         console.log(item.qSelected)
    })

What you did is iteration on the keys of the Array, and not the values.




回答7:


With your JavaScript example, you are incorrectly navigating through the Object. In order to move through it, use [] and the variable you set. For example,

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

This chooses each child inside of CurrentSelec and prints out its corresponding qSelected value.

In your example, when you say for (var index in CurrentSelec) {...};, you are saying for each child inside of CurrentSelec, set index = child (where child is only a key to your CurrentSelec Object), and then proceed with the for loop.

Therefore, when you say console.log(index.qSelected), you are trying to navigate to the qSelected value under index, which will never exist. Instead you should navigate through the parent Object using each index you receive in your for loop.




回答8:


You can use jQuery as like this,

$.each(CurrentSelec, function( key, value ) {
   alert( key + ": " + value );
});

I am sure it helps,




回答9:


It seems like you mixed up Arrays and Objects. Looking at the picture you provided I think you have an Array which contains Objects.

//Naming indicates mutliple selections
app1.selectionState().selections; 

Looping over an array is easy, accessing a object value too:

// CurrentSelec = [{qSelected : '1'},{qSelected : '2'}];
CurrentSelec.forEach ( selection => console.log(selection.qSelected) );



回答10:


var displayArray = [{id:1,dispName:"abc"},{id:2,dispName:"aaaa"},{id:3,dispName:"bbbb"},{id:4,dispName:"xxxxx"},{id:5,dispName:"www"}]
      displayArray.forEach(obj => {
      console.log(obj)
      });


来源:https://stackoverflow.com/questions/44971934/how-to-make-a-foreach-of-an-object-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!