I
I
IvanOne2016-02-16 16:31:17
JavaScript
IvanOne, 2016-02-16 16:31:17

js how to iterate over an array?

There is an array
ports = [];
I fill it with lines (using lodash):

_.each(data.ports_variables, function(val){
                ports.push(val.ifName);
})

But here's the problem
console.log(ports[0]);
->undefined

The various iterations just don't work, as if it's empty, although if you do
console.log(ports);
That, a list of values ​​is displayed, what could be the problem? How can you iterate over the entire array?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Makarov, 2016-02-16
@IvanOne

Perhaps you are iterating in a callback to some kind of asynchronous function? request type

S
Sergey Melnikov, 2016-02-16
@mlnkv

Everything is working fine, you are doing something wrong
https://jsfiddle.net/rxLomLLw/

T
Taras Labiak, 2016-02-16
@kissarat

Why is Array.prototype.forEach bad? Everyone forgot about the good old for ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question