深层对象的 getPathValue() 函数

getPathValue() function for deep objects

这是对此的后续问题:

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

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

...应该 return 只有这个字符串:

{"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');

...这会 return:

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

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

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

您可以在拆分前删除前导点。

function getValue(object, path) {
    return path
       .replace(/\[/g, '.')
       .replace(/\]/g, '')
       .replace(/^\./, '')
       .split('.')
       .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"},{"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 = getValue(obj, '[0]');
    
console.log(value)