0

I have the following structure:

{
  cp: {
    id: 8,
    login: "something@gmail.com",
    name: "Yuri",
    age: 19,
    ...
  },
  admin: {
    id: 19,
    login: "test",
    password: "somehash",
    ...
  }
}

I wish this object to have id and login properties only, like:

{
  cp: {
    id: 8,
    login: "something@gmail.com"
  },
  admin: {
    id: 19,
    login: "test"
  }
}

Is there way to do this using lodash ? I have read this answer but I don't know how to do the same operations with my structure. Please, help.

Denys Rybkin
  • 637
  • 1
  • 6
  • 18

3 Answers3

1

You can use transform and pick to convert your object.

const obj = { cp: { id: 8, login: "something@gmail.com", name: "Yuri", age: 19, }, admin: { id: 19, login: "test", password: "somehash", }, },
      result = _.transform(obj, (r, v, k) => {
        r[k] = _.pick(v, ['id', 'login']);
        return r;
      }, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

I know you specifically searched for a solution using lodash, but you could also accomplish the same like this.

const data = {
  cp: {
    id: 8,
    login: "something@gmail.com",
    name: "Yuri",
    age: 19,
  },
  admin: {
    id: 19,
    login: "test",
    password: "somehash",
  }
}

const result = Object.fromEntries(Object.entries(data)
  .map(([k, {id, login}]) => [k, {id, login}]));

console.log(result);

It maps through the objects entries and returns only the properties you need as new entries to recreate the object from.

axtck
  • 3,707
  • 2
  • 10
  • 26
-1

i do not test this, code, but hope it help you

var obj = {
  cp: {
    id: 8,
    login: "something@gmail.com",
    name: "Yuri",
    age: 19,
    ...
  },
  admin: {
    id: 19,
    login: "test",
    password: "somehash",
    ...
  }
}
var model = {
   id:null,
   login:null
};
Object.keys(obj).forEach(e=>obj[e] = _.pick(obj[e], _.keys(model)))

update without lodash

var obj = {
                cp: {
                    id: 8,
                    login: "something@gmail.com",
                    name: "Yuri",
                    age: 19,
                },
                admin: {
                    id: 19,
                    login: "test",
                    password: "somehash",
                }
            }
            const keeps = ["id","login"];
            Object.keys(obj).forEach(e=>{
                Object.keys(obj[e]).forEach(f=>{
                    if(!keeps.includes(f)){
                        delete obj[e][f]
                    }
                })
            })
tuan nguyen
  • 386
  • 2
  • 8
  • 1. There is no need to create another object `model` and use _.keys(model) because you can just put an array: `['id', 'login']`. 2. I'd like to use only `lodash` to do what I want, so that I do think that usage of naitve `Object.keys()` is a good idea... Is there better way to do this using `lodash` only? – Denys Rybkin Jun 09 '21 at 09:12
  • @LeonardoDiPierro there is another way to to this without lodash, use key word: "delete" to delete property from object – tuan nguyen Jun 09 '21 at 09:15
  • Do you propose to remove each property one by one, even if their number can reach up to 10-15? – Denys Rybkin Jun 09 '21 at 09:20