2017-09-14 8 views
0

こんにちは、私は石鹸サービスからデータを取得し、JSONにXMLを変換し、私はこのように必要な値取得してい選ぶ:Lodashは深い

console.log(result['soap:Envelope']['soap:Body']['ns2:getFichaGeneralResponse']['return']['instituciones']['datosPrincipales']['registros'][1].valor) 

をこのような何かをする方法はありますか?

console.log(_.pick(result, 'registros')) 

私は必要な情報を持つオブジェクトを取得できますか?

+0

?その構造は何ですか?このconsole.logはエラー 'console.logをスローします(結果['soap:Envelope'] ['soap:Body'] ['ns2:getFichaGeneralResponse'] ['return'] ['instituciones'] ['datosPrincipales'] [ 'registros'] [1] .valor) ' –

答えて

1

私が正しく理解していると、複雑なオブジェクトがあり、指定されたパスでいくつかの小道具を選びたいのですか?例えば

、あなたは以下のオブジェクトからパスa.b.c{ d: 'foo', e: 'bar' }を取得したい: `result`変数をどのように見えるか

var object = { 
 
    a: { 
 
    b: { 
 
     c: { 
 
     d: 'foo', 
 
     e: 'bar', 
 
     f: 'baz' 
 
     } 
 
    } 
 
    }, 
 
    g: { 
 
    h: 1 
 
    } 
 
}; 
 

 
function pickPropsByPath(object, path, arrayOfPropsNames) { 
 
    return _.pick(_.get(object, path), arrayOfPropsNames); 
 
} 
 

 
console.log(pickPropsByPath(object, 'a.b.c', ['d', 'e'])) // => { d: 'foo', e: 'bar' }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>