【问题标题】:is there a simply way through which i can filter json data based on different conditions有没有一种简单的方法可以根据不同的条件过滤json数据
【发布时间】:2019-10-07 21:10:55
【问题描述】:

我收到以下数据作为我的一项网络服务的响应。我想根据不同值的几种组合过滤数据。什么是最简单的方法,它会影响我的网络服务的性能

数据:我正在获取对象数组

[
    {
        "source": "9606.ENSP00000000233",
        "target": "9606.ENSP00000366563",
        "relationship": "ON_INTERACTION_WITH",
        "relationshipdetails": {
            "database4": "0",
            "database3": "0",
            "coexpression2": "42",
            "coexpression3": "42",
            "experimental": "174",
            "database2": "0",
            "combinedscore3": "298",
            "combinedscore4": "298",
            "experimental2": "174",
            "combinedscore": "298",
            "database": "0",
            "combinedscore2": "298",
            "experimental3": "174",
            "experimental4": "174",
            "coexpression4": "42",
            "textmining3": "184",
            "coexpression": "42",
            "textmining4": "184",
            "textmining2": "184",
            "textmining": "184"
        }
    },
    {
        "source": "9606.ENSP00000000233",
        "target": "9606.ENSP00000402551",
        "relationship": "ON_INTERACTION_WITH",
        "relationshipdetails": {
            "database4": "0",
            "database3": "0",
            "coexpression2": "148",
            "coexpression3": "148",
            "experimental": "138",
            "database2": "0",
            "combinedscore3": "247",
            "combinedscore4": "247",
            "experimental2": "138",
            "combinedscore": "247",
            "database": "0",
            "combinedscore2": "247",
            "experimental3": "138",
            "experimental4": "138",
            "coexpression4": "148",
            "textmining3": "57",
            "coexpression": "148",
            "textmining4": "57",
            "textmining2": "57",
            "textmining": "57"
        }
    },
    {
        "source": "9606.ENSP00000000233",
        "target": "9606.ENSP00000386239",
        "relationship": "ON_INTERACTION_WITH",
        "relationshipdetails": {
            "database4": "0",
            "database3": "0",
            "coexpression2": "49",
            "coexpression3": "49",
            "experimental": "120",
            "database2": "0",
            "combinedscore3": "190",
            "combinedscore4": "190",
            "experimental2": "120",
            "combinedscore": "190",
            "database": "0",
            "combinedscore2": "190",
            "experimental3": "120",
            "experimental4": "120",
            "coexpression4": "49",
            "textmining3": "110",
            "coexpression": "49",
            "textmining4": "110",
            "textmining2": "110",
            "textmining": "110"
        }
    }
]

条件:where textmining >/</= 100 and coexpression >/</= 30 and combinedscore >/</= 250 数据必须根据大于/等于/小于条件的 5 个过滤器值(文本挖掘、共表达、实验、数据库和组合分数)进行过滤,所有这些都必须用作输入。

【问题讨论】:

  • 请更具体。你在过滤什么?举几个输入输出的例子。
  • 条件是什么?更清楚。 >/// 是什么意思?
  • 这是一个对象数组。你可以使用Array.prototype.filter 做你想做的事。
  • 顺便说一句,那些不是对象,它是 JSON @Archer
  • @Kevin.a JSON 是一种文本格式。一旦你从任何地方获取数据,它就是一个对象数组。

标签: javascript node.js express filter


【解决方案1】:

这取决于你想在数组上 executespecific 逻辑。
但通常获取值和比较值我认为更好的选择是循环。

性能方面

more detail here

