0

I have a JS object like this:

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

and an array of object like this:

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'
  },
];

I am trying to write a function fo filter myObj and only get the fields I can find in myArray.propKey.

this is what I tried so far with no luck:

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);
};

any idea how to do that please?

1
  • 2
    please add the wanted result. Commented Feb 16, 2020 at 9:50

3 Answers 3

1

You could iterate the array and map found properties.

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);

With keys

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);

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

Comments

0

Turn the myArray into a Set of propKeys first, then filter myObj by whether the key being iterated over is in the Set:

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);

1 Comment

If you want the entries and not just the keys, then remove the final .map so that you have an array of entries. (if you need an object instead, you can use Object.fromEntries to turn the array of entries into an object)
0

Another answer.

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)

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.