4

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 :

CurrentSelec Object

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.

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
Clément Boyer
  • 41
  • 1
  • 1
  • 3
  • `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 Jul 07 '17 at 13:29

10 Answers10

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
});
Just
  • 437
  • 5
  • 15
3

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

CurrentSelec.forEach(function (el) {
    console.log(el.qSelected);
});
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

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]);
}
Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39
  • [Why is using "for...in" with array iteration a bad idea?](https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea) – Tyler Roper Jul 07 '17 at 13:38
0

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.

notphilphil
  • 385
  • 5
  • 16
0

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

try this

Durga
  • 15,263
  • 2
  • 28
  • 52
  • Consider adding some more substance to your answer. Just giving OP a code to substitute doesn't really explain much. – Tyler Roper Jul 07 '17 at 13:36
0

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.

bgauryy
  • 416
  • 3
  • 7
0

You can use jQuery as like this,

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

I am sure it helps,

Ritesh Khatri
  • 1,253
  • 13
  • 29
0

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) );
J.Doe
  • 143
  • 1
  • 1
  • 5
0

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