【问题标题】:Lodash : Filter with needed attributes onlyLodash :仅使用所需属性进行过滤
【发布时间】:2017-05-11 08:30:11
【问题描述】:
 [
  {
    "Office_Id": 100,
    "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  },
  {
    "Office_Id": 200,
     "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  },
  {
    "Office_Id": 300,
     "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  }
]

如何仅使用 Office_Id 和 OfficeName 进行过滤

【问题讨论】:

  • 你使用了filter这个词——我不认为它意味着你认为的那样。您只想要某些 office_id/officename,还是想要所有对象但只有这两个属性?
  • 你的意思是你想剥离所有其他的东西并只保留所有对象中的这两个属性?另外,分享你迄今为止尝试过的东西..

标签: javascript angularjs lodash


【解决方案1】:

没有 lodash(plain javascript),像这样:
(如果你想去掉两个属性之外的所有内容)

let list = [
  {
    "Office_Id": 100,
    "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  },
  {
    "Office_Id": 200,
     "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  },
  {
    "Office_Id": 300,
     "Address1": "xxxxx",
    "Address2": "",
    "City": "ANNISTON",
    "District_Id": 1277,
    "OfficeName": "test"
  }
];

let newList = list.map(function(currentItem){
    return {"Office_Id": currentItem.Office_Id, "OfficeName": currentItem.OfficeName};
});
console.info(newList);

// Tested on Win7 64bit Chrome 57+

数组对象的map 函数将创建一个新数组,函数返回的值是过去的(在本例中为x => {return {"Office_Id": x.Office_Id, "OfficeName": x.OfficeName};})。该功能的详细信息可以在这里找到MDN Reference

作为 cmets 中的状态,您可以使用 lambda 函数、解构和优化的文字符号来最小化代码。 (但在使用新闻 javascript 版本支持的每个功能之前,请检查兼容性

这里有一个简短的版本:
list.map(({Office_Id, OfficeName}) => ({Office_Id, OfficeName})});

更新,使用 lodash:

let list = [
      {
        "Office_Id": 100,
        "Address1": "xxxxx",
        "Address2": "",
        "City": "ANNISTON",
        "District_Id": 1277,
        "OfficeName": "test"
      },
      {
        "Office_Id": 200,
         "Address1": "xxxxx",
        "Address2": "",
        "City": "ANNISTON",
        "District_Id": 1277,
        "OfficeName": "test"
      },
      {
        "Office_Id": 300,
         "Address1": "xxxxx",
        "Address2": "",
        "City": "ANNISTON",
        "District_Id": 1277,
        "OfficeName": "test"
      }
    ];


let newList = _.map(list, function(value){
  return { "Office_Id": value.Office_Id,"OfficeName": value.OfficeName};
});

console.info(newList);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-compat/3.10.2/lodash.js"></script>

这里是link to the Documentation of the lodash map function

【讨论】:

  • list.map(({Office_Id, OfficeName}) => {return {Office_Id, OfficeName};});
  • @Jamiec 没有。
  • 是的,更好,但我希望这个例子不要太简约。 :-D
  • @ShriyaR 我用 lodash 进行了更新,但如果它与 nativ 函数一起使用,请使用库。
  • @winner_joiner 谢谢 :)
猜你喜欢
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 2018-09-27
  • 2013-06-18
  • 2015-11-05
  • 2017-01-10
  • 1970-01-01
  • 2021-07-14
相关资源
最近更新 更多