[英]How to filter JSON data using Typescript / Javascript / Angular 4?
I have json data like this 我有这样的json数据
data :[
{taskname:'chennai', id:'maa' }
{taskname:'mumbai', id:'bom' }
{taskname:'delhi', id:'del' }
....
....
....
{taskname:'salem', id:'che'}
{taskname:'bengaluru', id:'blr'}
{taskname:'chavvapet', id:'chv'}
}]
now i need to filter this data and return matching values (just like autocomplete). 现在,我需要过滤此数据并返回匹配的值(就像自动完成一样)。 For example when i get value CH , first i need to check on ID and then on taskname .
例如,当我获得值CH时 ,首先需要检查ID ,然后检查taskname 。 so the return value should be like this.
所以返回值应该是这样的。 must order ID matching top and then taksname matching.
必须先将ID匹配到top,然后再匹配taksname。
{taskname:'salem', id:'che'}
{taskname:'chavvapet', id:'chv'}
{taskname:'chennai', id:'maa'}
Incase if i get value CHE, then i need to return value like this 如果我得到值CHE,那么我需要像这样返回值
{taskname:'salem', id:'che' }
{taskname:'chennai', id:'maa' }
Could someone please tell me how to do filter using typescript ? 有人可以告诉我如何使用打字稿进行过滤吗?
Tried few of this answer , but no luck. 试过这个答案很少 ,但没有运气。
Try this code 试试这个代码
const data = [ {taskname:'chennai', id:'maa',status:'Submitted'}, {taskname:'mumbai', id:'bom',status:'Resolved'}, {taskname:'delhi', id:'del',status:'Submitted'}, {taskname:'salem', id:'che',status:'In Progress'}, {taskname:'bengaluru',id:'blr',status:'Resolved'}, {taskname:'chavvapet',id:'chv',status:'Submitted'} ]; function filterByString(data, s) { return data.filter(e => e.id.includes(s) || e.taskname.includes(s)) .sort((a,b) => a.id.includes(s) && !b.id.includes(s) ? -1 : b.id.includes(s) && !a.id.includes(s) ? 1 :0); } console.log(filterByString(data, "ch"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.