根据angularjs中画布上的位置对列表项进行排序
Sorting list items based on postion on canvas in angularjs
我正在html5画布上使用angularjs和javascript以下面的json格式绘制矩形。我需要根据x,y的位置对数组进行排序。
{
"obj0": {
"outerRects": [
{
"outerRectRoi": {
"x1": 0,
"x2": 650,
"x3": 350,
"x4": 340,
"y1": 0,
"y2": 680,
"y3": 0,
"y4": 680
},
"line": [
{
"lineRoi": {
"x1": 471,
"x2": 460,
"y1": 1,
"y2": 680
},
"innerRect": [
{
"innerRoi": {
"x1": 368,
"x2": 390,
"y1": 50,
"y2": 55
}
},
{
"innerRoi": {
"x1": 368,
"x2": 390,
"y1": 70,
"y2": 74
}
}
]
},
{
"lineRoi": {
"x1": 202,
"x2": 197,
"y1": 1,
"y2": 680
}
},
{
"lineRoi": {
"x1": 380,
"x2": 372,
"y1": 1,
"y2": 680
}
}
]
},
{}
]
},
"obj1": {}
}
以下是根据Roi或画布上的位置进行排序的条件:
a。外部矩形(outerResults)必须从左到右排序。它们可以在下方的多行中
1 2
3 4
b。行也要在外部矩形中从左到右排序。
c。innerRect将根据其在行顶部的位置从上到下进行排序。
这将是预期的json结果
{
"obj0": {
"outerRects": [
{
"outerRectRoi": {
"x1": 0,
"x2": 650,
"x3": 350,
"x4": 340,
"y1": 0,
"y2": 680,
"y3": 0,
"y4": 680
},
"line": [
{
"lineRoi": {
"x1": 202,
"x2": 197,
"y1": 1,
"y2": 680
},
"innerRect": []
},
{
"lineRoi": {
"x1": 380,
"x2": 372,
"y1": 1,
"y2": 680
},
"innerRext": []
},
{
"lineRoi": {
"x1": 471,
"x2": 460,
"y1": 1,
"y2": 680
},
"innerRect": [
{
"innerRoi": {
"x1": 368,
"x2": 390,
"y1": 70,
"y2": 74
}
},
{
"innerRoi": {
"x1": 368,
"x2": 390,
"y1": 50,
"y2": 55
}
}
]
}
]
},
{}
]
},
"obj1": {}
}
如何使用angularjs和javascript实现这一点?
感谢
您可以迭代各种数组,并根据需要使用它们的属性对子数组进行排序。
var object = { "obj0": { "outerRects": [{ "outerRectRoi": { "x1": 0, "x2": 650, "x3": 350, "x4": 340, "y1": 0, "y2": 680, "y3": 0, "y4": 680 }, "line": [{ "lineRoi": { "x1": 471, "x2": 460, "y1": 1, "y2": 680 }, "innerRect": [{ "innerRoi": { "x1": 368, "x2": 390, "y1": 50, "y2": 55 } }, { "innerRoi": { "x1": 368, "x2": 390, "y1": 70, "y2": 74 } }] }, { "lineRoi": { "x1": 202, "x2": 197, "y1": 1, "y2": 680 } }, { "lineRoi": { "x1": 380, "x2": 372, "y1": 1, "y2": 680 } }] }, {}] }, "obj1": {} }
object.obj0.outerRects.forEach(function (a) {
if (a.line) {
a.line.sort(function (a, b) {
return a.lineRoi.x1 - b.lineRoi.x1;
});
a.line.forEach(function (b) {
if (b.innerRect) {
b.innerRect.sort(function (a, b) {
return a.innerRoi.y1 - b.innerRoi.y1;
});
}
});
}
});
document.write('<pre>' + JSON.stringify(object, 0, 4) + '</pre>');
相关文章:
- 为什么HTML5拖放的目标是孩子?(可排序列表)
- 当属性不一致时,如何根据属性对JS对象列表进行排序
- jQuery UI可排序-多连接列表拖动
- 如何在javascript中通过rtl字母表对列表进行排序和分组
- 如何在Javascript中创建排序、拖放多级列表
- 使用选项组对下拉列表进行排序
- JQuery根据连字符前的首字母对列表进行排序
- 可排序和可丢弃的淘汰列表
- 在Javascript中,当使用array.prush方法向列表中添加项目时,如何对其进行排序
- 使用JS或新查询对列表进行排序
- 对Soundcloud自定义播放器播放列表的顺序进行排序
- 如何在角度js的自动完成下拉列表中对项目列表(按字母顺序)进行排序
- 尝试将长列表排序为单独的列
- 不使用排序数组的动态列表排序
- 使用角度 js 按顺序列表排序
- 在JavaScript和MySQL中存储列表排序顺序的替代方法
- 如何用Vue.js动画化列表排序
- 按字母顺序对列表排序,末尾为字符
- IE7,jQuery-1.6.2:列表项向下移动到包含元素的下面(多列表排序)
- 按元素的值对列表排序