使用任何lodash获取嵌套JSON对象的所有值

时间:2016-01-24 10:03:57

标签: javascript node.js underscore.js lodash

场景:我有一个变量JSON

var json = {
    "order": {
        "orderDetails": [{
            "a1": "b1",
            "c1": "d1",
            "e1": "f1"
        }, {
            "a1": "b2",
            "c1": "d2",
            "e1": "f2"
        }, {
            "a1": "b3",
            "c1": "d3",
            "e1": "f3"
        }],
        "orderHeader": [{
            "a2": "b1",
            "c2": "d1",
            "e2": "f1"
        }, {
            "a2": "b2",
            "c2": "d2",
            "e2": "f2"
        }]
    }
};

我需要获取order.orderdetails.a1的所有值的数组,如

['b1', 'b2', 'b3']

3 个答案:

答案 0 :(得分:6)

正如您lodash_.map所包含,为什么不利用它们而不是重新发明轮子。

使用_.pluck的单行怎么样? _.map(json.order.orderDetails, 'a1') 也接受字符串 iteratee ,并将从传递的对象返回该键的值。

var json = {
    "order": {
        "orderDetails": [{
            "a1": "b1",
            "c1": "d1",
            "e1": "f1"
        }, {
            "a1": "b2",
            "c1": "d2",
            "e1": "f2"
        }, {
            "a1": "b3",
            "c1": "d3",
            "e1": "f3"
        }],
        "orderHeader": [{
            "a2": "b1",
            "c2": "d1",
            "e2": "f1"
        }, {
            "a2": "b2",
            "c2": "d2",
            "e2": "f2"
        }]
    }
};

var result = _.map(json.order.orderDetails, 'a1');

console.log(result);
document.getElementById('result').innerHTML = JSON.stringify(result, 0, 4); // For Demo: Showing the result on the screen



<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.0.0/lodash.js"></script>
<pre id="result"></pre>
&#13;
_.pluck(json.order.orderDetails, 'a1')
&#13;
&#13;
&#13;

这与旧版本的lodash中的Array#map类似。

json.order.orderDetails.map(e => e.a1)

使用{{3}}

在纯JavaScript中可以实现相同的结果
var json = {
    "order": {
        "orderDetails": [{
            "a1": "b1",
            "c1": "d1",
            "e1": "f1"
        }, {
            "a1": "b2",
            "c1": "d2",
            "e1": "f2"
        }, {
            "a1": "b3",
            "c1": "d3",
            "e1": "f3"
        }],
        "orderHeader": [{
            "a2": "b1",
            "c2": "d1",
            "e2": "f1"
        }, {
            "a2": "b2",
            "c2": "d2",
            "e2": "f2"
        }]
    }
};

var result = json.order.orderDetails.map(e => e.a1);
console.log(result);
document.getElementById('result').innerHTML = JSON.stringify(result, 0, 4);

&#13;
&#13;
<pre id="result"></pre>
&#13;
IBOutlet UITextField* fldUsername;

IBOutlet UITextField* fldPassword;

@property (nonatomic, retain) IBOutlet UITextField*  nicknameTextField;
@property (nonatomic, retain) IBOutlet UITextField* secretCodeTextField;
&#13;
&#13;
&#13;

答案 1 :(得分:5)

您可以使用此代码执行此操作。

&#13;
&#13;
var json={
              "order": {
                "orderDetails": [
                  {
                    "a1": "b1",
                    "c1": "d1",
                    "e1": "f1"
                  },
                  {
                    "a1": "b2",
                    "c1": "d2",
                    "e1": "f2"
                  },
                  {
                    "a1": "b3",
                    "c1": "d3",
                    "e1": "f3"
                  }
                ],
                "orderHeader": [
                  {
                    "a2": "b1",
                    "c2": "d1",
                    "e2": "f1"
                  },
                  {
                    "a2": "b2",
                    "c2": "d2",
                    "e2": "f2"
                  }
                ]
              }
            }

var a1 = json.order.orderDetails.map(function(obj){ return obj.a1 });
console.log(a1);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以尝试这样的事情:

&#13;
&#13;
var json = {
   "order": {
     "orderDetails": [{
       "a1": "b1",
       "c1": "d1",
       "e1": "f1"
     }, {
       "a1": "b2",
       "c1": "d2",
       "e1": "f2"
     }, {
       "a1": "b3",
       "c1": "d3",
       "e1": "f3"
     }],
     "orderHeader": [{
       "a2": "b1",
       "c2": "d1",
       "e2": "f1"
     }, {
       "a2": "b2",
       "c2": "d2",
       "e2": "f2"
     }]
   }
 }

 var result = {};

// Loop over props of "order"
 for (var order in json.order){
   
   // Each prop is array. Loop over them
   json.order[order].forEach(function(item) {
     
     // Loop over each object's prop
     for (var key in item) {
       
       // Check if result has a prop with key. If not initialize it.
       if (!result[key])
         result[key] = [];

       // Push vaues to necessary array
       result[key].push(item[key]);
     }
   })
 };

 console.log(result);
&#13;
&#13;
&#13;