gpt4 book ai didi

javascript - 深度对象的 getPathValue() 函数

转载 作者:行者123 更新时间:2023-12-03 00:28:41 25 4
gpt4 key购买 nike

这是对此的后续问题: getPathValue() function for deep objects with arrays and with packed JSON

接受的答案在大多数情况下都非常有效:

function getPathValue(object, path) {
return path
.replace(/\[/g, '.')
.replace(/\]/g, '')
.split('.')
.reduce(function (o, k) {
return (typeof o === 'string' ? JSON.parse(o) : (o || {}))[k];
}, object);
}

我现在有一个新要求。我需要它来处理根级别的数组。例如这个对象:

[{"currencyPair":"EUR/USD","timestamp":"1546028298698","bidBig":"1.14","bidPips":"450","offerBig":"1.14","offerPips":"457","high":"1.14253","low":"1.14732","open":"1.14305"},{"currencyPair":"USD/JPY","timestamp":"1546028299095","bidBig":"110.","bidPips":"326","offerBig":"110.","offerPips":"336","high":"110.222","low":"111.062","open":"111.012"},{"currencyPair":"GBP/USD","timestamp":"1546028246468","bidBig":"1.26","bidPips":"974","offerBig":"1.26","offerPips":"988","high":"1.26350","low":"1.27087","open":"1.26505"},{"currencyPair":"EUR/GBP","timestamp":"1546028296658","bidBig":"0.90","bidPips":"127","offerBig":"0.90","offerPips":"140","high":"0.90034","low":"0.90601","open":"0.90355"},{"currencyPair":"USD/CHF","timestamp":"1546028296551","bidBig":"0.98","bidPips":"470","offerBig":"0.98","offerPips":"481","high":"0.97896","low":"0.99113","open":"0.98854"},{"currencyPair":"EUR/JPY","timestamp":"1546028299289","bidBig":"126.","bidPips":"322","offerBig":"126.","offerPips":"336","high":"126.256","low":"127.204","open":"127.008"},{"currencyPair":"EUR/CHF","timestamp":"1546028296543","bidBig":"1.12","bidPips":"714","offerBig":"1.12","offerPips":"726","high":"1.12146","low":"1.13180","open":"1.12920"},{"currencyPair":"USD/CAD","timestamp":"1546028299908","bidBig":"1.36","bidPips":"470","offerBig":"1.36","offerPips":"485","high":"1.35946","low":"1.36613","open":"1.36142"},{"currencyPair":"AUD/USD","timestamp":"1546028296222","bidBig":"0.70","bidPips":"445","offerBig":"0.70","offerPips":"453","high":"0.70238","low":"0.70695","open":"0.70288"},{"currencyPair":"GBP/JPY","timestamp":"1546028297767","bidBig":"140.","bidPips":"123","offerBig":"140.","offerPips":"148","high":"139.495","low":"140.628","open":"140.462"}]

我想保留先前接受的答案的所有功能,但此外,我希望能够检索特定的数组元素,甚至特定数组元素中的特定键值。例如,

value = getPathValue(obj, '[0]');

...应该只返回这个字符串:

{"currencyPair":"EUR/USD","timestamp":"1546028298698","bidBig":"1.14","bidPips":"450","offerBig":"1.14","offerPips":"457","high":"1.14253","low":"1.14732","open":"1.14305"}

value = getPathValue(obj, '[3].bidPips');

...将返回:

"0.90"

最佳答案

更改:

.split('.')

作者:

.split('.').filter(Boolean)

它将修复位于字符串开头的点,这是当输入以 [ 开头时发生的情况,如 [0] .

演示:

function getPathValue(object, path) {
return path
.replace(/\[/g, '.')
.replace(/\]/g, '')
.split('.').filter(Boolean)
.reduce(function (o, k) {
return (typeof o === 'string' ? JSON.parse(o) : (o || {}))[k];
}, object);
}

var obj = [{"currencyPair":"EUR/USD","timestamp":"1546028298698","bidBig":"1.14","bidPips":"450","offerBig":"1.14","offerPips":"457","high":"1.14253","low":"1.14732","open":"1.14305"},{"currencyPair":"USD/JPY","timestamp":"1546028299095","bidBig":"110.","bidPips":"326","offerBig":"110.","offerPips":"336","high":"110.222","low":"111.062","open":"111.012"}];

value = getPathValue(obj, '[0].bidPips');

console.log(value);

不过,通过用一个 match 调用替换 replacesplit 调用,可以更轻松地完成此操作:

function getPathValue(object, path) {
return path
.match(/[^[\].]+/g)
.reduce(function (o, k) {
return (typeof o === 'string' ? JSON.parse(o) : (o || {}))[k];
}, object);
}

var obj = [{"currencyPair":"EUR/USD","timestamp":"1546028298698","bidBig":"1.14","bidPips":"450","offerBig":"1.14","offerPips":"457","high":"1.14253","low":"1.14732","open":"1.14305"},{"currencyPair":"USD/JPY","timestamp":"1546028299095","bidBig":"110.","bidPips":"326","offerBig":"110.","offerPips":"336","high":"110.222","low":"111.062","open":"111.012"}];
value = getPathValue(obj, '[0].bidPips');

console.log(value);

关于javascript - 深度对象的 getPathValue() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com