3

我试图让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


提示输入 If field 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;

如果字段选择 = Company,

预期输出:

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

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

3 回答 3

1

您可以在本机可用时使用 JSON 对象,也可以使用JSON2作为 shim。

之后,只需使用 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;
});
于 2012-07-18T11:32:37.277 回答
0

使用eval('(' + json_object + ')'),您将能够创建一个 JavaScript 对象。该对象将是一个数组,您可以使用.. 例如,如果你的json_object被称为数据,例如:那么

    var temp = eval('(' + data + ')'); // temp now is an array.

如果要访问第一个_indexidjson对象:

    "_index": "test",
    "id": "YMFT112",

做alert( temp[0]._index),它会告诉你"test"。对于其他属性,遵循相同的逻辑。这个stackoverflow 问题或JSON页面将帮助您了解在其他方面必须做什么才能完成任务。雅虎有一个称为YUI的 API ,它可能会更有帮助。

于 2012-07-12T12:52:59.913 回答
0

这是使用对象扫描的解决方案

// const objectScan = require('object-scan');

const data = [{"_index":"test","id":"YMFT112","_source":{"Author":"SAM","Map":[{"count":1,"term":"Company","Company":[{"sector":"Technology","ticker":"AAPL","Apple_Inc":[{"count":1,"term":"Apple"}],"term":"Apple Inc","type":"BCap"}]},{"count":1,"term":"Country","Country":[{"region":"North Americas","index":"DOW JONES INDUS. AVG , S&P 500 INDEX , NASDAQ COMPOSITE INDEX","United_States":[{"count":1,"term":"United States"}],"term":"Canada","currency":"Dollar (USD)","Canada":[{"count":1,"term":"Canada","Canada":[{"count":1,"term":"Toronto"}]}]}]},{"count":5,"term":"Personality","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"}]}]},"Link":"http://testLink.com/1"},{"_index":"test","_id":"YMFT113","_source":{"Author":"MAX","Map":[{"count":1,"term":"Company","Company":[{"sector":"Technology","ticker":"AAPL","Microsoft Corp":[{"count":1,"term":"Microsoft"}],"term":"Microsoft","type":"BCap"}]},{"count":1,"term":"Country","Country":[{"region":"South Americas","Brazil":[{"count":1,"term":"Brazil"}],"term":"Brazil","currency":"Dollar (USD)"}]},{"SalesRelated":[{"count":1,"term":"traffic"}]},{"count":4,"term":"Personality","Personality":[{"count":1,"term":"Maximor"},{"count":1,"term":"R.V.P"},{"count":1,"term":"Wenger"},{"count":1,"term":"SAF"}]}]},"Link":"http://testLink.com/2"}];

const find = (term, input) => {
  const r = objectScan([`[*]._source.Map[*].${term}[*].**.term`], {
    reverse: false,
    filterFn: ({ key, parents, context }) => {
      if (Object.values(parents[0]).some((e) => e instanceof Object)) {
        return;
      }
      const root = parents[parents.length - 2];
      context.push([
        root.id || root._id,
        parents[parents.length - 5].term,
        key[key.length - 3].replace(/_/g, ' '),
        ...parents.slice(0, -7).filter((e) => !Array.isArray(e)).map((p) => p.term).reverse(),
        root.Link
      ]);
    }
  })(input, []);
  const maxLength = Math.max(...r.map((e) => e.length));
  r
    .filter((e) => e.length < maxLength)
    .forEach((e) => e.splice(-1, 0, 'NA'.repeat(maxLength - e.length)));
  return r;
};

console.log(find('Country', data).map((e) => e.join(';    ')).join('\n'));
/* =>
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
 */
console.log(find('Company', data).map((e) => e.join(';    ')).join('\n'));
/* =>
YMFT112; Company;   Apple Inc;      Apple;      http://testLink.com/1
YMFT113; Company;   Microsoft Corp; Microsoft;  http://testLink.com/2
 */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>

免责声明:我是对象扫描的作者

于 2020-12-27T02:26:42.617 回答