-3

Why I can't loop on 'data' ?

data = [{type: 'a', data: 'xyz'}, {type: 'a', data: 'xyz'}, {type: 'a', data: 'xyz'}];

for (i in data) {
  console.log('one line');
}

0 results, but data[0], data1, data[2] has data...

enter image description here

Slake
  • 2,080
  • 3
  • 25
  • 32
  • Did you use `i` anywhere else in the scope? try using a var in front of the i like this: `for (var i in data) {}` – Sirmyself Dec 01 '22 at 17:01
  • 3
    Check if you don't have a filter or you've filtered out info messages. – VLAZ Dec 01 '22 at 17:06
  • 1
    Most likely a duplicate of: [Console.log not working at all](https://stackoverflow.com/q/19662018) or [In Firefox, console.log is not showing anything](https://stackoverflow.com/q/15633404) – VLAZ Dec 01 '22 at 17:09
  • Thanks @VLAZ, shame on me . That drove me crazy, I did a rage post here to see what could go wrong, you fixed it. – Slake Dec 01 '22 at 23:10

2 Answers2

-1

You can definitely loop any kind of array in javascript I'll only provide one way to loop and I believe it is the most easier one for beginners:

const data = [{},{},{}]
data.map((object)=>console.log(object)) // object would be each object of your data array.
Moussa Bistami
  • 929
  • 5
  • 15
  • 4
    `map` is not what you want to use if you're not returning anything. Did you mean `forEach`? – General Grievance Dec 01 '22 at 17:04
  • 1
    Sorry, I don't see how it can be inferred from the question that the OP is a beginner, especially after the resolution achieved in the comments. – General Grievance Dec 01 '22 at 17:12
  • It is still a way to loop in javascript and it is still a valid way of looping. sorry but I don't understand why we'll keep talking about this, have a nice day! thanks. – Moussa Bistami Dec 01 '22 at 17:13
  • 2
    @MoussaBistami - it is a loop but its not the correct one. The goal of this service is to help people learn, and so it's important that the instruction align with _best practices_. There was no mention of returning values or altering arrays so `map` isn't the right tool for the job – Kinglish Dec 01 '22 at 17:18
-1

try using for of


for (i of data) {
 console.log(i);
}

output:

{ type: 'a', data: 'xyz' }
{ type: 'a', data: 'xyz' }
{ type: 'a', data: 'xyz' }