3

json内のアイテムを整然と並べようとしています。json に存在する "term" 値を選択できましたが、期待される出力部分に示されている方法でこれを配置することは可能ですか? どこに到達したかを示す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


プロンプト入力 フィールドが選択されている場合 = 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をシムとして使用できます。

その後は、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がデータと呼ばれる場合、たとえば:Then

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

_index最初のオブジェクトまたはオブジェクトidからアクセスするjson場合:

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

alert(temp[0]._index)を実行すると、が表示されます"test"。他のプロパティについては、同じロジックに従います。このstackoverflowの質問、またはJSONページは、タスクを実行するために他で何をしなければならないかを理解するのに役立ちます。Yahooには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>

免責事項: 私はobject-scanの作成者です

于 2020-12-27T02:26:42.617 に答える