qq_37583141
南烟i
采纳率13.3%
2018-07-20 03:53 阅读 6.9k

前端js中Map取值问题,如下:

10

我在前端js中写
var Map = JSON.stringify(layer.p1);
console.log(Map);
页面得到:
{"type":"line","x1":429,"y1":135,"x2":374,"y2":182,"x3":432,"y3":220,"x4":559,"y4":306,"x5":605,"y5":191,"x6":458,"y6":100}
我该如何从数组中获得x,y的最大值与最小值?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    jslang 天际的海浪 2018-07-20 05:25
    var map = {"type":"line","x1":429,"y1":135,"x2":374,"y2":182,"x3":432,"y3":220,"x4":559,"y4":306,"x5":605,"y5":191,"x6":458,"y6":100};
    var xmax = -Infinity;
    var ymax = -Infinity;
    var xmin = Infinity;
    var ymin = Infinity;
    for (var n in map) {
        if (n.charAt(0)=="x") {
            xmax = Math.max(map[n],xmax);
            xmin = Math.min(map[n],xmin);
        }
        if (n.charAt(0)=="y") {
            ymax = Math.max(map[n],ymax);
            ymin = Math.min(map[n],ymin);
        }
    }
    alert("xmax = "+xmax+", ymax = "+ymax+", xmin = "+xmin+", ymin = "+ymin);
    
    点赞 3 评论 复制链接分享
  • caozhy 从今以后生命中的每一秒都属于我爱的人 2018-07-20 04:34
     var x = { Map.x1, Map.x2, ... Map.x6 };
    var y = { Map.y1, Map.y2, ... Map.y6 };
    var max = x[0];
    for (var i = 1; i < 6; i++)
    if (max < x[i]) max = x[i];
    alert("x的最大值" + max);
    var max = y[0];
    for (var i = 1; i < 6; i++)
    if (max < y[i]) max = y[i];
    alert("y的最大值" + max);
    
    点赞 评论 复制链接分享
  • zhukewen123 zhukewen123 2018-07-20 05:00

    var maxx=0,maxy=0,minx=9999,miny=9999;
    for(var i = 1; i<=6;i++){
    var x=Map["x"+i];
    var y=Map["y"+i];
    if(x>maxx)
    maxx = x;
    else if(x minx = x;
    if(y>maxy)
    maxy = y;
    else if(y<miny)
    miny = y;
    }

    点赞 评论 复制链接分享
  • th1991 眯眯眼有杀气 2018-07-20 05:17

    let data = {"type":"line","x1":429,"y1":135,"x2":374,"y2":182,"x3":432,"y3":220,"x4":559,"y4":306,"x5":605,"y5":191,"x6":458,"y6":100}

    let xArray = []
    let yArray = []
    Object.keys(data).forEach(function(key){
    if(key.match(/^x\d+/)){
    xArray.push(data[key])
    }
    if(key.match(/^y\d+/)){
    yArray.push(data[key])
    }
    })
    let xMax = Math.max.apply(null,xArray)
    let yMax = Math.max.apply(null,yArray)
    let xMin = Math.min.apply(null,xArray)
    let yMin = Math.min.apply(null,yArray)

    点赞 评论 复制链接分享
  • orcmylove orcmylove 2018-07-20 06:43

    var map = {"type":"line","x1":429,"y1":135,"x2":374,"y2":182,"x3":432,"y3":220,"x4":559,"y4":306,"x5":605,"y5":191,"x6":458,"y6":100};

    var x_max = -999999; //X最大值
    var x_min = 9999999999999; //X最小值
    var y_max = -999999;//Y最大值
    var y_min = 9999999999999; //Y最小值
    for (var k in map) {
    if (k.substr(0, 1) == 'x') {
    if (x_max < map[k]) x_max = map[k];
    if (x_min > map[k]) x_min = map[k];
    }
    if (k.substr(0, 1) == 'y') {
    if (y_max < map[k]) y_max = map[k];
    if (y_min > map[k]) y_min = map[k];
    }
    }

    点赞 评论 复制链接分享
  • qq_27689675 mchens 2018-07-20 07:40
    
    var obj ={"type":"line","x1":429,"y1":135,"x2":374,"y2":182,"x3":432,"y3":220,"x4":559,"y4":306,"x5":605,"y5":191,"x6":458,"y6":100}
    
    let arrX = []
    let arrY = []
    for (var key in obj) {
        console.log(typeof(obj[key]))
        if(typeof(obj[key]) === 'number'){
            if(key.indexOf('x') > -1) {
                arrX.push(obj[key])
            }
            if(key.indexOf('y') > -1) {
                arrY.push(obj[key])
            }
        }
    
    }
    
    console.log(arrX, arrY)
    
    // es6
    console.log('x最大值:' + Math.max(...arrX), 'x最小值:' + Math.min(...arrX))
    console.log('y最大值:' + Math.max(...arrY), 'y最小值:' + Math.min(...arrY))
    
    //es5 请使用下面的方法
    // Math.max.apply(null,arrX)
    
    
    

    望采纳

    点赞 评论 复制链接分享

相关推荐