2017-11-08 24 views
-2

私は、不規則になることがある大きなJSONオブジェクトを持っています。私は、そのオブジェクトをすべてフラットな配列に配置する方法を見つけることを望んでいます。入れ子オブジェクトと配列のオブジェクトを再帰的に平坦化するjavascript

これは、例えば、JSONです:

{ 
    "name":"getPost", 
    "request":{ 
     "type":"object", 
     "fields":[{ 
      "key":"id", 
      "value":{"type":"string"} 
     }] 
    }, 
    "response":{ 
     "type":"object", 
     "fields":[{ 
      "key":"post", 
      "value":{ 
       "type":"object", 
       "fields":[{ 
        "key":"id", 
        "value":{"type":"string"} 
       },{ 
        "key":"title", 
        "value":{"type":"string"} 
       },{ 
        "key":"content", 
        "value":{"type":"string"} 
       },{ 
        "key":"userId", 
        "value":{"type":"string"} 
       }] 
      } 
     }] 
    }, 
    "error":[] 
} 

と私はそれが私が唯一の浅いキーの値を持つオブジェクトが必要:(このような何かになりたい)

[ 
    { 
     "key":"id", 
     "value":{"type":"string"} 
    },{ 
     "key":"post", 
     "value":{"type":"object"} 
    },{ 
     "key":"id", 
     "value":{"type":"string"} 
    },{ 
     "key":"title", 
     "value":{"type":"string"} 
    },{ 
     "key":"content", 
     "value":{"type":"string"} 
    },{ 
     "key":"userId", 
     "value":{"type":"string"} 
    } 
] 

私が知っている必要がありますES6なしでこれを行う簡単な方法があれば。

+2

これは非常に特殊な問題です。配列を平坦化するだけでなく、データが正しく見えるときに知っている形式にデータを正規化します。したがって、問題は次のとおりです。ターゲット構造を定義するルールは何ですか? –

+0

... - ***のOPの要求に固執している場合、浅いキー値を持つオブジェクトのみが必要です*** - 1つは '{" type ":" string "}'のリストで終わりますこれは唯一の浅い部分構造です。 '{" key ":" userId "、" value ": {" type ":" string "}}'すでに複雑すぎます。または***浅いキー値***はどういう意味ですか? –

+0

私は型を持つすべてのオブジェクトを意味する、私の問題は型を検証したい、文字列や数値、配列やオブジェクトのようにプリミティブにすることができます。また、それらをすべて見つける必要があるので、それらを検証します。 @PeterSeliger –

答えて

1

次に提供されるアプローチは、提供する必要のある別のキーとの一致を探しながら、任意のデータ構造のプロパティーキーリストを再帰的に歩きます。

キーが一致する場合、関連/参照される(サブ)データ構造は、追加提供されるリストを介して収集されます。

(サブ)データ構造が配列の場合、その各データ項目も再帰的に処理されます。

任意の(サブ)データ構造が文字列型でない場合、プロセスはこのテキストの冒頭で説明したように新しい再帰を開始します。処理されたデータ構造について

var data = { 
 
\t "name": "getPost", 
 
\t "request": { 
 
\t \t "type": "object", 
 
\t \t "fields": [{ 
 
\t \t \t "key": "id", 
 
\t \t \t "value": { 
 
\t \t \t \t "type": "string" 
 
\t \t \t } 
 
\t \t }] 
 
\t }, 
 
\t "response": { 
 
\t \t "type": "object", 
 
\t \t "fields": [{ 
 
\t \t \t "key": "post", 
 
\t \t \t "value": { 
 
\t \t \t \t "type": "object", 
 
\t \t \t \t "fields": [{ 
 
\t \t \t \t \t "key": "id", 
 
\t \t \t \t \t "value": { 
 
\t \t \t \t \t \t "type": "string" 
 
\t \t \t \t \t } 
 
\t \t \t \t }, { 
 
\t \t \t \t \t "key": "title", 
 
\t \t \t \t \t "value": { 
 
\t \t \t \t \t \t "type": "string" 
 
\t \t \t \t \t } 
 
\t \t \t \t }, { 
 
\t \t \t \t \t "key": "content", 
 
\t \t \t \t \t "value": { 
 
\t \t \t \t \t \t "type": "string" 
 
\t \t \t \t \t } 
 
\t \t \t \t }, { 
 
\t \t \t \t \t "key": "userId", 
 
\t \t \t \t \t "value": { 
 
\t \t \t \t \t \t "type": "string" 
 
\t \t \t \t \t } 
 
\t \t \t \t }] 
 
\t \t \t } 
 
\t \t }] 
 
\t }, 
 
\t "error": [] 
 
}; 
 

 

 
function collectListOfAllSubTypeDataRecursivelyByKey(collector, key) { 
 
    var 
 
    itemKey = collector.key, 
 
    data = collector.data, 
 
    list = collector.list; 
 

 
    if (Array.isArray(data)) { 
 
    data.forEach(function (dataItem) { 
 

 
     collector.list = list.concat(Object.keys(dataItem).reduce(collectListOfAllSubTypeDataRecursivelyByKey, { 
 

 
     key: itemKey, 
 
     data: dataItem, 
 
     list: [] 
 

 
     }).list); 
 
    }) 
 
    } else { 
 
    if (key === itemKey) { 
 

 
     list.push(data); 
 
    } 
 
    if (typeof (data = data[key]) !== 'string') { 
 

 
     collector.list = list.concat(Object.keys(data).reduce(collectListOfAllSubTypeDataRecursivelyByKey, { 
 

 
     key: itemKey, 
 
     data: data, 
 
     list: [] 
 

 
     }).list); 
 
    } 
 

 
    } 
 
    return collector; 
 
} 
 

 

 
var subTypeList = Object.keys(data).reduce(collectListOfAllSubTypeDataRecursivelyByKey, { 
 

 
    key: 'type', 
 
    data: data, 
 
    list: [] 
 

 
}).list; 
 

 

 
subTypeList.forEach(function (type) { 
 
    console.log('type : ', type); 
 
}); 
 
// console.log('subTypeList : ', subTypeList);
.as-console-wrapper { max-height: 100%!important; top: 0; }

OPによって提供されるように、各そのようtypeプロパティを特徴一口とサブデータ構造の8つの項目を参照するリストが存在することになります頼んでいた。したがって、この8項目のフラット化されたリストを反映します。