D
D
Dark192015-06-28 10:52:48
JavaScript
Dark19, 2015-06-28 10:52:48

Is it possible to loop through array elements inside function properties?

Good afternoon. Is it possible to loop through array elements inside function properties?
For example, there is a function for displaying points on the map ymaps.route to it, elements from the array are transferred to its properties. The first point and the last are static (they need to be displayed once per array traversal), but the intermediate points type: 'viaPoint' need to be output all from the array. That is, the result should be like this: start point el['pointCenter'][0] then output all points from the array C->0 then end point el['pointCenter'][0], el['pointCenter'][0] then print all points from array C->1 then end point el['pointCenter'][0], el['pointCenter'][0] then print all points from array D->0 then end point el['pointCenter' ][0] etc. Can this be done and how?
Here is an object that comes from php for processing:

{
 
"C":[{"path":["C","C3","C8","C2","C12","C"],"point":,"pointCenter":},{"path":["C","C11","C"],"point":,"pointCenter":}],
 
"D":[{"path":["D","D7","D5","D4","D14","D"],"point":,"pointCenter":},{"path":["D","D10","D"],"point":,"pointCenter":}],
 
"A":[{"path":["A","A0","A13","A1","A"],"point":,"pointCenter":}],
 
"B":[{"path":["B","B9","B6","B"],"point":,"pointCenter":}]
 
}


Here is his processing:

function fn(data)
{
Object.keys(data).forEach(function (key) {
var p = 0, x = 0;
data[key].forEach(function(el,i) {
console.log(el["point"]);
for(p=0;p<el['point'].length;p++){

ymaps.route([
el['pointCenter'][0],
{
type: 'viaPoint',
point: el['point'][p]
},
el['pointCenter'][0]
]).then(
function (route) {
myMap.geoObjects.add(route);
},
function (error) {
alert("Возникла ошибка: " + error.message);
}
);

}
});
});
}
fn(msg);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
forgotten, 2015-06-28
@forgotten

javascript.ru/book/definitiveguide chapter "Scope of functions and closures"

D
Dark19, 2015-06-28
@Dark19

and it is impossible to run the enumeration there cyclically, or run it in a function, and submit the function as a parameter to a property?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question