es6的 for(var of)语法怎么适应es5

Reset
  • 24
 var json ={"SnapPolygonList":{"SnapPolygon":[{"id":"1","polygonType":"1","color":{"r":"0","g":"255","b":"0"},"tips":"#1#","isClosed":"true","pointList":{"point":[{"x":"0.324415","y":"0.496516"},{"x":"0.538462","y":"0.486063"},{"x":"0.438127","y":"0.806620"},{"x":"0.269231","y":"0.759582"}]}},{"id":"2","polygonType":"1","color":{"r":"255","g":"0","b":"0"},"tips":"#2#","isClosed":"true","pointList":{"point":[{"x":"0.168896","y":"0.728223"},{"x":"0.650502","y":"0.780488"},{"x":"0.493311","y":"0.945993"},{"x":"0.143813","y":"0.970383"},{"x":"0.157191","y":"0.949477"}]}}]}};

    var eventAttributes = {
        '布防区域': [],
        '撤防区域': [],
        '开始检测时间': '00:00:00'
    };

    for (var snap of  json.SnapPolygonList.SnapPolygon) {
        var pointList = [];
        for (var point of snap.pointList.point) {
            pointList.push([point.x, point.y]);
        }
        if (snap.color.r === '0') {
            eventAttributes['布防区域'].push({
                '区域坐标': pointList,
                '区域编号': snap.id
            });
        }
        if (snap.color.r === '255') {
            eventAttributes['撤防区域'].push({
                '撤防区域': pointList,
                '区域编号': snap.id
            });
        }
    };

    console.log(eventAttributes);
回复
阅读 2.6k
2 个回答

babel 它不香吗?

ref:
https://babeljs.io/docs/en/

"use strict";

function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

var json = {
  "SnapPolygonList": {
    "SnapPolygon": [{
      "id": "1",
      "polygonType": "1",
      "color": {
        "r": "0",
        "g": "255",
        "b": "0"
      },
      "tips": "#1#",
      "isClosed": "true",
      "pointList": {
        "point": [{
          "x": "0.324415",
          "y": "0.496516"
        }, {
          "x": "0.538462",
          "y": "0.486063"
        }, {
          "x": "0.438127",
          "y": "0.806620"
        }, {
          "x": "0.269231",
          "y": "0.759582"
        }]
      }
    }, {
      "id": "2",
      "polygonType": "1",
      "color": {
        "r": "255",
        "g": "0",
        "b": "0"
      },
      "tips": "#2#",
      "isClosed": "true",
      "pointList": {
        "point": [{
          "x": "0.168896",
          "y": "0.728223"
        }, {
          "x": "0.650502",
          "y": "0.780488"
        }, {
          "x": "0.493311",
          "y": "0.945993"
        }, {
          "x": "0.143813",
          "y": "0.970383"
        }, {
          "x": "0.157191",
          "y": "0.949477"
        }]
      }
    }]
  }
};
var eventAttributes = {
  '布防区域': [],
  '撤防区域': [],
  '开始检测时间': '00:00:00'
};

var _iterator = _createForOfIteratorHelper(json.SnapPolygonList.SnapPolygon),
    _step;

try {
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
    var snap = _step.value;
    var pointList = [];

    var _iterator2 = _createForOfIteratorHelper(snap.pointList.point),
        _step2;

    try {
      for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
        var point = _step2.value;
        pointList.push([point.x, point.y]);
      }
    } catch (err) {
      _iterator2.e(err);
    } finally {
      _iterator2.f();
    }

    if (snap.color.r === '0') {
      eventAttributes['布防区域'].push({
        '区域坐标': pointList,
        '区域编号': snap.id
      });
    }

    if (snap.color.r === '255') {
      eventAttributes['撤防区域'].push({
        '撤防区域': pointList,
        '区域编号': snap.id
      });
    }
  }
} catch (err) {
  _iterator.e(err);
} finally {
  _iterator.f();
}

;
console.log(eventAttributes);
宣传栏