Home > Software engineering >  lodash Library k- only extract keys
lodash Library k- only extract keys

Time:05-18

I need a little help. I need to get an object from the console log and return only his keys using the lodash library, i tried

const ObjLetters = ({value}) => {
    return  _.findKey({value}) }
    console.log (ObjLetters{
        miki:"handsome",
        shon:"adorable"
        tomer:"man"})

im not aware of my problem, if its a syntax problem or maybe the wrong commands.

CodePudding user response:

Are you trying to obtain an array of keys in an object ?

If yes, try

console.log(Object.keys({
  miki: "handsome",
  shon: "adorable"
  tomer: "man",
}));

// output: ["miki", "shon", "tomer"]

CodePudding user response:

Ok, now my code looks like this:

const ObjLetters = ({object}) => {
    return  _.keys({object}) }
    console.log (ObjLetters({ miki:"handsome", shon:"adorable", tomer:"man"}))

but, instead of returning the keys, i get

'object'

CodePudding user response:

if you just want the keys you can use:

const ObjLetters = (object) => _.keys(object) 

console.log(ObjLetters({ miki:"handsome", shon:"adorable", tomer:"man"}))
// out: ["miki", "shon", "tomer"]

_ findKey find a key of the object with the values passed.

see this example from docs:

var users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
};

_.findKey(users, { 'age': 1, 'active': true });
// out: 'pebbles'
  • Related