gpt4 book ai didi

javascript - 更改函数以接受多个参数

转载 作者:行者123 更新时间:2023-12-03 03:02:38 25 4
gpt4 key购买 nike

我需要修改下面的函数以接受多个参数而不是仅接受 4 个参数,我已经能够成功修改第一个函数 UseWicketToGoFromGooglePolysToWKT,但是我无法修改 UseJstsToDissolveGeometries 因为我不确定如何循环遍历参数并分配稍后在函数中使用的变量名称,并且我还收到错误 Uncaught TypeError: Cannot read property 'union' of undefined

如何修改 UseJstsToDissolveGeometries 以接受多个参数?

function UseWicketToGoFromGooglePolysToWKT(poly1,poly2,poly3,poly4) {
var wicket = new Wkt.Wkt();

wicket.fromObject(poly1);
var wkt1 = wicket.write();

wicket.fromObject(poly2);
var wkt2 = wicket.write();

wicket.fromObject(poly3);
var wkt3 = wicket.write();

wicket.fromObject(poly4);
var wkt4 = wicket.write();

return [wkt1, wkt2, wkt3, wkt4];
}

function UseJstsToDissolveGeometries(wkt1, wkt2, wkt3, wkt4) {
// Instantiate JSTS WKTReader and get two JSTS geometry objects
var wktReader = new jsts.io.WKTReader();
var geom1 = wktReader.read(wkt1);
var geom2 = wktReader.read(wkt2);
var geom3 = wktReader.read(wkt3);
var geom4 = wktReader.read(wkt4);

// In JSTS, "union" is synonymous with "dissolve"

var dissolvedGeometry;
dissolvedGeometry = geom2.union(geom1);
dissolvedGeometry = dissolvedGeometry.union(geom3);
dissolvedGeometry = dissolvedGeometry.union(geom4);

// Instantiate JSTS WKTWriter and get new geometry's WKT

var wktWriter = new jsts.io.WKTWriter();
var wkt = wktWriter.write(dissolvedGeometry);

// Use Wicket to ingest the new geometry's WKT

var wicket = new Wkt.Wkt();
wicket.read(wkt);
}

function DoEverything() {
var wkt = UseWicketToGoFromGooglePolysToWKT(polygon6, polygon7,polygon8, polygon9);
UseJstsToDissolveGeometries(wkt[0], wkt[1], wkt[2], wkt[3]);
}

我的尝试

function UseWicketToGoFromGooglePolysToWKT(...args) {
var wicket = new Wkt.Wkt();
return args.map(item => {
wicket.fromObject(item);
return wicket.write();
})
}

function UseJstsToDissolveGeometries(...args) {
var wktReader = new jsts.io.WKTReader();
args.map(item => {
return wktReader.read(item);
})

var dissolvedGeometry;
args.map(item => {
return dissolvedGeometry = dissolvedGeometry.union(item)
})

var wktWriter = new jsts.io.WKTWriter();
var wkt = wktWriter.write(dissolvedGeometry);

// Use Wicket to ingest the new geometry's WKT
var wicket = new Wkt.Wkt();
wicket.read(wkt);
}

function DoEverything() {
var wkt = UseWicketToGoFromGooglePolysToWKT(polygon6, polygon7,polygon8, polygon9);
UseJstsToDissolveGeometries(wkt[0], wkt[1], wkt[2], wkt[3]);
}

最佳答案

我相信您正在寻找这样的东西,问题是您正在使用dissolvedGeometry 在定义之前

function UseJstsToDissolveGeometries(...args) {
var wktReader = new jsts.io.WKTReader();
var geoms = args.map(item => {
return wktReader.read(item);
})

var dissolvedGeometry = geoms.reduce((accumulator , item, currentIndex, array) => {
if (currentIndex == 0) {
return array[1].union(item);
}

if(currentIndex == 1){
return accumulator;
}

return accumulator.union(item)
})

var wktWriter = new jsts.io.WKTWriter();
var wkt = wktWriter.write(dissolvedGeometry);

// Use Wicket to ingest the new geometry's WKT
var wicket = new Wkt.Wkt();
wicket.read(wkt);
}

此外,您还需要 .map 的结果。另外,由于您只想要溶解几何中的一个结果,我认为 .reduce 会更好

P.S 因为我实际上不知道这些对象会产生什么、它们有什么方法等,所以这段代码可能无法工作,但会给你一些关于要做什么的想法让它发挥作用

关于javascript - 更改函数以接受多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47304664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com