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..
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
}