说这是我的数据,如何使用Underscore过滤数据以仅通过tax_rank保留嵌套部分,而保留对象结构?
我正在尝试创建一个热图,当您从下拉菜单中进行选择时,它将按tax_rank重新绘制热图。我是Underscore的新手,.filter .pluck和.map实用程序看起来非常有用,但似乎无法正确实现链接。
let data = [
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 832,
"parent": 204431,
"count": 12,
"count_norm": 213,
"tax_name": "Fibrobacter",
"tax_rank": "genus"
},
{
"taxon": 841,
"parent": 186803,
"count": 1285,
"count_norm": 22911,
"tax_name": "Roseburia",
"tax_rank": "genus"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
},
{
"taxon": 976,
"parent": 68336,
"count": 26028,
"count_norm": 464081,
"tax_name": "Bacteroidetes",
"tax_rank": "phylum"
}
]
},
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 832,
"parent": 204431,
"count": 12,
"count_norm": 213,
"tax_name": "Fibrobacter",
"tax_rank": "genus"
},
{
"taxon": 841,
"parent": 186803,
"count": 1285,
"count_norm": 22911,
"tax_name": "Roseburia",
"tax_rank": "genus"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
},
{
"taxon": 976,
"parent": 68336,
"count": 26028,
"count_norm": 464081,
"tax_name": "Bacteroidetes",
"tax_rank": "phylum"
}
]
}]
如果过滤器参数为tax_rank =种,这就是我希望实现的目标:
let data = [
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
}
]
},
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
}
]
}]
答案 0 :(得分:1)
不要使用下划线,普通js会更短。
data.forEach(e => e.ubiome = e.ubiome.filter(e => e.tax_rank === 'species'));
console.log(data);
<script>
let data = [
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 832,
"parent": 204431,
"count": 12,
"count_norm": 213,
"tax_name": "Fibrobacter",
"tax_rank": "genus"
},
{
"taxon": 841,
"parent": 186803,
"count": 1285,
"count_norm": 22911,
"tax_name": "Roseburia",
"tax_rank": "genus"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
},
{
"taxon": 976,
"parent": 68336,
"count": 26028,
"count_norm": 464081,
"tax_name": "Bacteroidetes",
"tax_rank": "phylum"
}
]
},
{
"sample_date": "2017-07-04T00:00:00.000Z",
"ubiome": [
{
"taxon": 817,
"parent": 816,
"count": 72,
"count_norm": 1283,
"tax_name": "Bacteroides fragilis",
"tax_rank": "species"
},
{
"taxon": 818,
"parent": 816,
"count": 208,
"count_norm": 3708,
"tax_name": "Bacteroides thetaiotaomicron",
"tax_rank": "species"
},
{
"taxon": 820,
"parent": 816,
"count": 41,
"count_norm": 731,
"tax_name": "Bacteroides uniformis",
"tax_rank": "species"
},
{
"taxon": 821,
"parent": 816,
"count": 3490,
"count_norm": 62226,
"tax_name": "Bacteroides vulgatus",
"tax_rank": "species"
},
{
"taxon": 823,
"parent": 375288,
"count": 120,
"count_norm": 2139,
"tax_name": "Parabacteroides distasonis",
"tax_rank": "species"
},
{
"taxon": 832,
"parent": 204431,
"count": 12,
"count_norm": 213,
"tax_name": "Fibrobacter",
"tax_rank": "genus"
},
{
"taxon": 841,
"parent": 186803,
"count": 1285,
"count_norm": 22911,
"tax_name": "Roseburia",
"tax_rank": "genus"
},
{
"taxon": 842,
"parent": 841,
"count": 11,
"count_norm": 196,
"tax_name": "Roseburia cecicola",
"tax_rank": "species"
},
{
"taxon": 853,
"parent": 216851,
"count": 7853,
"count_norm": 140019,
"tax_name": "Faecalibacterium prausnitzii",
"tax_rank": "species"
},
{
"taxon": 976,
"parent": 68336,
"count": 26028,
"count_norm": 464081,
"tax_name": "Bacteroidetes",
"tax_rank": "phylum"
}
]
}];
</script>
在纯js具有完全相同的功能之前,IMHO下划线/破折号更为重要,现在大多数内容都在Array.prototype上,这使得使用纯JavaScript更容易链接,更清晰地阅读...。