我有这个对象数组
[{},{"IGG":"\n1000400078","Date effet":"16/04/2007","Statut déontologie":"A","Version charte":"1","N°Charte":"0","Nom charte":"Non exposé","Statut Charte":"A","Date envoi charte":"","Dat 1er relance":"","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":""},{"IGG":""},{"IGG":"\n1000400103","Date effet":"23/05/2016","Statut déontologie":"A","Version charte":"2","N°Charte":"1","Nom charte":"Régime Général","Statut Charte":"A","Date envoi charte":"27/09/2010","Dat 1er relance":"07/10/2010","Date 2eme relance":"18/10/2010","Date 3 eme relance":"28/10/2010","Date de validation":"20/11/2015"},{},{"IGG":"\n1000400381","Date effet":"04/09/2019","Statut déontologie":"A","Version charte":"2","N°Charte":"3","Nom charte":"Initié Société Générale","Statut Charte":"A","Date envoi charte":"16/09/2019","Dat 1er relance":"26/09/2019","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"27/09/2019"},{},{"IGG":"\n1000401040","Date effet":"01/09/2015","Statut déontologie":"A","Version charte":"2","N°Charte":"5","Nom charte":"Régime Général & Initié Société Générale","Statut Charte":"A","Date envoi charte":"03/09/2015","Dat 1er relance":"14/09/2015","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"18/09/2015"},{},{"IGG":"\n1007829487","Date effet":"03/12/2019","Statut déontologie":"A","Version charte":"1","N°Charte":"8","Nom charte":"Conseiller Gestion Patrimoine","Statut Charte":"A","Date envoi charte":"13/12/2019","Dat 1er relance":"23/12/2019","Date 2eme relance":"02/01/2020","Date 3 eme relance":"13/01/2020","Date de validation":"14/01/2020"},{},{"IGG":"\n"}]
并且我想要获得一个具有IGG值的对象,其中包括URL查询参数中的IGG
例如,如果我使用localhost:8000 / igg?igg = 1000400078,则需要获取具有“ IGG”的对象:\ n1000400078”,因此该对象:
{"IGG":"\n1000400078","Date effet":"16/04/2007","Statut déontologie":"A","Version charte":"1","N°Charte":"0","Nom charte":"Non exposé","Statut Charte":"A","Date envoi charte":"","Dat 1er relance":"","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":""}
所以我尝试了:
json.find(item => i.includes(item.IGG);
但它返回
[{"IGG":""}]
有人可以帮我吗
答案 0 :(得分:2)
您必须与浏览器中的网址进行比较,因此请使用location
const data = [{}, {"IGG":"\n1000400078","Date effet":"16/04/2007","Statut déontologie":"A","Version charte":"1","N°Charte":"0","Nom charte":"Non exposé","Statut Charte":"A","Date envoi charte":"","Dat 1er relance":"","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":""},{"IGG":""},{"IGG":"\n1000400103","Date effet":"23/05/2016","Statut déontologie":"A","Version charte":"2","N°Charte":"1","Nom charte":"Régime Général","Statut Charte":"A","Date envoi charte":"27/09/2010","Dat 1er relance":"07/10/2010","Date 2eme relance":"18/10/2010","Date 3 eme relance":"28/10/2010","Date de validation":"20/11/2015"},{},{"IGG":"\n1000400381","Date effet":"04/09/2019","Statut déontologie":"A","Version charte":"2","N°Charte":"3","Nom charte":"Initié Société Générale","Statut Charte":"A","Date envoi charte":"16/09/2019","Dat 1er relance":"26/09/2019","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"27/09/2019"},{},{"IGG":"\n1000401040","Date effet":"01/09/2015","Statut déontologie":"A","Version charte":"2","N°Charte":"5","Nom charte":"Régime Général & Initié Société Générale","Statut Charte":"A","Date envoi charte":"03/09/2015","Dat 1er relance":"14/09/2015","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"18/09/2015"},{},{"IGG":"\n1007829487","Date effet":"03/12/2019","Statut déontologie":"A","Version charte":"1","N°Charte":"8","Nom charte":"Conseiller Gestion Patrimoine","Statut Charte":"A","Date envoi charte":"13/12/2019","Dat 1er relance":"23/12/2019","Date 2eme relance":"02/01/2020","Date 3 eme relance":"13/01/2020","Date de validation":"14/01/2020"},{},{"IGG":"\n"}]
const result = data.find(item => item.IGG.replace(/[^a-zA-Z ]/g, '') === location.search.split('igg=')[1]);
console.log(result);
答案 1 :(得分:1)
您的includes
测试是倒数,应该是item.IGG.includes(i)
。
您还需要检查item.IGG
是否存在,否则尝试在.includes()
上调用undefined
时会出错。
const json = [{},{"IGG":"\n1000400078","Date effet":"16/04/2007","Statut déontologie":"A","Version charte":"1","N°Charte":"0","Nom charte":"Non exposé","Statut Charte":"A","Date envoi charte":"","Dat 1er relance":"","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":""},{"IGG":""},{"IGG":"\n1000400103","Date effet":"23/05/2016","Statut déontologie":"A","Version charte":"2","N°Charte":"1","Nom charte":"Régime Général","Statut Charte":"A","Date envoi charte":"27/09/2010","Dat 1er relance":"07/10/2010","Date 2eme relance":"18/10/2010","Date 3 eme relance":"28/10/2010","Date de validation":"20/11/2015"},{},{"IGG":"\n1000400381","Date effet":"04/09/2019","Statut déontologie":"A","Version charte":"2","N°Charte":"3","Nom charte":"Initié Société Générale","Statut Charte":"A","Date envoi charte":"16/09/2019","Dat 1er relance":"26/09/2019","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"27/09/2019"},{},{"IGG":"\n1000401040","Date effet":"01/09/2015","Statut déontologie":"A","Version charte":"2","N°Charte":"5","Nom charte":"Régime Général & Initié Société Générale","Statut Charte":"A","Date envoi charte":"03/09/2015","Dat 1er relance":"14/09/2015","Date 2eme relance":"","Date 3 eme relance":"","Date de validation":"18/09/2015"},{},{"IGG":"\n1007829487","Date effet":"03/12/2019","Statut déontologie":"A","Version charte":"1","N°Charte":"8","Nom charte":"Conseiller Gestion Patrimoine","Statut Charte":"A","Date envoi charte":"13/12/2019","Dat 1er relance":"23/12/2019","Date 2eme relance":"02/01/2020","Date 3 eme relance":"13/01/2020","Date de validation":"14/01/2020"},{},{"IGG":"\n"}]
let i = "1000400078";
console.log(json.find(item => item.IGG && item.IGG.includes(i)));