gpt4 book ai didi

json - 如何过滤具有多个整数条件和键值的 JSON 对象数组

转载 作者:行者123 更新时间:2023-12-03 06:36:11 25 4
gpt4 key购买 nike

我在 VueJS ( It's a app with drop downs and ranges @ codepen ) 中挣扎于交互式搜索过滤器

一条船有 BrandName、BrandYear、Price...,我可以使用 selected = {...} 过滤掉这些信息。 ,但我想知道如何充分利用此 if-statement下面,通过传递 expected_selected = {...} 来识别价格并检查最小值/最大值并返回结果

我正在寻找有关如何与以下代码一起过滤最小值/最大值的解释/帮助。

目标是输入最小值和最大值以及一个或多个匹配的键值

var boats = [{
Price: 599900,
BrandName: "FLIPPER",
BoatYear: 2020,
}, {
Price: 97e3,
BrandName: "MICORE",
BoatYear: 2020,
}, {
Price: 189300,
BrandName: "LINDER",
BoatYear: 2020,
}, {
Price: 396900,
BrandName: null,
BoatYear: 2020,
}, {
Price: 334900,
BrandName: "MICORE",
BoatYear: 2019,
}, {
Price: 138700,
BrandName: "HR",
BoatYear: 2020,
}, {
Price: 178900,
BrandName: "HR",
BoatYear: 2020,
}, {
Price: 348900,
BrandName: "HR",
BoatYear: 2020,
}, {
Price: 285800,
BrandName: "HR",
BoatYear: 2020,
}, {
Price: 186900,
BrandName: "MICORE",
BoatYear: 2019,
}, {
Price: 276800,
BrandName: "MICORE",
BoatYear: 2020,
}, {
Price: 518900,
BrandName: "SILVER",
BoatYear: 2020,
}, {
Price: 226900,
BrandName: "MICORE",
BoatYear: 2020,
}, {
Price: 132600,
BrandName: "LINDER",
BoatYear: 2020,
}, {
Price: 137200,
BrandName: "LINDER",
BoatYear: 2020,
}, {
Price: 366900,
BrandName: "SILVER",
BoatYear: 2020,
}, {
Price: 365900,
BrandName: "SILVER",
BoatYear: 2020,
}, {
Price: 247900,
BrandName: "SILVER",
BoatYear: 2020,
}];


var selected = {
BoatYear: 2020,
BrandName: "LINDER"
};

var expected_selected = {
BoatYear: 2020,
BrandName: 'LINDER',
Price: [0, 138000] // min , max
}

boats = boats.filter(function(item) {
for (var key in selected) {
if (item[key] === undefined || item[key] != selected[key]) return false;
}
return true;
});

console.log(`Results: ${JSON.stringify(boats)}`);

最佳答案

  • 最简单的解决方案:只需硬编码所有字段


  • let boats = [
    {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
    {Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
    {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 396900, BrandName: null , BoatYear: 2020},
    {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 138700, BrandName: "HR" , BoatYear: 2020},
    {Price: 178900, BrandName: "HR" , BoatYear: 2020},
    {Price: 348900, BrandName: "HR" , BoatYear: 2020},
    {Price: 285800, BrandName: "HR" , BoatYear: 2020},
    {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 247900, BrandName: "SILVER" , BoatYear: 2020}
    ];

    const expected_selected = {
    BoatYear : 2020,
    BrandName: 'LINDER',
    Price : { min: 0, max: 138000 },
    }
    const filter_by = filters => item => {
    if (item.BoatYear === undefined || item.BoatYear !== filters.BoatYear ) return false
    if (item.BrandName === undefined || item.BrandName !== filters.BrandName ) return false
    if (item.Price < filters.Price.min || item.Price > filters.Price.max) return false
    return true
    }
    boats = boats.filter(filter_by(expected_selected))

    console.log(`Results: ${JSON.stringify(boats)}`);


  • 或者在任何地方使用 min/max


  • let boats = [
    {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
    {Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
    {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 396900, BrandName: null , BoatYear: 2020},
    {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 138700, BrandName: "HR" , BoatYear: 2020},
    {Price: 178900, BrandName: "HR" , BoatYear: 2020},
    {Price: 348900, BrandName: "HR" , BoatYear: 2020},
    {Price: 285800, BrandName: "HR" , BoatYear: 2020},
    {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
    ]

    const expected_selected = {
    BoatYear : { min: 2020 , max: 2020 },
    BrandName: { min: 'LINDER', max: 'LINDER' },
    Price : { min: 0 , max: 138000 },
    }
    const filter_by = filters => item => {
    for (var key in filters) {
    if (item[key] === undefined) return false
    if (item[key] < filters[key].min || item[key] > filters[key].max) return false
    }
    return true
    }
    boats = boats.filter(filter_by(expected_selected))

    console.log(`Results: ${JSON.stringify(boats)}`);


  • 或查看 selected 的类型字段(在这种情况下 Array.isArray ,在 {min,max} 的情况下,它将是 instanceof )


  • let boats = [
    {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
    {Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
    {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 396900, BrandName: null , BoatYear: 2020},
    {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 138700, BrandName: "HR" , BoatYear: 2020},
    {Price: 178900, BrandName: "HR" , BoatYear: 2020},
    {Price: 348900, BrandName: "HR" , BoatYear: 2020},
    {Price: 285800, BrandName: "HR" , BoatYear: 2020},
    {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
    ]

    const expected_selected = {
    BoatYear : 2020,
    BrandName: 'LINDER',
    Price : [ 0, 138000 ],
    }
    const filter_by = filters => item => {
    for (var key in filters) {
    if (item[key] === undefined) return false
    if (Array.isArray(filters[key])) {
    if(item[key] < filters[key][0] || item[key] > filters[key][1]) return false
    } else if (item[key] !== filters[key]) return false
    }
    return true
    }
    boats = boats.filter(filter_by(expected_selected))

    console.log(`Results: ${JSON.stringify(boats)}`);


  • 或者更好的是,使用 OOP


  • let boats = [
    {Price: 599900, BrandName: "FLIPPER", BoatYear: 2020},
    {Price: 97e3 , BrandName: "MICORE" , BoatYear: 2020},
    {Price: 189300, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 396900, BrandName: null , BoatYear: 2020},
    {Price: 334900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 138700, BrandName: "HR" , BoatYear: 2020},
    {Price: 178900, BrandName: "HR" , BoatYear: 2020},
    {Price: 348900, BrandName: "HR" , BoatYear: 2020},
    {Price: 285800, BrandName: "HR" , BoatYear: 2020},
    {Price: 186900, BrandName: "MICORE" , BoatYear: 2019},
    {Price: 276800, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 518900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 226900, BrandName: "MICORE" , BoatYear: 2020},
    {Price: 132600, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 137200, BrandName: "LINDER" , BoatYear: 2020},
    {Price: 366900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 365900, BrandName: "SILVER" , BoatYear: 2020},
    {Price: 247900, BrandName: "SILVER" , BoatYear: 2020},
    ]

    class MinMax {
    constructor(min, max) { this.min = min, this.max = max }
    check(val) { return val >= this.min && val <= this.max }
    }
    class Eq {
    constructor(val) { this.val = val }
    check(val) { return val === this.val }
    }
    var expected_selected = {
    BoatYear : new Eq(2020),
    BrandName: new Eq('LINDER'),
    Price : new MinMax(0, 138000)
    }
    const filter_by = filters => item => {
    for (var key in filters) {
    if (item[key] === undefined) return false
    if (filters[key].check(item[key]) === false) return false
    }
    return true
    }
    boats = boats.filter(filter_by(expected_selected))

    console.log(`Results: ${JSON.stringify(boats)}`);


    通过这种方式,您可以通过添加新类来扩展过滤器,而无需更改 filter_by功能。

    关于json - 如何过滤具有多个整数条件和键值的 JSON 对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61502469/

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