1

So neophyte trying to understand early steps of Javascript. I'm attempting to learn about arrays of objects and how to display the object and its property. The example is to show the color of fruit:

var fruitColor = {'apples':'red', 'bananas':'yellow', 'grapes':'purple'};


//answer would be string and then the properties...
//"Color of fruit: apples - red, bananas - yellow, grapes - purple"
2

1 Answer 1

1

something like :

var fruitColor = {'apples':'red', 
                  'bananas':'yellow', 
                  'grapes':'purple'
                  };
var sentence="Color of fruit: ";
for(var fruit in fruitColor){
    sentence+= fruit + " - " + fruitColor[fruit] + ", ";
}
sentence=sentence.substring(0, sentence.length - 2); 
// remove the last ','

Look at the for...in loop

https://jsfiddle.net/n0z13ge6/

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

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.