最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to make a foreach of an object in javascript? - Stack Overflow

programmeradmin2浏览0评论

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.

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.

Share Improve this question edited Jul 7, 2017 at 13:27 ibrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges asked Jul 7, 2017 at 13:24 Clément BoyerClément Boyer 411 gold badge1 silver badge3 bronze badges 1
  • CurrentSelec is not an object, it is an array. If it were an object, the first line of your image would say Object instead of Array(1), and the typeof CurrentSelect would output Object {} instead of object. – Tyler Roper Commented Jul 7, 2017 at 13:29
Add a comment  | 

10 Answers 10

Reset to default 9

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

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

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

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

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)
});
for (var index in CurrentSelec[0]) {
 console.log(index.qSelected);
}

try this

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.

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.

You can use jQuery as like this,

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

I am sure it helps,

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

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

发布评论

评论列表(0)

  1. 暂无评论