1

I am trying to create an array with some values, then assign a variable a value contained in one of the arrays. I want to then return the name of the array if true.

I have got as far as the following (http://jsfiddle.net/4DhqW/1/)

<script>
  var internal = 'WKF1';

  Daily = [WKF1,WKF2,WKF3,WKF3,WKF4,WKF5];
  Weekly = [WKF6,WKF7,WKF8];
  Monthly = [WKF9,WKF10];

    function frequency(wkf, array) {
    if (wkf in //arrays daily weekly monthly ) {
    return //array name
        }
    }
document.write(frequency(internal)); //this should return the name of the array
</script>

Can someone please guide me, thanks in advance.

David Garcia
  • 3,056
  • 18
  • 55
  • 90
  • So you want it to be like: "if Daily contains wkf, name = Daily"? Maybe [this](http://stackoverflow.com/questions/237104/array-containsobj-in-javascript) would help, helps with javascript equivalents of `contains`. – MikeM Jul 18 '14 at 12:54
  • You need to quote the values in your arrays. – Andy Jul 18 '14 at 12:55
  • document.write(frequency(internal)); – David Garcia Jul 18 '14 at 12:57

2 Answers2

1

I think you should have your arrays as values of an object. That will allow you to get the name of the array (the key) easily. Make sure you quote your array values too.

var obj = {
  Daily: ['WKF1', 'WKF2', 'WKF3', 'WKF3', 'WKF4', 'WKF5'],
  Weekly: ['WKF6', 'WKF7', 'WKF8'],
  Monthly: ['WKF9', 'WKF10']
}

function frequency(wkf, obj) {
  for (var k in obj) {
    if (obj[k].indexOf(wkf) > -1) {
      return k;
    }
  }
  return 'None found';
}

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
  • So this is working fine on fiddle, however, on the platform that I am using it does not, it always return none found, the wkf value is being sent from another variable in another loop. `document.write("" + frequency(wID, obj) + "");` – David Garcia Jul 28 '14 at 18:14
  • Then your code is doing something wrong. What's the value of `wID`? Log it to the console to find out. – Andy Jul 28 '14 at 22:12
  • :) The value of wid contains one of the values from the object, ie WKF1. since is in a for each loop, i am printing out all the wIDs and their corresponding object property name. but it always print out none found. – David Garcia Jul 29 '14 at 09:20
  • How would you do a "for each" loop instead a "for" loop, when ever I try a for each loop i get "obj[k] is undefined" – David Garcia Jul 29 '14 at 11:45
  • Hi Andy, basically I have a loop that is fetching the values from the database and assigning them to wID. `for each ( var wkf in data.results ) { document.write("" + frequency(wID, obj) + ""); – David Garcia Jul 29 '14 at 12:00
  • That's not valid JS. No wonder you're getting problems. Also you're going to have big problems using `document.write` eventually. Update [this fiddle](http://jsfiddle.net/feZu4/) with whatever `data.results` is and I'll see if I can help. – Andy Jul 29 '14 at 12:02
  • Hi Andy, a friend helped me find the issue, the code is perfect the only problem was using the function in another loop, it was returning the last value always, so we had to create a local variable to store the values i.e. `var wkf= "" + wkf;` he did explain it was an issue in referencing or something i didn't get all the technicality. – David Garcia Jul 29 '14 at 16:00
1

You could inline all the arrays into your function and do this:

if (['WKF1','WKF2','WKF3','WKF3','WKF4','WKF5'].indexOf(wkf) != -1) {
    return 'Daily';
} else if (['WKF6','WKF7','WKF8'].indexOf(wkf) != -1) {
    return 'Weekly';
} else if (['WKF9', 'WKF10'].indexOf(wkf) != -1) {
    return 'Monthly';
}
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309