2017-08-13 6 views
0

私はlodashを初めて使用しており、以下のクエリを持っています。私は私のサンプルjsonを添付しています。私は他の多くの記事を参照さが、何も私は真の状態を持つ配列を見つけ、以下返すようにしたいlo-dash:JSONを解析して必要な出力を返す

  1. 名のみ
  2. 名を値とPARTNO

    • 私のために働いていない

  • 私はまた、州と名前を返すだけです。

  • [{ 
     
    \t \t 'state': true, 
     
    \t \t 'partno': 21, 
     
    \t \t 'idno': 1, 
     
    \t \t 'name': 'abc' 
     
    \t }, 
     
    \t { 
     
    \t \t 'state': true, 
     
    \t \t 'partno': 22, 
     
    \t \t 'idno': 2, 
     
    \t \t 'name': 'xyz' 
     
    \t }, 
     
    \t { 
     
    \t \t 'state': false, 
     
    \t \t 'partno': 23, 
     
    \t \t 'idno': 3, 
     
    \t \t 'name': 'mnd' 
     
    \t }, 
     
    \t { 
     
    \t \t 'state': true, 
     
    \t \t 'partno': 26, 
     
    \t \t 'idno': 4, 
     
    \t \t 'name': 'koi' 
     
    \t }, 
     
    \t { 
     
    \t \t 'state': false, 
     
    \t \t 'partno': 21, 
     
    \t \t 'idno': 1, 
     
    \t \t 'name': 'abc' 
     
    \t } 
     
    ]

    答えて

    0

    以下のコードは、何が必要ありません

    const data = getData(); 
     
    
     
    console.log('I want to...\n\n'); 
     
    
     
    
     
    console.log('1. find array with state as true and return name only') 
     
    const result1 = _(data) 
     
        .filter('state') 
     
        .map('name') 
     
        .value(); 
     
    
     
    // logAsJson is a helper function below 
     
    logAsJson(result1); 
     
         
     
         
     
    console.log('2. find array with state as true and return name and partno'); 
     
    const result2 = _(data) 
     
        .filter('state') 
     
        // pickProperties is a helper function below 
     
        .map(pickProperties(['name', 'partno'])) 
     
        .value(); 
     
    logAsJson(result2); 
     
    
     
    
     
    console.log('3. just return state and name'); 
     
    const result3 = _.map(data, pickProperties(['state', 'name'])); 
     
    logAsJson(result3); 
     
    
     
    
     
    // 
     
    // Helper functions 
     
    // 
     
    
     
    function logAsJson(anObject) { 
     
        console.log(JSON.stringify(anObject, null, 2)); 
     
    } 
     
    
     
    function pickProperties(propsToPick) { 
     
        return function mapperFn(anObject) { 
     
        return _.pick(anObject, propsToPick); 
     
        }; 
     
    } 
     
    
     
    function getData() { 
     
        return [ 
     
        { 
     
         'state': true, 
     
         'partno': 21, 
     
         'idno': 1, 
     
         'name': 'abc' 
     
        }, 
     
        { 
     
         'state': true, 
     
         'partno': 22, 
     
         'idno': 2, 
     
         'name': 'xyz' 
     
        }, 
     
        { 
     
         'state': false, 
     
         'partno': 23, 
     
         'idno': 3, 
     
         'name': 'mnd' 
     
        }, 
     
        { 
     
         'state': true, 
     
         'partno': 26, 
     
         'idno': 4, 
     
         'name': 'koi' 
     
        }, 
     
        { 
     
         'state': false, 
     
         'partno': 21, 
     
         'idno': 1, 
     
         'name': 'abc' 
     
        } 
     
        ]; 
     
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

    0

    たぶん私はあなたの質問を得るが、ここにあなたが要求するものではありませんでした。

    //Filters your array and creates a new object 
    yourArray.filter(x => x.state) 
         .map(x => ({'name':x.name, 'partno':x.partno})) 
    

    私はlodashを知っているが、ドキュメントを見てみませんでした、私はこれと一緒に来た:

    _map(_.filter(yourArray, 'state'), (x) => ({'name':x.name,'partno':x.partno}) 
    

    これはほとんど同じです。これにロダッシュを使用している理由はありますか?

    関連する問題