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.

3
  • So you want it to be like: "if Daily contains wkf, name = Daily"? Maybe this would help, helps with javascript equivalents of contains. Commented Jul 18, 2014 at 12:54
  • You need to quote the values in your arrays. Commented Jul 18, 2014 at 12:55
  • document.write(frequency(internal)); Commented Jul 18, 2014 at 12:57

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

7 Comments

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("<td>" + frequency(wID, obj) + "</td>");
Then your code is doing something wrong. What's the value of wID? Log it to the console to find out.
:) 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.
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"
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("<td>" + frequency(wID, obj) + "</td>");
|
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';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.