【问题标题】:Search object inside array using object nested property and its value [duplicate]使用对象嵌套属性及其值在数组内搜索对象[重复]
【发布时间】:2020-07-04 05:03:28
【问题描述】:

如何按属性名称和值过滤我的对象数组?

例如,如果对象的属性aa.town 是爱丁堡

,我必须过滤对象

我需要使用作为参数传递的任何对象文字进行过滤 - 我不想硬编码该值。

到目前为止我尝试过的是

export function objectFilter(people, 'aa.town', 'Edinburgh'); {
    return objects.filter(item => item[keysString] == searchString);
}

这是一个人员数组:

export const people = [
  {
    a: 'Jim',
    b: 'male',
    c: '1971',
    aa: {
      town: 'London',
    },
  },
  {
    a: 'Bob',
    b: 'male',
    c: '1971',
    aa: {
      town: 'Edinburgh',
    },
  },

【问题讨论】:

    标签: javascript


    【解决方案1】:

    使用Filter方法

    (更新答案)

    function objectFilter(people, input) {
      return people.filter(item => "town" in item.aa && item.aa.town === input);
    }
    

    const people = [
      {
        a: "Jim",
        b: "male",
        c: "1971",
        aa: {
          town: "Edinburgh"
        }
      },
    
      {
        a: "Bob",
        b: "male",
        c: "1971",
        aa: {
          town: "London"
        }
      },
      {
        a: "Tom",
        b: "male",
        c: "1951",
        aa: {
          city: "Edinburgh"
        }
      }
    ];
    
    function objectFilter(people, input) {
      return people.filter(item => "town" in item.aa && item.aa.town === input);
    }
    
    console.log(objectFilter(people, "Edinburgh"));

    【讨论】:

    • 我想根据作为参数传递的字符串进行过滤,而不是对值进行硬编码
    • @Bomber,我已经更新了满足“爱丁堡”财产和城镇要求的答案
    • aa.town 可以是任何值 - 我想过滤参数是什么,它可以是任何对象文字。不对值进行硬编码。
    • @Bomber,请重新写清楚你的问题。这对我有帮助。
    【解决方案2】:

    从字符串“aa.town”中获取对象值obj.aa.town 我们可以像这样使用reduce函数

    nestedProp.split('.').reduce((a,b)=>a[b], item)
    

    function objectFilter(objects, nestedProp, propValue){
    return objects.filter(item =>        nestedProp.split('.').reduce((a,b)=>a[b], item) == propValue);
    }
    const people = [
      {
        a: 'Jim',
        b: 'male',
        c: '1971',
        aa: {
          town: 'London',
        },
      },
      {
        a: 'Bob',
        b: 'male',
        c: '1971',
        aa: {
          town: 'Edinburgh',
        },
      }
    ];  
    console.log(objectFilter(people, 'aa.town', 'Edinburgh'));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案3】:

      我希望这可以加快速度并适用于您想要的。

      编辑:我已经测试过这个问题。这没有树的深度限制,并且具有良好的性能影响。

      /* set mode = 0 for root object retval,
         set mode = 1 for first object retval,
         otherwise for last object retval
      
         eg. mode2 : for key = "aa.town" and value = "Edinburgh" will return { town: "Edinburgh" }, and so on.
         eg. mode1 : for key = "bb.gen.OO7" and value = "Gen 7" will return { bb: { gen: { OO7: "Gen 7" } } }, and so on.
      */
      export function getObject(array, key, value, mode = 1) {
          if (typeof key !== "string" || key.trim() === "") return;
      
          const gen7 = (_a, _key) => { return _a[_key] };
      
          const getItem= (_object, _keys) => {
              let i = 0, n = _keys.length-1, stack = _object;
              while (i < n && (stack = gen7(stack, _keys[i++])) !== undefined);
              return stack;
          };
      
          let keys = key.split("."),
              first = keys[0],
              last = keys[keys.length-1],
              i = 0, len = array.length, obj;
      
          while (i < len && ((obj = getItem(array[i], keys)) === undefined || obj[last] !== value)) i++;
      
          if (i < len) return mode === 0 ? array[i] : mode === 1 ? array[i][first] : obj;
      }
      
      const people = [
      {
          a: 'Jim',
          b: 'male',
          c: '1971',
          aa: {
              town: 'London',
          },
          bb: {
              gen: {
                  OO7: 'Gen 7'
              }
          }
      },
      {
          a: 'Bob',
          b: 'male',
          c: '1971',
          aa: {
              town: 'Edinburgh',
          },
        }
      ]; 
      

      【讨论】:

        猜你喜欢
        • 2018-01-16
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 2011-02-24
        • 1970-01-01
        • 2016-10-27
        • 2021-12-04
        • 1970-01-01
        相关资源
        最近更新 更多