简体   繁体   English

如何在使用 Lodash 时获取特定的键值

[英]How to get particular key values in using Lodash

I have array of objects like following.我有像下面这样的对象数组。

[{
  "key": "remember_tip",
  "description": "some remember description"
}, {
  "key": "logout_tip",
  "description": "some logout description"
}, {
  "key": "notremember_tip",
  "description": "some not remember description"
},{
  "key": "backgroundOff",
  "description": "some backgroundOff description"
},
{
   "key": "backgroundOn",
      "description": "some backgroundOn description"
    },
 ..];

I have methods like following.我有如下方法。

someMethod = (variable) => {

  if (variable === remember) {
    this.rememberHandler()
  } else if (variable === logout) {
    this.logoutHandler()
  } else if (variable === notremember) {
    this.notrememberHandler()
  }else if (variable === backgroundoff) {
    this.backgroundoffHandler()
  }

}

rememberHandler = () => {
  //showpopup with remember_tip description
}

logoutHandler = () => {
  //showpopup with logout_tip description
}

notrememberHandler = () => {
  //showpopup with notremember_tip description
}
    backgroundoffHandler = () => {
  //showpopup with backgroundOff description
}

You can use the current structure to get the description values.您可以使用当前结构来获取描述值。 Use _.find() to get the object, with the requested key, and get the value with _.get() :使用_.find()来获取对象,用请求的密钥,并获得同值_.get()

 const { flow, partialRight: pr, find, get } = _ const getFrom = arr => flow( key => find(arr, { key }), pr(get, 'description'), ) const tips = [{ "key": "remember_tip", "description": "some remember description" }, { "key": "logout_tip", "description": "some logout description" }, { "key": "notremember_tip", "description": "some not remember description" }] const getFromTips = getFrom(tips) const result = getFromTips('remember_tip') console.log(result)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.14/lodash.js"></script>

However, this is a lot of code for something quite simple.然而,对于一些非常简单的事情来说,这是很多代码。 Create a Map of key to description from the array.从数组创建一个键到描述的映射。 When the show method is called, it gets the description from the Map, and displays the popup:当调用show方法时,它会从 Map 获取description ,并显示弹出窗口:

 const tips = [{ "key": "remember_tip", "description": "some remember description" }, { "key": "logout_tip", "description": "some logout description" }, { "key": "notremember_tip", "description": "some not remember description" }, { "key": "backgroundOff", "description": "some backgroundOff description" }, { "key": "backgroundOn", "description": "some backgroundOn description" } ]; const tipsMap = new Map(tips.map(({ key, description }) => [key, description])) const showpopup = console.log // demo show popup const show = popUp => { const description = tipsMap.get(popUp); if (description) showpopup(description) } show('logout_tip') show('backgroundOff')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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