0
var fruitTypes = [{type:"Banana", color:"Yellow"},
                 {type:"Orange", color: "Orange"},
                 {type:"Apple", color: "Red"}];

How would I print each object "type" all at once inside of this array, without using a loop or concatenating each individual index?

ironSteel
  • 41
  • 5
  • with a property accessor? – Nina Scholz Nov 04 '16 at 13:10
  • `fruitTypes[0].type`, `fruitTypes[1].type`, etc, or `fruitTypes[i].type` if `i` is a variable – Pointy Nov 04 '16 at 13:10
  • you want to print all `type` values? – gurvinder372 Nov 04 '16 at 13:10
  • fruitTypes.forEach(function(element) { console.log(element.type); }); – Gavin Nov 04 '16 at 13:10
  • It's not nested. It's an array of objects. – satya-j Nov 04 '16 at 13:11
  • Is there a way to print out all the object types without using a loop nor accessing the array via a number? – ironSteel Nov 04 '16 at 13:12
  • You want to have your cake and eat it too? If you're not looping over *all* objects, and you're also not picking a specific one by index… then what *do* you want? – deceze Nov 04 '16 at 13:17
  • I checked out that question, it is different. It involves accessing an object THEN an array. Not an array and then an object. – ironSteel Nov 04 '16 at 13:17
  • That doesn't matter. Read it. Understand it. It's about how to work with arrays and objects. Apply it to your particular object/array as necessary. – deceze Nov 04 '16 at 13:19
  • I edited the question to be more specific, I am trying to print an object inside of an array. In this instance, I'd like to print all the fruit types without using a loop. – ironSteel Nov 04 '16 at 13:24
  • Why the restriction on using a loop?! – deceze Nov 04 '16 at 13:24
  • Because I cannot use a loop for the challenge. I have to print all of the "type" objects inside of the array without a loop. Is this possible? – ironSteel Nov 04 '16 at 13:27
  • And they have to be printed all at the same time, I cannot access them individually via their separate index numbers. – ironSteel Nov 04 '16 at 13:28
  • @ gurvinder372 Yes all type values without a loop or fruitTypes[0]+fruitTypes[1] and so on. I need a simple console.log access that prints all the types without a loop or individual concatenation of indexes. – ironSteel Nov 04 '16 at 13:43
  • You're asking for the solution to a challenge on SO? Not much of a challenge, is it? – deceze Nov 04 '16 at 13:43
  • Well this isn't the challenge itself. The challenge is much more complex. But there is no mention of a loop therefore I am assuming it does not use a loop. But I need to know if this is even possible without using a loop, or if js simply does not allow for this. – ironSteel Nov 04 '16 at 13:51
  • If there's no specific mention of loops being disallowed, then you're probably overthinking this. You'll want to look at the Array object methods for all the various ways in which you can iterate array members without explicitly writing `for` (through it's still a loop under the hood, there's no magic way to do this otherwise): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Iteration_methods – deceze Nov 04 '16 at 13:53
  • Thanks that is in the right direction. If it cannot be done any other way, and I cannot write an explicit loop, then a prototype may be the answer. – ironSteel Nov 04 '16 at 14:00

0 Answers0