【问题标题】:How to filter an array of objects by the element inside an inner array - Functional Programming Javascript如何通过内部数组中的元素过滤对象数组 - JavaScript 函数式编程
【发布时间】:2017-04-26 19:26:27
【问题描述】:

我仍在通过函数式编程:.forEach()、.map()、.filter()、.reduce()。

我正在应对新的挑战。

var bikes = [
   {name: 'Cinelli Bolt', price: '2000', age: 1, singlespeed: 'yes', features: ['tubeless tires', 'disk brakes', 'carbon frame']},
   {name: 'Cinelli Mash', price: '1700', age: 3, singlespeed: 'yes', features: ['gatorskin tyres', 'sram drivetrain', 'steel frame']},
   {name: 'Specialized Langster', price: '1000', age: 1, singlespeed: 'no', features: ['two speed enclosed hub', 'bullbars', 'carbon frame']}
 ]; 

我正在尝试通过特定功能“碳框架”过滤阵列。我正在考虑使用过滤器,但我对在 key.value 对中调用该功能感到困惑。

var justCarbonBikes = bikes.filter(function(bike) {
  return bike.features['carbon frame'];
});

预期结果应该修改原始数组:

var bikes = [
   {name: 'Cinelli Bolt', price: '2000', age: 1, singlespeed: 'yes', features: ['tubeless tires', 'disk brakes', 'carbon frame']},
   {name: 'Specialized Langster', price: '1000', age: 1, singlespeed: 'no', features: ['two speed enclosed hub', 'bullbars', 'carbon frame']}
 ]; 

【问题讨论】:

    标签: javascript arrays


    【解决方案1】:

    你快到了。通过Array#indexOf检查索引,您可以将其用作返回值。

    return bike.features.indexOf('carbon frame') + 1;
    

    var bikes = [{ name: 'Cinelli Bolt', price: '2000', age: 1, singlespeed: 'yes', features: ['tubeless tires', 'disk brakes', 'carbon frame'] }, { name: 'Cinelli Mash', price: '1700', age: 3, singlespeed: 'yes', features: ['gatorskin tyres', 'sram drivetrain', 'steel frame'] }, { name: 'Specialized Langster', price: '1000', age: 1, singlespeed: 'no', features: ['two speed enclosed hub', 'bullbars', 'carbon frame'] }],
        justCarbonBikes = bikes.filter(function(bike) {
            return bike.features.indexOf('carbon frame') + 1;
        }),
        bikesWithoutCarbon = bikes.filter(function(bike) {
            return bike.features.indexOf('carbon frame') === -1;
        });
    
    console.log(justCarbonBikes);
    console.log(bikesWithoutCarbon);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    Array#includes 作为返回值的ES6。

    var bikes = [{ name: 'Cinelli Bolt', price: '2000', age: 1, singlespeed: 'yes', features: ['tubeless tires', 'disk brakes', 'carbon frame'] }, { name: 'Cinelli Mash', price: '1700', age: 3, singlespeed: 'yes', features: ['gatorskin tyres', 'sram drivetrain', 'steel frame'] }, { name: 'Specialized Langster', price: '1000', age: 1, singlespeed: 'no', features: ['two speed enclosed hub', 'bullbars', 'carbon frame'] }],
        justCarbonBikes = bikes.filter(b => b.features.includes('carbon frame')),
        bikesWithoutCarbon = bikes.filter(b => !b.features.includes('carbon frame'));
    
    console.log(justCarbonBikes);
    console.log(bikesWithoutCarbon);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 我怎么能否定这个功能:所有没有“碳纤维车架”功能的自行车?
    • 你可以检查结果或否定它,就像包含一样。
    【解决方案2】:

    这是一种解决方案,您可以遍历过滤器函数中的特征,如果找到碳框则返回

    var justCarbonBikes = bikes.filter(function(bike) {
      for(let i of bike.features){
        if(i == 'carbon frame'){
          return bike
        }
      }
    });
    

    【讨论】:

    • 很好,我更喜欢这个来过滤,因为你可以指定一个特定的标准:如果我想要所有不是碳纤维车架的自行车。 ?
    猜你喜欢
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    相关资源
    最近更新 更多