【讨论】:

    【解决方案2】:

    const arr = [
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000366563",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "42",
                "coexpression3": "42",
                "experimental": "174",
                "database2": "0",
                "combinedscore3": "298",
                "combinedscore4": "298",
                "experimental2": "174",
                "combinedscore": "298",
                "database": "0",
                "combinedscore2": "298",
                "experimental3": "174",
                "experimental4": "174",
                "coexpression4": "42",
                "textmining3": "184",
                "coexpression": "42",
                "textmining4": "184",
                "textmining2": "184",
                "textmining": "184"
            }
        },
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000402551",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "148",
                "coexpression3": "148",
                "experimental": "138",
                "database2": "0",
                "combinedscore3": "247",
                "combinedscore4": "247",
                "experimental2": "138",
                "combinedscore": "247",
                "database": "0",
                "combinedscore2": "247",
                "experimental3": "138",
                "experimental4": "138",
                "coexpression4": "148",
                "textmining3": "57",
                "coexpression": "148",
                "textmining4": "57",
                "textmining2": "57",
                "textmining": "57"
            }
        },
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000386239",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "49",
                "coexpression3": "49",
                "experimental": "120",
                "database2": "0",
                "combinedscore3": "190",
                "combinedscore4": "190",
                "experimental2": "120",
                "combinedscore": "190",
                "database": "0",
                "combinedscore2": "190",
                "experimental3": "120",
                "experimental4": "120",
                "coexpression4": "49",
                "textmining3": "110",
                "coexpression": "49",
                "textmining4": "110",
                "textmining2": "110",
                "textmining": "110"
            }
        }
    ] ; 
    
    console.log(arr.filter(entry => entry.relationshipdetails.textmining > 110 ))

    使用过滤器。在我的示例中,我过滤了小于 110 的所有内容。使用 Array.prototype.filter 您可以使用我给出的示例将其应用于其余部分

    理想情况下,您会将这些结果存储到一个变量中。为了解释起见,我只是记录它。

    【讨论】:

    • 有什么我可以解释得更好的吗?
    【解决方案3】:

    您可以使用过滤器,只需添加所需的比较和逻辑以返回真或假

    var data = [
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000366563",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "42",
                "coexpression3": "42",
                "experimental": "174",
                "database2": "0",
                "combinedscore3": "298",
                "combinedscore4": "298",
                "experimental2": "174",
                "combinedscore": "298",
                "database": "0",
                "combinedscore2": "298",
                "experimental3": "174",
                "experimental4": "174",
                "coexpression4": "42",
                "textmining3": "184",
                "coexpression": "42",
                "textmining4": "184",
                "textmining2": "184",
                "textmining": "184"
            }
        },
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000402551",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "148",
                "coexpression3": "148",
                "experimental": "138",
                "database2": "0",
                "combinedscore3": "247",
                "combinedscore4": "247",
                "experimental2": "138",
                "combinedscore": "247",
                "database": "0",
                "combinedscore2": "247",
                "experimental3": "138",
                "experimental4": "138",
                "coexpression4": "148",
                "textmining3": "57",
                "coexpression": "148",
                "textmining4": "57",
                "textmining2": "57",
                "textmining": "57"
            }
        },
        {
            "source": "9606.ENSP00000000233",
            "target": "9606.ENSP00000386239",
            "relationship": "ON_INTERACTION_WITH",
            "relationshipdetails": {
                "database4": "0",
                "database3": "0",
                "coexpression2": "49",
                "coexpression3": "49",
                "experimental": "120",
                "database2": "0",
                "combinedscore3": "190",
                "combinedscore4": "190",
                "experimental2": "120",
                "combinedscore": "190",
                "database": "0",
                "combinedscore2": "190",
                "experimental3": "120",
                "experimental4": "120",
                "coexpression4": "49",
                "textmining3": "110",
                "coexpression": "49",
                "textmining4": "110",
                "textmining2": "110",
                "textmining": "110"
            }
        }
    ]
    
    var filtered = data.filter((entry) => {
      if (!('relationshipdetails' in entry)) return false
      return (entry.relationshipdetails.textmining > 100 && entry.relationshipdetails.coexpression < 50)
    });
    
    console.log(filtered);

    【讨论】:

      【解决方案4】:

      给定输入对象input和条件textmining &gt; 100coexpression &lt; 30

      var result = input .filter (entry => entry ["relationshipdetails"] ["textmining"] > 100) .filter (entry => entry ["relationshipdetails"] ["coexpression"] < 30); // etc...

      (见the MDN docs on Array.prototype.filter。)

      【讨论】:

        猜你喜欢
        • 2021-01-09
        • 2022-01-20
        • 2020-01-25
        • 1970-01-01
        • 1970-01-01
        • 2019-03-10
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        相关资源
        最近更新 更多