简体   繁体   English

从文档/对象中删除特定的键值对

[英]Removing specific key-value pairs from a document/object

I have a document that looks like something like this: 我有一个看起来像这样的文档:

{
  name: "Johnny Boy",
  age: 24,
  hobbies: ["fencing", "chess", "raves"],
  _createdAt: 2015-05-15T18:12:26.731Z,
  _createdBy: "JohnnyBoy",
  _id: mQW4G5yEfZtsB6pcN
}

My goal is to return everything that doesn't start with an underscore , and format it a bit differently so I will end up with this: 我的目标是返回不以下划线开头的所有内容 ,并对其格式进行一些不同的调整,因此我将得出以下结论

[
  {
    fieldName: "name",
    value: "Johnny Boy"
  },
  {
    fieldName: "age",
    value: 24
  },
  {
    fieldName: "hobbies",
    value: ["fencing", "chess", "raves"]
  }
]

My initial solution was to run it through the _.map function of the Underscore library (which has nothing to do with my wanting to remove underscores specifically...) and then using lastIndexOf to figure out which keys begin with an underscore: 我最初的解决方案是通过_.map库的_.map函数运行它(与我要删除下划线无关...),然后使用lastIndexOf找出哪些键以下划线开头:

var listWithoutUnderscores = _.map(myList, function(val, key) {
  if (key.lastIndexOf("_", 0) === -1)
    return {fieldName: key, value: val}
  return null
})

However, this will literally return null instead of the fields that began with _ in the returned array: 但是,这实际上将返回null而不是返回数组中以_开头的字段:

[
  ...
  {
    fieldname: "hobbies",
    value: ["fencing", "chess", "raves"]
  },
  null,
  null,
  null
]

I want to remove them completely , ideally within the map function itself, or else by chaining it through some kind of filter but I don't know which one is fastest in this case. 我想完全删除它们 ,最好是在map函数本身中删除它们 ,或者通过某种过滤器将其链接起来,但是我不知道在这种情况下哪一个最快。

Underscore also comes with an array method compact which will remove all falsey and null values from an array: Underscore还带有一个数组方法compact ,它将从数组中删除所有false和null值:

_.compact([0, 1, false, 2, '', null, 3]);
=> [1, 2, 3]

You could just call _.compact(array) on the array after your map. 您可以在映射后在数组上调用_.compact(array)

You can use reduce for this: 您可以为此使用reduce

var listWithoutUnderscores = _.reduce(myList, function(list, val, key) {
  if (key.lastIndexOf("_", 0) === -1){
    list.push( {fieldName: key, value: val});
  }
  return list;
}, []);

You can use pick and pass a predicate to get the valid keys and then map across those fields: 您可以使用选择并传递谓词来获取有效键,然后在这些字段之间进行映射

var validKey = function(value, key){
    return _.first(key) != '_';
}

var createField = function(value, key){
    return {
        fieldname: key,
        value: value
    }
}

var result = _.chain(data)
    .pick(validKey)
    .map(createField)
    .value();

 var data = { name: "Johnny Boy", age: 24, hobbies: ["fencing", "chess", "raves"], _createdAt: '2015-05-15T18:12:26.731Z', _createdBy: "JohnnyBoy", _id: 'mQW4G5yEfZtsB6pcN' } var validKey = function(value, key){ return _.first(key) != '_'; } var createField = function(value, key){ return { fieldname: key, value: value } } var result = _.chain(data) .pick(validKey) .map(createField) .value(); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

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

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