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

如何在Javascript中通过数组从对象筛选中获取属性

  •  0
  • user3174311  · 技术社区  · 4 年前

    我有一个像这样的JS对象:

    const myObj = {
      '$prop1': 1,
      '$prop2': 2,
      '$prop3': 3
    };
    

    const myArray = [
      {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop1'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop2'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop5'
      },
    ];
    

    我试图编写一个函数来过滤myObj,并且只获取myArray.propKey中可以找到的字段。

    这是我迄今为止没有成功的尝试:

    export const filterVariantContentByColumns = (myObj, myArray) => {
      const objArray = Object.keys(myObj).map(i => myObj[i]);
      return objArray.filter(value => myArray.includes(value.key_name));
    
      console.log(res);
    };
    

    你知道怎么做吗?

    3 回复  |  直到 4 年前
        1
  •  1
  •   CertainPerformance    4 年前

    转动 myArray 变成一套 propKey myObj 通过迭代的键是否在集合中:

    const myObj = {
      '$prop1': 1,
      '$prop2': 2,
      '$prop3': 3
    };
    
    const myArray = [
      {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop1'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop2'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop5'
      },
    ];
    const haveKeys = new Set(myArray.map(({ propKey }) => propKey));
    const output = Object.entries(myObj)
      .filter(([key]) => haveKeys.has(key))
    console.log(output);
        2
  •  2
  •   Nina Scholz    4 年前

    您可以迭代数组并映射找到的属性。

    const
        myObj = { $prop1: 1, $prop2: 2, $prop3: 3 },
        myArray = [{ prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop1' }, { prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop2' }, { prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop5' }],
        result = myArray.flatMap(({ propKey }) => propKey in myObj ? myObj[propKey] : []);
    
    console.log(result);

    带钥匙

    const
        myObj = { $prop1: 1, $prop2: 2, $prop3: 3 },
        myArray = [{ prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop1' }, { prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop2' }, { prop1: 'some stuff', prop2: 'some other stuff', propKey: '$prop5' }],
        result = Object.fromEntries(myArray.flatMap(({ propKey }) => propKey in myObj ? [[propKey, myObj[propKey]]] : []));
    
    console.log(result);
        3
  •  0
  •   sunfy    4 年前

    const myObj = {
      '$prop1': 1,
      '$prop2': 2,
      '$prop3': 3,
      '$prop4': 4
    };
    const myArray = [
      {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop1'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop2'
      },
            {
        prop1: 'some stuff',
        prop2: 'some other stuff',
        propKey: '$prop5'
      },
    ];
    
    let propKeys = myArray.map(ele => ele.propKey)
    
    let obj = Object.keys(myObj).reduce((ret, key) => {
      if (propKeys.indexOf(key) > -1) {
        ret[key] = myObj[key]
      }
      return ret
    }, {})
    console.log(obj)