【问题标题】:Get all values of a nested JSON object using any lodash使用任何 lodash 获取嵌套 JSON 对象的所有值
【发布时间】:2016-01-24 10:03:56
【问题描述】:

场景:我有一个变量 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.a1like 的所有值的数组

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

【问题讨论】:

  • all the vaues of order.orderdetails.a1 ... order.orderdetails.a1 将是未定义的
  • @jaromandaX a1 的所有值表示,a1 的值在数组 orderdetails 中

标签: javascript node.js underscore.js lodash


【解决方案1】:

既然你已经 underscore.jslodash 包括在内,为什么不利用它们而不是重新发明轮子。

单行使用_.map 怎么样。 _.map 也接受字符串作为 iteratee 并将从传递的对象返回该键的值。

_.map(json.order.orderDetails, 'a1')

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>

这类似于旧版本 lodash 中的_.pluck

_.pluck(json.order.orderDetails, 'a1')

在纯 JavaScript 中使用 Array#map 可以实现相同的结果

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

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);
&lt;pre id="result"&gt;&lt;/pre&gt;

【讨论】:

    【解决方案2】:

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

    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);

    【讨论】:

    • 谢谢 sachin。我是 js 新手,不知道 map()。如果有嵌套数组,你能帮我找到“b1key”的值吗,比如“orderDetails”:[{“a1”:[“b1key”:“b1value”],“c1”:“d1”,“e1” : "f1" },
    • 嗯,没有嵌套数组,但是嵌套对象..欢迎您:)
    • btw 在这种情况下 a1 的值仍然是一个对象,而不是一个数组。 [ { "a1": {"b1key": "b1value"}, "c1": "d1", "e1": "f1" } 您将通过具有不同返回语句的相同函数获得它,如下所示:return obj.a1.b1key
    【解决方案3】:

    你可以试试这样的:

    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);

    【讨论】:

    • 嗨 rajesh,谢谢你的帮助。但我正在寻找一个库函数,以便维护更少的代码
    • 没关系。我仍然会保留我的答案。您可以将其用作参考。 :-)
    猜你喜欢
    • 2019-07-28
    • 2018-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多