有没有一种简单的方法可以根据不同的条件过滤json数据

时间:2019-10-07 21:10:55

标签: javascript node.js express filter

我从以下Web服务之一获得以下数据作为响应。我想根据不同值的几种组合来过滤数据。这样做最简单的方法是什么,它确实会影响我的Web服务的性能

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

[
    {
        "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个大于/等于/小于条件的过滤器值(文本挖掘,共表达,实验,数据库和组合分数)进行过滤,所有这些都必须使用输入。

4 个答案:

答案 0 :(得分:0)

这确实取决于要在数组上specific进行的execute逻辑。
 但通常获取值并比较值,我认为更好的选择是循环。

明智的性能

请参见more detail here

performance analysis of for loop

答案 1 :(得分:0)

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将其应用于我给出的示例中的其余部分

理想情况下,您会将这些结果存储到变量中。为了便于说明,我只记录了它。

答案 2 :(得分:0)

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

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);

答案 3 :(得分:0)

给出输入对象input以及条件textmining > 100coexpression < 30

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

(请参阅the MDN docs on Array.prototype.filter。)