2

I'm gonna explain my problem. I don't have time to think more about that and I have a blocker and I can't figure it out so any help will be appreciated. For example, I have an array of objects (it can be array with 100+ elements):

  const arr = [
    { value: '1', id: 1, number: 1, other: 'example', data: '6' },
    { value: '2', id: 2, number: 2, other: 'example', data: '7' },
    { value: '3', id: 3, number: 3, other: 'example', data: '8' },
    { value: '4', id: 4, number: 4, other: 'example', data: '9' },
    { value: '5', id: 5, number: 4, other: 'example', data: '10' },
  ];

and in the other array I have strings which contain specific keys like that:

  const keys = ['value', 'id', 'number'];

and my problem is that I want to return the variable arr only contains objects based on values on the keys variable. Something like that:

 const arr = [
    { value: '1', id: 1, number: 1 },
    { value: '2', id: 2, number: 2 },
    { value: '3', id: 3, number: 3 },
    { value: '4', id: 4, number: 4 },
    { value: '5', id: 5, number: 4 },
  ];

I would like to have it dynamically because values in the keys variable are not constant and can be a value, other, data or only data or id and other etc.

2
  • Please post a minimal reproducible example of your attempt and say specifically where you're stuck. People will be glad to help. Commented Apr 16, 2020 at 10:34
  • Iterating over the array is a given, thus the issue is removing not needed keys from an object. You can either create a new object and set its props by iterating over keys, or use delete to remove them from the existing object. The former means you'll want to use arr.map() while the latter is a job for arr.forEach() Commented Apr 16, 2020 at 10:37

3 Answers 3

3

Create a function which will return based on keys array and use that function to map..

const arr = [
        { value: '1', id: 1, number: 1, other: 'example', data: '6' },
        { value: '2', id: 2, number: 2, other: 'example', data: '7' },
        { value: '3', id: 3, number: 3, other: 'example', data: '8' },
        { value: '4', id: 4, number: 4, other: 'example', data: '9' },
        { value: '5', id: 5, number: 4, other: 'example', data: '10' },
      ];

    const keys = ['value', 'id', 'number'];

    function pick(obj, keys){
        let result = {};
        for(let i=0; i<keys.length; i++){
            result[keys[i]] = obj[keys[i]];
        }
        return result;
    }

    let finalArr = arr.map( value => pick(value,keys));

    console.log(finalArr);
Sign up to request clarification or add additional context in comments.

Comments

2

You can do it by using Array.prototype.map() and a reduce.

const arr = [
    { value: '1', id: 1, number: 1, other: 'example', data: '6' },
    { value: '2', id: 2, number: 2, other: 'example', data: '7' },
    { value: '3', id: 3, number: 3, other: 'example', data: '8' },
    { value: '4', id: 4, number: 4, other: 'example', data: '9' },
    { value: '5', id: 5, number: 4, other: 'example', data: '10' },
];

const keys = ['value', 'id', 'number'];

const res = arr.map(item => keys.reduce((acc, key) => ({...acc, [key]: item[key]}), {}));

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

6 Comments

This is exactly what I needed. Thank you very much ;)
Sorry I didn't check that It creates more objects in the array and I needed everything in the one object. I have to change the acceptance of the answer but your answer is still good ;) Thank you very much again.
Sorry for your inconvinience. I've updated my code.
Thanks again, I like to reduce + map than for loop so I change it again :) Thanks!!
You are right. So If you do not mind I gave him the best answer.
|
0

If you're willing to use lodash (great util lib imo), you can do the following:

  import _ from 'lodash';

  const arr = [
    { value: '1', id: 1, number: 1, other: 'example', data: '6' },
    { value: '2', id: 2, number: 2, other: 'example', data: '7' },
    { value: '3', id: 3, number: 3, other: 'example', data: '8' },
    { value: '4', id: 4, number: 4, other: 'example', data: '9' },
    { value: '5', id: 5, number: 4, other: 'example', data: '10' },
  ];

  const keys = ['value', 'id', 'number'];

  const filteredArr = arr.map((row) => _.pick(row, keys))

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.