2017-11-20 7 views
3

私は以下のオブジェクトをオブジェクトおよび非オブジェクトと混在させています。オブジェクトをループして特定の値を取得する

{ 
    "boundingBox": "250,420,124,59", 
    "lines": [ 
    { 
     "boundingBox": "281,420,62,15", 
     "words": [ 
     { 
      "boundingBox": "281,420,62,15", 
      "text": "BLACK" 
     } 
     ] 
    }, 
    { 
     "boundingBox": "250,441,124,16", 
     "words": [ 
     { 
      "boundingBox": "250,441,75,16", 
      "text": "FOREST" 
     }, 
     { 
      "boundingBox": "331,441,43,16", 
      "text": "HAM" 
     } 
     ] 
    }, 
    { 
     "boundingBox": "275,463,73,16", 
     "words": [ 
     { 
      "boundingBox": "275,464,53,15", 
      "text": "290\/570" 
     }, 
     { 
      "boundingBox": "332,463,16,15", 
      "text": "cal" 
     } 
     ] 
    } 
    ] 
} 

私が達成したいのは、すべてのテキスト値を抽出することです。 これから返されると予想されるのは、(黒、森林、ハム、290/570、cal)です。

私は、以前より小さなオブジェクト上でこれをやった:

{ 
    "boundingBox": "275,463,73,16", 
    "words": [ 
    { 
    "boundingBox": "275,464,53,15", 
    "text": "290\/570" 
    }, 
    { 
     "boundingBox": "332,463,16,15", 
     "text": "cal" 
    } 
    ] 
} 

そして、私は次のコードで(570分の290、CAL)を達成することができました。

for (x in jsonStruct) { 
    $initialValue = ""; 
    if (typeof(jsonStruct[x]) == "object") { 
     //var initialValue = traverseJSON(jsonStruct[x], initialValue); 
     var wantedValue = jsonStruct[x]; 
     for (var i=0;i<wantedValue.length; i++){ 
      initialValue += wantedValue[i].text +","; 
     } 
    } else { 
     initialValue += x + "->" + jsonStruct[x] + "/"; 
    } 
} 
return initialValue; 

しかしながら、上記の大きな目的で、Iは、値のいくつかは目的ではないので、コードストップがオブジェクトではない最初のもの、で実行することを考えます。私が得た唯一の応答はboundingBox-> 250,420,124,59 /でした。

オブジェクト全体をループするループを作成するには、オブジェクトであるかどうかにかかわらずすべてのテキスト値を返します。あまりにも長い彼らはすべてのテキスト値を返しますか?

あなたのご協力をお待ちしております。ありがとうございました!

+0

入力の構造は固定されていませんか?大きなオブジェクトには 'line'があります –

答えて

1

私はこれが仕事をすると信じて:

const obj = { 
    "boundingBox": "250,420,124,59", 
    "lines": [ 
    { 
     "boundingBox": "281,420,62,15", 
     "words": [ 
     { 
      "boundingBox": "281,420,62,15", 
      "text": "BLACK" 
     } 
     ] 
    }, 
    { 
     "boundingBox": "250,441,124,16", 
     "words": [ 
     { 
      "boundingBox": "250,441,75,16", 
      "text": "FOREST" 
     }, 
     { 
      "boundingBox": "331,441,43,16", 
      "text": "HAM" 
     } 
     ] 
    }, 
    { 
     "boundingBox": "275,463,73,16", 
     "words": [ 
     { 
      "boundingBox": "275,464,53,15", 
      "text": "290\/570" 
     }, 
     { 
      "boundingBox": "332,463,16,15", 
      "text": "cal" 
     } 
     ] 
    } 
    ] 
} 

const result = obj.lines.reduce(function(acc, line){ 
    line.words.forEach(function(word){ 
     acc.push(word.text)); 
    }; 
    return acc; 
}, []); 

//Or in arrow notation: 
const result = obj.lines.reduce((acc, line) => { 
    line.words.forEach(word => acc.push(word.text)); 
    return acc; 
}, []); 

console.log(result); 
// Prints out ["BLACK", "FOREST", "HAM", "290/570", "cal"] 

私はあなたが、配列を反復処理し、ご希望の結果を蓄積することができます減らす機能を使用しています。 また、矢印表記の構文にも注意してください。

これが役に立ちます。

関連する問題