0
Array: ['Date Taken', 'Weight'],

I have this Array and the way I am iterating it is:

for (vb = 0, len = Array.length; vb < len; vb++) {
     alert(Array[vb]); // would give me the Array Values... 
}

Obj: {DateTaken: 'this.getPicker()', Weight: 'this.getWeight()'}
  1. How can I seperate the keys and the values when I am printing? I want the key and value to be alerted separately
  2. Am I constructing the object right way?
  3. Can a key have multiple values? I mean DateTaken:'value 1, value 2' or something like this.
1
  • Your question doesn't make any sense. You need to clarify what you have got now and what you want to achieve. Commented Jul 16, 2011 at 10:45

2 Answers 2

1

Okay, that's pseudo-Javascript! :)

Here's the real JS:

var myArray = ['Date Taken', 'Weight'];

for (var i = 0, len = myArray.length; i < len; i++)
{
     alert(myArray[i]); // would totally give you myArray Values! 
}

var myObj = { DateTaken: this.getPicker(), Weight: this.getWeight() };

To iterate both the key and value of myObj, you can do this:

for (var i in myObj)
{
   alert('the key is ' + i + ' and value is ' + myObj[i]);
}

The key in myObj can contain anything, so say if you want multiple strings in DateTaken, then it'll look like this:

var myObj = { DateTaken: ['aloha', 'mahalo', 'etc'], Weight: this.getWeight() };
Sign up to request clarification or add additional context in comments.

2 Comments

how to print the mutliple values in DateTaken and can i have only strings in it...
@John myObj.DateTaken will give you the array. Then you just iterate it like you would any array. Your array can contain whatever you want, strings, objects, arrays, functions, etc.
1

Somebody might have added custom keys to the Object prototype so generally it is a good practice to check if all the keys are the actual objects properties. To iterate over an objects keys, you would then:

var myObj = { DateTaken: this.getPicker(), Weight: this.getWeight() };
for (var key in myObj) {
  if (myObj.hasOwnProperty(key)) {
    alert("Key: " + key + ", value: " + myObj[key]);
  }
}

If your values can be arrays you can combine this approach with the one you already found yourself for arrays. I'm using a helper 'output' string here to compose the output.

var myObj = { single: "foo", multiple: ["one", "two", 3] };

for (var key in myObj) {
  if (myObj.hasOwnProperty(key)) {
    var output = "key: " + key + ", value = "; 
    // Check for Array
    if (typeof myObj[key] == "object" && myObj[key].length != undefined) {
      output += "[";
      for (var i = 0, len = myObj[key].length; i < len; ++i) {
        output += myObj[key][i] + ",";
      }
      output += "]";
    } else {
      output += myObj[key];
    } 
    alert(output);
  }
}

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.