Can't fix my script to log the way I need it

后端 未结 2 959

So this is really getting on my nerves. I needed to explain this a bit better so here it is. I\'m trying to make my script log in google\'s console a specific way. I\'ve got

相关标签:
2条回答
  • 2020-12-22 07:43

    Your code was almost there, but what you were logging to the console was ridiculous, since it was just the points attribute, unmodified

    So, something like this

    const polylines = Array.from(xmlDoc.getElementsByTagName('polyline'));
    const Lines = polylines.map(pl => pl.getAttribute('points').split(' ').map(pair => pair.split(',').map(Number)));
    
    Lines.forEach(line => console.log(JSON.stringify(line)));
    

    Or, given that the second argument to Array.from is a map function, you can further simplify the code to

    const Lines = Array.from(xmlDoc.getElementsByTagName('polyline'), pl => pl.getAttribute('points').split(' ').map(pair => pair.split(',').map(Number)));
    
    Lines.forEach(line => console.log(JSON.stringify(line)));
    

    Note, the console.log is done AFTER the processing

    as for the comment

    Lines.forEach(line => {
        // here you can post each line to whatever you need
    });
    
    0 讨论(0)
  • 2020-12-22 07:54

    please try this, maybe this will work for you.

    var dt = "346 453 346 452 346 452 346 453 346 453 347 453 347 453 347 454 348 454 349 454 350 454 351 454 352 454 353 454 354 454 354 453 355 452 355 453";
    
    function fnGetObject(data) {
        var objList = [];
        var obList = data.split(' ');
    
        var obResult = []
        for (var i = 0; i < obList.length; i += 2) {
            obResult.push([parseInt(obList[i]), parseInt(obList[i + 1])]);
        }
        return JSON.stringify(obResult);
    }
    
    console.log(fnGetObject(dt));
    
    0 讨论(0)
提交回复
热议问题