0

I have this code:

  var deptDictionary={
           <?php foreach($dept as $cd){
            echo '"'.$cd->department_id.'":"'.$cd->department_name.'",';
            }
           ?>
  }; 

which outputed this:

'Object {1: "ACCOUNTING", 5: "HUMAN RESOURCES", 6: "DEVELOPERS", 15: "ENGINEERING", 23: "ASDASD", 26: "QWEQWE"} '

now, I want to find the index of ACCOUNTING by just inputting the data it points to.

[what's that part called anyway if index is the first part of assoc array?]

I have tried this:

console.log(deptDictionary["accounting"]);

but it returned undefined. Am I missing something? Is there any reading material that points to js assoc array?

EDIT: OK. I have reversed the key and the data, to fit my needs. now it looks like this:

  var deptDictionary={
           <?php foreach($dept as $cd){
            echo '"'.$cd->department_name.'":"'.$cd->department_id.'",';
            }
           ?>
  }; 

But I am still raising this question for future reference, if some other guy finds it important to find the key.

8
  • you have to go by key values: Try deptDictionary["1"] to get accounting. Commented Nov 18, 2013 at 3:30
  • you are returning a string instead of an object.. it is not a json formatted response it looks like you are returning a string value Commented Nov 18, 2013 at 3:30
  • no. the input I have is the word 'accounting' and I need to have its corresponding index value. Commented Nov 18, 2013 at 3:31
  • 1
    Your question is a duplicate of stackoverflow.com/a/4491284/1754902 Commented Nov 18, 2013 at 3:36
  • 1
    @FrancisXavierS.Antazo yeah bud Im afraid so, but you could do this jsfiddle.net/agconti/j5sX2. (reverse the way you structure your object) Commented Nov 18, 2013 at 3:39

3 Answers 3

2

Try this out: http://jsfiddle.net/agconti/j5sX2/; (code below)

// this doesnt work
var dict = {1: "ACCOUNTING", 5: "HUMAN RESOURCES", 6: "DEVELOPERS"};
alert(dict[1]);
alert(dict["ACCOUNTING"]);

//but this will
var dict = {"ACCOUNTING": 1, "HUMAN RESOURCES":5};
alert(dict[1]);
alert(dict["ACCOUNTING"]);
Sign up to request clarification or add additional context in comments.

Comments

0

Remember that its case-sensitive. If your output is like

'Object {1: "ACCOUNTING", 5: "HUMAN RESOURCES", 6: "DEVELOPERS", 15: "ENGINEERING", 23: "ASDASD", 26: "QWEQWE"} '

Then you should use

console.log(deptDictionary["ACCOUNTING"]);

Please verify what is your key? department_name or department_id in echo '"'.$cd->department_name.'":"'.$cd->department_id.'",';

Comments

0

It will be better if you make an array and convert that in a JSON using json_encode() and to access that JSON object statically then you can call the way you were trying,

Ex.deptDictionary["accounting"]

OR

you can also access that using
deptDictionary[Object.keys(deptDictionary)[0]]

Thanks

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.