0

I am using this java script in my kettle spoon ETL workflow. But always only the if statement is working, else if is not working, whats wrong with this?

var type1 = '';
var type2 = '';
var category1 = [1, 2, 3, 4, 5, 6, 7, 8, 50, 53];
var category2 = [10, 11, 12, 13, 14, 15, 56];

if (sub_type_id in category1) {
type1 = 'type-res';}
else if (sub_type_id in category2) {
type1 = 'type-ren';}
else type1 = '';

sub_type_id looks like this..

enter image description here

2
  • What is sub_type_id ? Commented Sep 5, 2017 at 10:21
  • That will have values like 1,2,3,4.. Commented Sep 5, 2017 at 10:22

1 Answer 1

1

Because in javascript, the in operator used for array doesn't check values in array, it checks the indexes.

See:

var category1 = [1, 2, 3, 4, 5, 6, 7, 8, 50, 53];
var sub_type_id = 50;
console.log(sub_type_id in category); // you get false

To see this more clearly, if you do:

for (i in category1) {
  console.log(i);
}

What's printed is 0 ~ 9, which are the indexes, rather than the actual values.

You need to use indexOf on array.

Try:

if(category1.indexOf(sub_type_id) !== -1) {
  // do something
} else if (category2.indexOf(sub_type_id) !== -1) {
  // do something
} else {
  // do something
}
Sign up to request clarification or add additional context in comments.

6 Comments

Let me try this. Thanks.
It is not working. :( Now even if statement is not working. empty cells.
Can you give me some examples of sub_type_id that you were using.
Edited the question with sub_type_id
Hi, I created this fiddle snippet: jsfiddle.net/k5ahwzue. It looks printing correctly. Can you put some prints to check if each branch is hit correctly
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.