Use variable from one forEach Method in another

前端 未结 1 369
情话喂你
情话喂你 2020-12-22 13:40

I am trying to use a value from inside one getJSON, forEach method in another. Is there any way for me to do that?

JavaScript



        
相关标签:
1条回答
  • 2020-12-22 14:08

    By the comment you wrote, I'd prefer do it like this (think that both will have same length):

    var first, second;
    $.when(
        $.getJSON(json1, result => first = result),
        $.getJSON(json2, result => second = result)
    ).then(function() {
        if (first && second) {
            let results = first.map((item, index) => item.x / second[index].v);
            // do whatever you want
        }
    });
    
    0 讨论(0)
提交回复
热议问题