使用Underscore.JS进行深度选择 [英] Deep picking using Underscore.JS

查看:59
本文介绍了使用Underscore.JS进行深度选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用underscoreJs来操作JavaScript对象,但是这样做有问题.

I am trying to use underscoreJs to manipulate a JavaScript object and having problems doing so.

这是我的例子

var data = {
  "label": "SomeName",
  "parent": [{
    "id": "parentId",
    "resources": [{
      "name": "ID1NAME",
      "calls": [
        "user_get", "user2_post", "user3_delete"
      ]
    }, {
      "name": "ID2",
      "calls": [
        "employee1_get", "employee2_delete", "employee3_update"
      ]
    }]
  }]
};
var res = _(data).chain().
    pluck('parent').
    flatten().
    findWhere(function(item){
     item === "user_get"
    }).
    value();
    
console.log(res);

使用属于data.parent.calls[]一部分的元素(示例:"user_get"),我想提取其父对象,即data.parent[0].

Using an element which is a part of data.parent.calls[] (example : "user_get") I would like to extract its parent object, i.e. data.parent[0].

我在上面尝试过,但始终未定义.感谢您的帮助.

I tried above but always get undefined. I appreciate any help on this.

推荐答案

您遇到的问题之一是使用_.pluck.如果对对象执行_.pluck,它将遍历对象的键,以尝试检索指定为第二个参数的属性(在本例中为'parent'). 'label'是一个字符串,而'parent '是一个数组,因此得到的数组是[undefined, undefined].其余的将出错.

One of the problems you're having is your use of _.pluck. If you execute _.pluck over an object, it'll go over the keys of the object trying to retrieve the property you specified as the second argument (in this case, 'parent'). 'label' is a string and 'parent' is an array so thus the array that you get as a result is [undefined, undefined]. The rest will then go wrong.

一种解决方案如下:

function findCallIndexInParent(call, parent) {
    return _.chain(parent)
            .pluck('resources')
            .flatten()
            .findIndex(function (obj) {
                return _.contains(obj.calls, call);
            })
            .value();
}

function findCall(call, data) {
    var parent = data.parent;
    return parent[findCallIndexInParent(call, parent)];
}

console.log(findCall('user_get', data));

findCall只是一种方便的方法,它将数据的父属性传递给findCallIndexInParent(它将检索 call 所在的索引)并使用返回所需的对象父级数组.

findCall is just a convenient method that will pass the parent property of data to findCallIndexInParent (that will retrieve the index where call is) and return the desired object with the parent array.

Lodash (下划线的分叉)提供了一种获取对象属性的方法,该对象本来会很方便的在这里(遗憾的是,下划线没有它).

Lodash (a fork of underscore) provides a method to get the property of an object that would have come really handy in here (sadly, underscore doesn't have it).

findCallIndexInParent的解释如下:

  1. 链接父级列表
  2. 选择资源数组
  3. 当拔出地图时,它会返回列表列表,因此需要展平.
  4. 找到调用包含 call
  5. 的元素的索引
  6. 返回在 parent 中包含 call 的对象的值(索引).
  1. Chain the parent list
  2. pluck the resources array
  3. As pluck maps, it returns a list of lists so a flatten is needed.
  4. Find the index of the element which calls contains call
  5. Return the value (the index) of the object that contains call within parent.

这是小提琴.希望对您有所帮助.

Here's the fiddle. Hope it helps.

这篇关于使用Underscore.JS进行深度选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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