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

只谈情不闲聊 提交于 2019-11-29 17:51:17

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
});

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!