简体   繁体   中英

lodash Library k- only extract keys

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.

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"]

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'

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM