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

5

场景:我有一个变量 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']

order.orderdetails.a1 的所有值都将是未定义的。 - Jaromanda X
@jaromandaX,a1的所有值指的是orderdetails数组中a1的所有值。 - Jibin TJ
3个回答

6

既然你已经使用了underscore.js,包括lodash,为什么不利用它们,而是重复造轮子呢。

那么,使用_.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);
<pre id="result"></pre>


5
你可以使用这段代码来实现。

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" }, - Jibin TJ
好的,这里没有嵌套数组,但有嵌套对象.. 不客气 :) - Sachin
顺便提一句,在这种情况下,a1的值仍然是一个对象,而不是一个数组。[ { "a1": {"b1key": "b1value"}, "c1": "d1", "e1": "f1" } 你可以通过相同的函数获得它,只需使用不同的返回语句,如下所示:return obj.a1.b1key - Sachin

1
你可以尝试像这样做:

你可以尝试像这样做:

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,谢谢你的帮助。但我正在寻找一个库函数,以便减少需要维护的代码量。 - Jibin TJ
没问题。我还是会保留我的答案,你可以把它作为参考。 :-) - Rajesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接