JavaScript 排序多维 JSON
我有这个 JSON 字符串:
{
"widgets":[
{"column1":[
{"weight":1, "bID":1, "hideMe":false, "collapse":false, "titleOf":"Test 1", "colorOf":"color-blue", "theFunction":"functionName"},
{"weight":2, "bID":2, "hideMe":false, "collapse":false, "titleOf":"Test 2", "colorOf":"color-red", "theFunction":"functionName"},
{"weight":3, "bID":3, "hideMe":false, "collapse":true, "titleOf":"Test 3", "colorOf":"color-yellow", "theFunction":"functionName"}
]},
{"column2":[
{"weight":1, "bID":4, "hideMe":false, "collapse":false, "titleOf":"Test 4", "colorOf":"color-white", "theFunction":"functionName"},
{"weight":3, "bID":5, "hideMe":false, "collapse":false, "titleOf":"Test 5", "colorOf":"color-green", "theFunction":"functionName"},
{"weight":2, "bID":6, "hideMe":false, "collapse":true, "titleOf":"Test 6", "colorOf":"color-green", "theFunction":"functionName"}
]},
{"column3":[
{"weight":3, "bID":7, "hideMe":false, "collapse":false, "titleOf":"Test 7", "colorOf":"color-green", "theFunction":"functionName"},
{"weight":2, "bID":8, "hideMe":false, "collapse":true, "titleOf":"Test 8", "colorOf":"color-yellow", "theFunction":"functionName"},
{"weight":1, "bID":9, "hideMe":false, "collapse":false, "titleOf":"Test 9", "colorOf":"color-white", "theFunction":"functionName"},
]}
]}
如果我这样做,
alert(testJSON.widgets.length);
我会得到 3,但是如果我这样做,
alert(testJSON.widgets.column1.length);
我会收到“testJSON.widgets.column3 is undefined”错误。
我最终想要做的是获取每一列 1-3 并按权重对它们进行排序。通过类似
testJSON.widgets.column1.sort(function(a,b) { return parseFloat(a.weight) - parseFloat(b.weight) });
这样我就可以通过 jquery 执行 $.each()
I have this JSON string:
{
"widgets":[
{"column1":[
{"weight":1, "bID":1, "hideMe":false, "collapse":false, "titleOf":"Test 1", "colorOf":"color-blue", "theFunction":"functionName"},
{"weight":2, "bID":2, "hideMe":false, "collapse":false, "titleOf":"Test 2", "colorOf":"color-red", "theFunction":"functionName"},
{"weight":3, "bID":3, "hideMe":false, "collapse":true, "titleOf":"Test 3", "colorOf":"color-yellow", "theFunction":"functionName"}
]},
{"column2":[
{"weight":1, "bID":4, "hideMe":false, "collapse":false, "titleOf":"Test 4", "colorOf":"color-white", "theFunction":"functionName"},
{"weight":3, "bID":5, "hideMe":false, "collapse":false, "titleOf":"Test 5", "colorOf":"color-green", "theFunction":"functionName"},
{"weight":2, "bID":6, "hideMe":false, "collapse":true, "titleOf":"Test 6", "colorOf":"color-green", "theFunction":"functionName"}
]},
{"column3":[
{"weight":3, "bID":7, "hideMe":false, "collapse":false, "titleOf":"Test 7", "colorOf":"color-green", "theFunction":"functionName"},
{"weight":2, "bID":8, "hideMe":false, "collapse":true, "titleOf":"Test 8", "colorOf":"color-yellow", "theFunction":"functionName"},
{"weight":1, "bID":9, "hideMe":false, "collapse":false, "titleOf":"Test 9", "colorOf":"color-white", "theFunction":"functionName"},
]}
]}
if I do
alert(testJSON.widgets.length);
I get 3, however if I do
alert(testJSON.widgets.column1.length);
I get "testJSON.widgets.column3 is undefined" as an error.
What I am ultimately attempting to do is take each column1-3 and sort them by weight. Through something like
testJSON.widgets.column1.sort(function(a,b) { return parseFloat(a.weight) - parseFloat(b.weight) });
So I can then do a $.each() via jquery
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
而不是:
使用
“column1”是数组“testJSON.widgets”的第一个对象中的属性
Instead of:
use
"column1" is a property in the first of object of the array "testJSON.widgets"
如果您希望能够像您提到的那样进行访问,则需要像这样布局:
不需要将列包装在数组中。
If you want to be able to access like you mentioned, you need to lay it out like this:
No need to wrap the columns in an array.
你有数组而不是对象。
您应该编写
alert(testJSON.widgets[0].length); 而不是
alert(testJSON.widgets.column1.length);
You have arrays not objects.
Instead of
alert(testJSON.widgets.column1.length);
you should writealert(testJSON.widgets[0].length);
“widgets”键包含一个对象数组,因此您需要指定一个偏移量以获取适当的列。
您可以在这里尝试。
我建议修改架构,以便改为“columnX”的它只是“column”。这将简化遍历,因为您已经通过“小部件”的偏移量知道了列号,例如:
演示(新架构)。
The "widgets" key contains an array of objects, so you need to specify an offset to grab the appropriate column.
You can try it here.
I would suggest revising the schema such that instead of "columnX" it's just "column". That will simplify traversal since you already know the column number via the offset of the "widget", e.g.:
Demo (new schema).
testJSON.widgets.column1
未定义的原因是widgets
是一个Array
,并且您没有访问数组索引。您可以改为访问testJSON.widgets[0].column1
,或者将您的 JSON 重构为如下所示:更新:
实际上,我认为您的真实情况 em>想要的是有一个“二维”数组(实际上是数组的数组);无法在循环中方便地访问命名列。这将是一个更好的满足您需求的结构:
The reason why
testJSON.widgets.column1
is undefined is becausewidgets
is anArray
, and you're not accessing an array index. You can accesstestJSON.widgets[0].column1
instead, or restructure your JSON to look like this:Update:
Actually, I think what you really want is to have a "two-dimensional" array (actually an array of arrays); named columns cannot be conveniently accessed in a loop. This would be a better structure for your needs: