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

标签 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']

最佳答案

因为你 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>

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

_.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>

关于javascript - 使用任何 lodash 获取嵌套 JSON 对象的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34974445/

相关文章:

javascript - 无法在基于 JavaScript 的游戏中初始化全局变量

javascript - sinon.js stub - 如何 stub async.map

javascript - 当我们有多个 setTimeouts 时,javascript 事件队列如何工作

javascript - 如何使用 grunt 缩小 JSON 文本文件?

javascript - 比较和分配数字

javascript - 使用 underscore.js 对数组升序排序

javascript - 将数字格式化为两位小数

node.js - typeORM: "message": "Data type\"对象\"in\".. ."is not supported by\"postgres\"database."

node.js - Git基于其他repo推送新的repo,开始新的

javascript - 使用下划线随机选择并从集合中删除