简体   繁体   English

如何在 javascript 中深度嵌套的对象成员上使用 Ramda 的 omit() 函数

[英]How can I use Ramda's omit() function on a deeply nested object member in javascript

I have a situation where I want to remove any part of an object tree before flattening and exporting to CSV.我有一种情况,我想在展平和导出到 CSV 之前删除对象树的任何部分。 Ramda is my choice library for FP in JS, but I noticed the R.omit() function works at only one level deep of the target object. Ramda是我在 JS 中为FP选择的库,但我注意到R.omit()函数仅在目标对象的一层深度起作用。 How can I make it such that I can do the following?我怎样才能做到以下几点?

const R = require('ramda');

const obj = {
    id: 1,
    name: 'me',
    audience_sizes: {
        fb: 500,
        dfp: 2000,
        apn: 1800
    }
};

console.log(JSON.stringify(R.omit(['id', 'audience_sizes.fb'], obj)));

I would expect the following result:我希望得到以下结果:

{"name":"me","audience_sizes":{"dfp":2000, "apn": 1800}}

我认为Lenses是一种更实用的方法。

R.over(R.lensProp('audience_sizes'), R.omit(['fb']), R.omit(['id'], obj));

DissocPath look like what you are looking for. DissocPath看起来像您要找的东西。 Use it or lenses for more complex deep updates. 使用它或镜头进行更复杂的深度更新。

I solved this problem by using the following function. 我通过使用以下功能解决了这个问题。 How can I avoid the Use of eval() in handling deep reference of object members in the solution below? 在下面的解决方案中,如何避免使用eval()处理对象成员的深层引用? Basically, I'm looking for a better way of implementing this function: 基本上,我正在寻找一种更好的方法来实现此功能:

//This is a supplemental function to Ramda's omit(), which allows deep key referencing
export const omitDeep = R.curry((keys, obj) => {
  if(!keys || keys.length===0) return obj;
  let newObj = Object.assign({}, obj);
  keys.forEach( key => {
      let keyParts = key.split('.');
      if(keyParts.length < 2) {
          newObj = R.omit(keyParts, newObj);
          return;
      }
      let target = newObj[keyParts[0]];
      let objString = `newObj.${keyParts[0]}`;
      for(let i=1; i < keyParts.length - 1; i++){
          target = target[keyParts[i]];
          objString += `.${keyParts[0]}`;
      }
      const subj = keyParts[keyParts.length-1];
      target = R.omit([subj], target);
      eval(`${objString} = target`);
   });
   return newObj;
});

Thanks! 谢谢!

Based on advice by more experienced Ramda users, I've refactored my omitDeep function to read as follows: 根据更多经验丰富的omitDeep用户的建议,我将omitDeep函数重构如下:

//This is a supplemental function to Ramda's omit(), which allows deep key referencing
export const omitDeep = R.curry((keys, obj) => {
  if(!keys || keys.length===0) return obj;
  let newObj = R.clone(obj);
  keys.forEach( key => {
      let keyParts = key.split('.');
      newObj = R.dissocPath(keyParts, newObj);
  });    
  return newObj;
});

Thanks @Buggy and @Ebuall! 谢谢@Buggy和@Ebuall!

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

相关问题 我如何使用Ramda深度映射对象 - How can I deeply map over object with Ramda 如何在 javascript 中创建深度嵌套的 object? - how can create a deeply nested object in javascript? 如何将深度嵌套对象的属性总和移动或计算到对象数组中的顶层 - how can I either move or calculate the sum of a deeply nested object's properties to the top level in an object array 在javascript中替换深度嵌套在对象中的函数 - Replacing a function deeply nested inside an object in javascript 如何遍历JavaScript对象的深层嵌套属性? - How do I loop through deeply nested properties of a JavaScript object? 如何遍历javascript中的深度嵌套对象数组? - How can I iterate through array of deeply nested objects in javascript? 如何在谷歌实时数据库中定位深度嵌套的 object? - How can I target a deeply nested object in google realtime database? 如何在 Javascript 中更新深度嵌套的 object? - how to update a deeply nested object in Javascript? 使用 Ramda 或 vanillaJS 将任意深度嵌套的值转换为对象键 - Convert an arbitrarily deeply nested value into an object key using Ramda or vanillaJS 如何从包含字符串、对象和 arrays 的 Javascript object 中检索动态指定、任意和深度嵌套的值? - How can I retrieve dynamically specified, arbitrary and deeply nested values from a Javascript object containing strings, objects, and arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM