javascript - 将javascript对象转换为有序的逗号分隔值

标签 javascript arrays json recursion hash

我试图让json中的项目有序排列。我能够选择 json 中存在的“术语”值,但是否可以按照我在预期输出部分中显示的方式进行安排?我添加了一个 jsfiddle 链接以显示我到达的位置:

[
    {
        "Link": "http://testLink.com/1",
        "_index": "test",
        "_source": {
            "Author": "SAM",
            "Map": [
                {
                    "Company": [
                        {
                            "Apple_Inc": [
                                {
                                    "count": 1,
                                    "term": "Apple"
                                }
                            ],
                            "sector": "Technology",
                            "term": "Apple Inc",
                            "ticker": "AAPL",
                            "type": "BCap"
                        }
                    ],
                    "count": 1,
                    "term": "Company"
                },
                {
                    "Country": [
                        {
                            "Canada": [
                                {
                                    "Canada": [
                                        {
                                            "count": 1,
                                            "term": "Toronto"
                                        }
                                    ],
                                    "count": 1,
                                    "term": "Canada"
                                }
                            ],
                            "United_States": [
                                {
                                    "count": 1,
                                    "term": "United States"
                                }
                            ],
                            "currency": "Dollar (USD)",
                            "index": "DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX",
                            "region": "North Americas",
                            "term": "Canada"
                        }
                    ],
                    "count": 1,
                    "term": "Country"
                },
                {
                    "Personality": [
                        {
                            "count": 1,
                            "term": "Bart Prince"
                        },
                        {
                            "count": 1,
                            "term": "Thomas"
                        },
                        {
                            "count": 1,
                            "term": "Deborah Hornstra"
                        },
                        {
                            "count": 1,
                            "term": "Henderson Sotheby"
                        },
                        {
                            "count": 1,
                            "term": "Max Alliance"
                        }
                    ],
                    "count": 5,
                    "term": "Personality"
                }
            ]
        },
        "id": "YMFT112"
    },
    {
        "Link": "http://testLink.com/2",
        "_id": "YMFT113",
        "_index": "test",
        "_source": {
            "Author": "MAX",
            "Map": [
                {
                    "Company": [
                        {
                            "Microsoft Corp": [
                                {
                                    "count": 1,
                                    "term": "Microsoft"
                                }
                            ],
                            "sector": "Technology",
                            "term": "Microsoft",
                            "ticker": "AAPL",
                            "type": "BCap"
                        }
                    ],
                    "count": 1,
                    "term": "Company"
                },
                {
                    "Country": [
                        {
                            "Brazil": [
                                {
                                    "count": 1,
                                    "term": "Brazil"
                                }
                            ],
                            "currency": "Dollar (USD)",
                            "region": "South Americas",
                            "term": "Brazil"
                        }
                    ],
                    "count": 1,
                    "term": "Country"
                },
                {
                    "SalesRelated": [
                        {
                            "count": 1,
                            "term": "traffic"
                        }
                    ]
                },
                {
                    "Personality": [
                        {
                            "count": 1,
                            "term": "Maximor"
                        },
                        {
                            "count": 1,
                            "term": "R.V.P"
                        },
                        {
                            "count": 1,
                            "term": "Wenger"
                        },
                        {
                            "count": 1,
                            "term": "SAF"
                        }
                    ],
                    "count": 4,
                    "term": "Personality"
                }
            ]
        }
    }
]

http://jsbin.com/exuwet/3/edit


提示输入 如果字段 Selected = Country,

预期输出:

YMFT112;    Country;    United States;  United States;      NA;         http://testLink.com/1;

YMFT112;    Country;    Canada;         Canada;             Toronto;    http://testLink.com/1;

YMFT113;    Country;    Brazil;         Brazil;             NA;         http://testLink.com/2;

如果字段 Selected = Company,

预期输出:

YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1;

YMFT113; Company;   Microsoft Corp; Microsoft;  http://testLink.com/2;

最佳答案

您可以在 native 可用时使用 JSON 对象或使用 JSON2作为垫片。

之后,只需使用 JavaScript 的内置排序功能即可。您提供一个函数来比较数组项

var myArray = JSON.parse(jsonString);
myArray.sort(function(a, b){
    var nameA = a._source.Map.Company.term;
    var nameB = b._source.Map.Company.term;

    if (nameA === nameB) {
        return 0;
    } else if (nameA < nameB) {
        return -1
    }
    return 1;
});

关于javascript - 将javascript对象转换为有序的逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11452106/

相关文章:

javascript - ES6 将对象传递给构造函数并设置属性

java - 我如何在测试类中构建数组

javascript - JSON.stringify (Javascript) 和 json.dumps (Python) 不等价吗?

javascript - 为什么 HTML/Javascript 按钮会弄乱我的 CSS 下拉菜单?

javascript - 未捕获的 InvalidStateError : Failed to execute 'dispatchEvent' on 'EventTarget' : The event provided is null

javascript - 使用选项卡集加载应用程序时如何使选项卡处于事件状态?

c - malloc 在第四次调用时使程序崩溃

javascript - 基本 64 字节数组在 chrome 中不起作用

java - 在 Java 中使用 JSON 的 HTTP POST

javascript - JQuery:获取 JSON 回复的长度?