用Lodash省略嵌套属性

时间:2015-12-20 00:19:33

标签: javascript lodash

我正在尝试删除以下对象中的属性 public float xForceToAdd; public float yForceToAdd; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag == "Player") { //Store the vector 2 of the location where the initial hit happened; Vector2 initialHitPoint = new Vector2 (other.gameObject.transform.position.x, other.gameObject.transform.position.y); float xForce = 0; float yForce = 0; //Grab our collided with objects rigibody Rigidbody2D rigidForForce = other.gameObject.GetComponent<Rigidbody2D>(); //Determine left right center of X hit if (initialHitPoint.x > (this.transform.position.x + (this.transform.localScale.x /3))) { xForce = 1; } else if (initialHitPoint.x < (this.transform.position.x - (this.transform.localScale.x /3))) { xForce = -1; } else { xForce = 0; } if (initialHitPoint.y > (this.transform.position.y + (this.transform.localScale.y /3))) { yForce = 1; } else if (initialHitPoint.y < (this.transform.position.y - (this.transform.localScale.y /3))) { yForce = -1; } else { yForce = 0; } rigidForForce.velocity = new Vector2(xForce * xForceToAdd, yForce * yForceToAdd); } } 5MinuteRate

15MinuteRate

Lodash的省略() - 函数似乎不适用于嵌套对象。以下代码不起作用:

var object = { requestsPerSecond:
   { mean: 1710.2180279856818,
     count: 10511,
     'currentRate': 1941.4893498239829,
     '1MinuteRate': 168.08263156623656,
     '5MinuteRate': 34.74630977619571,
     '15MinuteRate': 11.646507524106095 } };

编辑:

我尝试了这个,但它不能正常工作:

console.log(_.omit(object, 'requestsPerSecond.count'));

5 个答案:

答案 0 :(得分:6)

你快到了。只需将subObject的结果分配给object.requestsPerSecond

&#13;
&#13;
var object = {
  requestsPerSecond: {
    mean: 1710.2180279856818,
    count: 10511,
    'currentRate': 1941.4893498239829,
    '1MinuteRate': 168.08263156623656,
    '5MinuteRate': 34.74630977619571,
    '15MinuteRate': 11.646507524106095
  }
};

object.requestsPerSecond = _.omit(object.requestsPerSecond, '5MinuteRate', '15MinuteRate');

console.log(object);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:4)

使用未设置:https://lodash.com/docs#unset,它变得更清洁:

var obj = { 
  requestsPerSecond: { 
    mean: 1710.2180279856818,
    count: 10511,
    'currentRate': 1941.4893498239829,
    '1MinuteRate': 168.08263156623656,
    '5MinuteRate': 34.74630977619571,
    '15MinuteRate': 11.646507524106095 
  } 
};

_.forEach(['requestsPerSecond.5MinuteRate', 'requestsPerSecond.15MinuteRate'], 
  function(omitProperty) {
    obj = _.unset(obj, omitProperty);
  }
);

// Or avoiding the "extra" loop.
obj = _.unset(obj, 'requestsPerSecond.5MinuteRate');
obj = _.unset(obj, 'requestsPerSecond.15MinuteRate');

答案 2 :(得分:1)

Lodash _。omit 适用于嵌套对象。自您提出问题以来,他们似乎改善了功能:)

object = _.omit(object, 'requestsPerSecond.5MinuteRate', 'requestsPerSecond.15MinuteRate');

更新

省略将从Lodash 5开始删除

答案 3 :(得分:1)

如果需要深度省略某些路径,请参阅Deepdash的omitDeep

obj = _.omitDeep(obj, /\.*5MinuteRate"\]$/);

请注意,此方法将排除任何深度的所有匹配路径。 它支持单个路径或数组,表示为常量值或正则表达式。

如果使用字符串路径作为参数,则将检查对象中的每个路径是否以给定条件结尾。 这是更详细的codepen for your case

(答案已更新为适合最新的Deepdash v3.1.0)

答案 4 :(得分:0)

如果您愿意尝试其他库,partial.lenses对集合的所有类型的操作都提供了强大的支持,包括复杂的修改,例如对象的所有子代,等等。

因此,在您的情况下,它就像(L只是从partial.lesnes导入的简写)一样简单

const newObject = L.remove(['requestsPerSecond','5MinuteRate'],object)

现在想象一下,您有一个不同的结构,其中有一组具有不同名称的嵌套属性,但所有属性都具有相同的结构,并且您希望将所有这些属性作为目标。想象一下这个例子:

obj = { 
  requestsPerSecond: {
  first: {
    mean: 1710.2180279856818,
    count: 10511,
    '5MinuteRate': 34.74630977619571,
    '15MinuteRate': 11.646507524106095 
  }}
  second: {
    mean: 1710.2180279856818,
    count: 10511,
    '5MinuteRate': 34.74630977619571,
    '15MinuteRate': 11.646507524106095 
  }} 
};

在这种情况下,它会很简单:

const newObject = L.remove(['requestsPerSecond',L.children,'5MinuteRate'],object)

L.children的意思是将requestPerSecond的所有子对象作为目标。