簡體   English   中英

使用任何lodash獲取嵌套JSON對象的所有值

[英]Get all values of a nested JSON object using any 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的所有值的order.orderdetails.a1

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

正如你所強調的那樣 ,包括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> 

這類似於_.pluck舊版本lodash的。

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

使用Array#map在純JavaScript中可以實現相同的結果

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> 

您可以使用此代碼執行此操作。

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

你可以嘗試這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM