1

I want to include all the keys of an object present in an array and discard the rest, For e.g., if I have an array,

const arr = ['apple', 'milk', 'bread', 'coke'];

and an object,

const dummy = {
    apple: {
        category: 'fruit'
    },
    banana: {
        category: 'fruit'
    },
    potato: {
        category: 'vegetable'
    },
    dairy: {
        milk: {
          type: 'A2'
        }
    },
    bakery: {
        bread: {
            type: 'brown'
        }
    },
    beverage: {
      cold_drink: {
        coke: {
          type: 'diet'
        },
        beer: {
        }
      }
    }
}

I want my resultant object to contain the keys from arr only, be it direct keys or deeply nested. So for above case, my resultant object will look like,

{
    apple: {
        category: 'fruit'
    },
    dairy: {
        milk: {
          type: 'A2'
        }
    },
    bakery: {
        bread: {
            type: 'brown'
        }
    },
    beverage: {
      cold_drink: {
        coke: {
          type: 'diet'
        },
        beer: {}
      }
    }
}

I am trying to solve this via recursion, but not able to get the output correctly. Below is my code, could you please help me with where I am going wrong?

function fetchResultantObject(object, key, result) {
  if(typeof object !== 'object')
    return null;
  for(let objKey in object) {
    if(key.indexOf(objKey) > -1) {
      result[objKey] = object[objKey];
    } else {
      result[objKey] = fetchValueByKey(object[objKey], key, result);
    }
  }
  return result;
}

console.log(fetchResultantObject(dummy, arr, {}));
2
  • 1
    If the object with property "coke", also has another property "beer" (and associated object), then should "beer" be included or excluded? Your example could be extended to cover more varieties to better understand what you want. Commented May 11, 2022 at 21:23
  • @trincot Yes, beer will be included in that case. Commented May 11, 2022 at 21:42

1 Answer 1

2

You could filter the entries and check if the (nested) objects contains a wanted key.

const
    hasKey = object => object
        && typeof object === 'object'
        && (keys.some(k => k in object) || Object.values(object).some(hasKey)),
    keys = ['apple', 'milk', 'bread', 'coke'],
    data = { apple: { category: 'fruit' }, banana: { category: 'fruit' }, potato: { category: 'vegetable' }, dairy: { milk: { type: 'A2' } }, bakery: { bread: { type: 'brown' } }, beverage: { cold_drink: { coke: { type: 'diet' } } } },
    result = Object.fromEntries(Object
        .entries(data)
        .filter(([k, v]) => hasKey({ [k]: v }))
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.