0

I have Object below which I need to convert into below object format but can its possible to do that?

  products = {
      color: 'red',
      type: '2',
      status: 'true'
  }

converted object

  const filters = {
    color: color => color === 'red',
    type: type => type === '2' ,
    status: status => status === 'true',
  };
2
  • 1
    And the problem is? Iterate over the properties and replace the values with functions that test for the original value. Commented Jan 25, 2021 at 11:23
  • Can you please provide your attempt? (and as previously mentioned, what you're struggling with) Commented Jan 25, 2021 at 11:26

3 Answers 3

1

You can do this type of operation:

let products = {
  color: 'red',
  type: '2',
  status: 'true'
};
let filters = {};
Object.entries(products).forEach(e => {
  filters[e[0]] = f => f === products[e[0]];
});

Where filters is the object you expect.

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

1 Comment

Why Object.entries() when you only use the key part?
1

You can grab the product object's entires using Object.entries():

[["color", "red"], ["type", "2"], ["status", "true"]]

Once you have the entries, you can map over each inner array (ie: each key-value pair) using .map(). For each inner array, you can use destructuring assignment ([key, value]) => to extract both the first and second elements (ie: the key and the value) from the inner array. You can then return a new array, which contains your key in the first index, and a function as a value that checks for equality against the current value and the input argument to the function:

[
  [ "color", arg => arg === val ], 
  [ "type", arg => arg === val ], 
  [ "status", arg => arg === val ] 
]

The above inner arrays are still in the form of [key, value]. As this is the case, you can use Object.fromEntries() on the above array to convert it into an. object:

const products = { color: 'red', type: '2', status: 'true' };

const filters = Object.fromEntries(Object.entries(products).map(([key, val]) => [
  key, arg => arg === val 
]));

console.log(res);

The argument name doesn't match the name of the key, but that shouldn't matter as the argument is a variable

Comments

0

You could loop over using Object.entries(), and add the comparison functions to the filters object.

Here is a working snippet, along with some test cases:

const products = {
  color: 'red',
  type: '2',
  status: 'true'
};

let filters = {};

for (const [k, v] of Object.entries(products)) {
  filters[k] = k => k === v;
}

console.log(filters);
console.log(filters['color']('red'));
console.log(filters['color']('blue'));
console.log(filters['type']('2'));
console.log(filters['type']('1'));
console.log(filters['status']('true'));
console.log(filters['status']('false'));

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.