Lodash深层过滤器和地图嵌套对象 [英] Lodash deep filter and map nested object

查看:290
本文介绍了Lodash深层过滤器和地图嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的对象,该对象当前正在大量不同的功能中循环通过,以获取具有名称的项目或设置新值.目前,我正在使用lodash _.foreach和._map来完成这两个动作.我知道lodash具有_.filter之类的功能,但是我无法使其与我拥有的对象一起使用.

I have below object which I am currently looping through in number of different functions to get an item with name or setting a new value. Currently I am using lodash _.foreach and ._map to achieve both actions. I know lodash has function such as _.filter however I am unable to get it to work with the object that I have.

let data =  [
  {
     "panels" : [
        {
          "title": "Panel 1",
          "items" : [
              {
                 "name": item1,
                 "value": 1
              }
              {
                 "name": item2,
                 "value": 2
              }
           ]
         },
         {
            "title": "Panel 2",
             "items" : [
              {
                 "name": item3,
                 "value": 3
              }
              {
                 "name": item4,
                 "value": 5
              }
           ]
         }
     ]
  }

  {
     "panels" : [
        {
          "title": "Panel 3"
          "items" : [
              {
                 "name": item5,
                 "value": 5
              }
              {
                 "name": item6,
                 "value": 6
              }
           ]
        }
     ]
  }  

  {
     "panels" : [
        {
          "title": "Panel 4"
          "items" : [
              {
                 "name": item7,
                 "value": 7
              }
              {
                 "name": item8,
                 "value": 8
              }
           ]
        }
     ]
  }    
]


 // Get item from the object with given name
 getItem: function(name) {
   let item = false;
   _.forEach(data, function (group) {
       _.forEach(group.panels, function (panel) {
           _.forEach(panel.items, function (item) {
               if (item.name == name) {
                   filterItem = item;
                   return false;
               }
            });
       });
   }); 
   return item ;
 },

 //Set item new value
 updateItemValue (name, value) {
    _.map(data, function(group) {
        _.map(group.panels, function(panel) {
            _.map(panel.items, function(item) {
                if( item.name === name ) {                      
                   item.value = value;                   
                }
            });
        });
    });
 }

还有其他方法可以更有效地实现这一目标吗?

Is there any other way I can achieve this in more efficient way ?

推荐答案

您可以使用嵌套的

You could use nested for...of loops with destructuring. If an item has the provided name, return the item. Apply the same approach to updateItemValue as well

const data=[{panels:[{title:"Panel 1",items:[{name:"item1",value:1},{name:"item2",value:2}]},{title:"Panel 2",items:[{name:"item3",value:3},{name:"item4",value:4}]}]},{panels:[{title:"Panel 3",items:[{name:"item5",value:5},{name:"item6",value:6}]}]}];

function getItem(name) {
  for (const { panels } of data)
    for (const { items } of panels)
      for (const o of items)
        if (o.name === name)
          return o

  return; // if no match is found
}

function updateItemValue(name, value) {
  for (const { panels } of data)
    for (const { items } of panels)
      for (const o of items)
        if (o.name === name) {
          o.value = value
          return;
        }
}

console.log(getItem('item1'))
console.log(getItem('item2'))
console.log(getItem('doesntexist'))

updateItemValue('item3', 33) // update the value
console.log(getItem('item3')) // gets the updated value

这篇关于Lodash深层过滤器和地图嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