代码之家  ›  专栏  ›  技术社区  ›  sixtyfootersdude

使用路径从对象提取数据-包括数组中的所有数据

  •  2
  • sixtyfootersdude  · 技术社区  · 6 年前

    我正试图用火山灰来解决以下问题。我知道如何使用for循环来解决问题,但是我正在寻找解决问题的现代函数方法。


    我有以下数据:

    const data = {
        people: [
            {
                name: "Bob",
                vehicles: [
                    {
                        model: "Mazda"
                        tires: [
                            { pressure: 20 },
                            { pressure: 22 },
                            { pressure: 23 },
                            { pressure: 21 },
                        ]
                    },
                    {
                        model: "Harley Davidson"
                        tires: [
                            { pressure: 20 }
                            { pressure: 25 }
                        ]
                    }
                ]
            },
            {...},
            {...},
            {...},
        ]
    }
    

    从中,我想提取每个人的轮胎气压列表。所以像这样:

    [20, 22, 23, 21, 20, 25, ... ]
    

    我要找的是一个我可以这样调用的方法:

    const path = 'people.vehicles[*].tires[*].pressure';
    const tirePressure = _.methodName(data, path);
    

    我知道Lodash支持一些类似的功能,比如 _.at(object, ['a[0].b.c', 'a[1]']); (link) 但据我所知,它不支持折叠整个数组。

    4 回复  |  直到 6 年前
        1
  •  1
  •   jannis Anjali    6 年前

    Lodash's flatMap flatMapDeep

    const data = {
        people: [
            {
                name: "Bob",
                vehicles: [
                    {
                        model: "Mazda",
                        tires: [
                            { pressure: 20 },
                            { pressure: 22 },
                            { pressure: 23 },
                            { pressure: 21 },
                        ]
                    },
                    {
                        model: "Harley Davidson",
                        tires: [
                            { pressure: 20 },
                            { pressure: 25 }
                        ]
                    }
                ]
            }
        ]
    };
    
    
    const pressures = _(data.people)
      .flatMap(_.property('vehicles'))
      .flatMap(_.property('tires'))
      .map(_.property('pressure'))
      .value();
    
    console.log(pressures);
    <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
        2
  •  0
  •   Muhammad Usman    6 年前

    JS

    .map

    const data = {
        people: [
            {
                name: "Bob",
                vehicles: [
                    {
                        model: "Mazda",
                        tires: [
                            { pressure: 20 },
                            { pressure: 22 },
                            { pressure: 23 },
                            { pressure: 21 },
                        ]
                    },
                    {
                        model: "Harley Davidson",
                        tires: [
                            { pressure: 20 },
                            { pressure: 25 }
                        ]
                    }
                ]
            }
        ]
    }
    var x=  data.people[0].vehicles.map(o=> o.tires.map(i=> i.pressure))
    
    console.log(x.join(",").split(","))
        3
  •  0
  •   Leonid Pyrlia    6 年前

    reduce

    const data={people:[{name:"Bob",vehicles:[{model:"Mazda",tires:[{pressure:20},{pressure:22},{pressure:23},{pressure:21}]},{model:"Harley Davidson",tires:[{pressure:20},{pressure:25}]}]}]};
    
    const result = data.people.reduce((all, {name, vehicles}) => {
    
      const pressures  = vehicles.reduce((total, {tires}) => {
        tires.forEach(({pressure}) => total.push(pressure));
        return total;
      }, []);
    
      all[name] = pressures;
    
      return all;
    
    }, {});
    
    console.log(result);
    
        4
  •  0
  •   Nina Scholz    6 年前

    pressure

    function getPressures(o) {
        return [].concat(...Object.entries(o).map(([key, value]) =>
            key === 'pressure'
                ? value
                : value && typeof value === 'object'
                    ? getPressures(value)
                    : []
        ));
    }
    
    var data = { people: [{ name: "Bob", vehicles: [{ model: "Mazda", tires: [{ pressure: 20 }, { pressure: 22 }, { pressure: 23 }, { pressure: 21 }] }, { model: "Harley Davidson", tires: [{ pressure: 20 }, { pressure: 25 }] }] }] };
    
    console.log(getPressures(data));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    function get(object, path) {
    
        function iter(o, p, i) {
            if (i === parts.length) {
                result[p.join('.')] = o;
                return;
            }
            if (!o || typeof o !== 'object') {
                return;
            }
            if (parts[i] === '*') {
                Object
                    .entries(o)
                    .forEach(([k, v]) => iter(v, p.concat(k), i + 1));
                return;
            }
            if (parts[i] in o) {
                iter(o[parts[i]], p.concat(parts[i]), i + 1);
            }
        }
    
        var result = {},
            parts = path.split('.');
    
        iter(object, [], 0);
        return result;
    }
    
    var data = { people: [{ name: "Bob", vehicles: [{ model: "Mazda", tires: [{ pressure: 20 }, { pressure: 22 }, { pressure: 23 }, { pressure: 21 }] }, { model: "Harley Davidson", tires: [{ pressure: 20 }, { pressure: 25 }] }] }] },
        result = get(data, 'people.*.vehicles.*.tires.*.pressure'),
        values = Object.values(result);
    
    console.log(result);
    console.log(values);