1

i have:

var myVariable= {
    var_one: ["satu", 'uno'],
    var_two: ["dua", 'dos'],
    var_three: ["tiga", 'tres'],
    var_four: ["empat", 'cuatro'],
    var_five: ["lima", 'cinco'],
    var_six: ["enam", 'seis']
};

for (var component in myVariable) {
    document.getElementById(component[0]).value = '';
}

the problem here is: the component value is actually returning:

var_one ... var_six

while i'm expecting:

satu ... enam

how do i do for in loop to get my array value?

6 Answers 6

3

What you are doing is iterating over the keys in your object, not the values. So your code would look something like:

for (var key in myVariable) {
    var value_you_want = myVariable[key][0];
    document.getElementById(value_you_want).value = '';
}

FOLLOW UP:

You may also want to check to make sure the key is a property of the object and not the object's prototype in some cases:

for (var key in myVariable) {
    var value_you_want = myVariable[key][0];

    if(myVariable.hasOwnProperty(key)) {                  //Include this condition
        document.getElementById(value_you_want).value = '';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

component is the key not the value, please use this:

var myVariable= {
    var_one: ["satu", 'uno'],
    var_two: ["dua", 'dos'],
    var_three: ["tiga", 'tres'],
    var_four: ["empat", 'cuatro'],
    var_five: ["lima", 'cinco'],
    var_six: ["enam", 'seis']
};

for (var component in myVariable) {
    document.getElementById(myVariable[component][0]).value = '';
}

Comments

2

The for...in loop loops trough the indices in the array, not the values.

If you have the array

["one", "two", "three"]

Then a for...in loop will return

0, 1, 2

You need to use a for...of loop if you have ES6 or you need to do

var value = myArray[component]

To get the array from the object

Comments

2

To retrieve the value of a object key use object.key In your case it will return an array.If you need the element of zero index, do like myVariable[component][0]

var myVariable= {
    var_one: ["satu", 'uno'],
    var_two: ["dua", 'dos'],
    var_three: ["tiga", 'tres'],
    var_four: ["empat", 'cuatro'],
    var_five: ["lima", 'cinco'],
    var_six: ["enam", 'seis']
};

for (var component in myVariable) {
    document.getElementById(myVariable[component][0]).value="";
}

Comments

1

Use the key component like this:

myVariable[component][0]

Comments

0

You can use the key from the for...in loop to get the values:

for (var component in myVariable) {
    document.getElementById(myVariable[component][0]).value = '';
}

Alternatively, if myVariable is iterable, you can use a for...of loop:

var myVariable= [["satu", 'uno'],
    ["dua", 'dos'],
    ["tiga", 'tres'],
    ["empat", 'cuatro'],
    ["lima", 'cinco'],
    ["enam", 'seis']
    ];

for (var component of myVariable) {
    console.log(component[0]);
}

3 Comments

this solution is interesting, but it giving me error: Uncaught TypeError: myVariable[Symbol.iterator] is not a function
You're right, it's because the variable in the original question was an object with key-value pairs. I've edited my answer to show how it would work with a for...of loop.
i can't do that, because i need the key value in the future :)

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.