任何人都可以通过一些代码帮助我,以从api获取特定数据并对其进行计数。我的api返回一个性别为json的男性和女性。我想计算男性人数,并在男性{{male.number}}和女性{{female.number}}上显示
提前谢谢。
答案 0 :(得分:1)
请共享您的API的数据结构,这将有助于理解您的要求。否则,对于基本的json,应该是这样的。
console.log( Object.keys( data.male ).length ) ;
答案 1 :(得分:1)
这是json结果
{"results":[{"gender":"female","name":{"title":"ms","first":"vicky","last":"johnston"},"location":{"street":"7743 fairview road","city":"st albans","state":"somerset","postcode":"H31 8UE","coordinates":{"latitude":"10.6391","longitude":"127.0689"},"timezone":{"offset":"-9:00","description":"Alaska"}},"email":"vicky.johnston@example.com","login":{"uuid":"f7f1c2cc-6258-4051-8878-545f934c87a4","username":"orangegoose340","password":"hudson","salt":"jwHcwhFH","md5":"96c0227c3d66c86902b941879db9aa1d","sha1":"f9871732bcacc659f7c7a362162e69e6ed4a7757","sha256":"bf0400b00feb9b599f27f056399a522664bea78c3cb90fb6451a5d3ee4e7a245"},"dob":{"date":"1963-03-17T06:04:01Z","age":55},"registered":{"date":"2017-11-07T07:31:39Z","age":0},"phone":"017687 27809","cell":"0789-228-430","id":{"name":"NINO","value":"WH 58 86 53 D"},"picture":{"large":"https://randomuser.me/api/portraits/women/54.jpg","medium":"https://randomuser.me/api/portraits/med/women/54.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/54.jpg"},"nat":"GB"},{"gender":"female","name":{"title":"ms","first":"ines","last":"dominguez"},"location":{"street":"2710 avenida de castilla","city":"oviedo","state":"cataluña","postcode":34564,"coordinates":{"latitude":"-76.4224","longitude":"42.9023"},"timezone":{"offset":"+5:00","description":"Ekaterinburg, Islamabad, Karachi, Tashkent"}},"email":"ines.dominguez@example.com","login":{"uuid":"f263f9e5-f81b-4fda-bcee-0aa4d19bc2cd","username":"bluebutterfly414","password":"treble","salt":"sz0DHpBb","md5":"2ab8a915f943e47af682537b4501d9f6","sha1":"1b038eb9bfb91f6a5e9e9ddb952bf27c12969cf8","sha256":"1099fcf097fef2c63f34750384fdb117b09d491e3eb613976b4dfee60d5b5c54"},"dob":{"date":"1986-10-30T22:58:58Z","age":31},"registered":{"date":"2003-11-04T01:40:18Z","age":14},"phone":"932-193-864","cell":"635-140-502","id":{"name":"DNI","value":"98066527-H"},"picture":{"large":"https://randomuser.me/api/portraits/women/29.jpg","medium":"https://randomuser.me/api/portraits/med/women/29.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/women/29.jpg"},"nat":"ES"},{"gender":"male","name":{"title":"mr","first":"todd","last":"olson"},"location":{"street":"5064 north street","city":"sunderland","state":"oxfordshire","postcode":"I7C 8DE","coordinates":{"latitude":"-78.2980","longitude":"-81.2629"},"timezone":{"offset":"+11:00","description":"Magadan, Solomon Islands, New Caledonia"}},"email":"todd.olson@example.com","login":{"uuid":"19191cde-cbad-4857-aa46-f8cbb3cb00bd","username":"lazyleopard315","password":"compaq","salt":"YIQ8XzTu","md5":"d7e7d6cf6ef8682f9cf88eac4c55967b","sha1":"6644ff73b6e29dd8441f8af8be763be586675326","sha256":"ca9f0d16cdb6ba7c944b9df749580c5288df1e0575fa93a22b41430f8bd61eb8"},"dob":{"date":"1970-09-28T14:09:20Z","age":47},"registered":{"date":"2011-08-12T13:01:33Z","age":6},"phone":"017683 80936","cell":"0709-514-024","id":{"name":"NINO","value":"XH 48 70 23 F"},"picture":{"large":"https://randomuser.me/api/portraits/men/46.jpg","medium":"https://randomuser.me/api/portraits/med/men/46.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/46.jpg"},"nat":"GB"},{"gender":"male","name":{"title":"mr","first":"marco","last":"skår"},"location":{"street":"binneveien 9448","city":"folkestad","state":"nord-trøndelag","postcode":"1283","coordinates":{"latitude":"-82.2629","longitude":"1.2593"},"timezone":{"offset":"-3:30","description":"Newfoundland"}},"email":"marco.skår@example.com","login":{"uuid":"9523481c-945f-4815-bcd0-e9d1e8623b40","username":"greenpeacock719","password":"salsero","salt":"sOQwluxS","md5":"748f189f04ddd32293fe6cfe3bf8bcc2","sha1":"64fb78708571328e7dfeba9f010282c7302ca0ca","sha256":"db6a562f98e5b033e2719f5303101a3d594accafd3704b21132c57ec464cfc5c"},"dob":{"date":"1993-11-05T15:06:16Z","age":24},"registered":{"date":"2006-02-24T21:42:58Z","age":12},"phone":"24315230","cell":"98529031","id":{"name":"FN","value":"05119303682"},"picture":{"large":"https://randomuser.me/api/portraits/men/18.jpg","medium":"https://randomuser.me/api/portraits/med/men/18.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/18.jpg"},"nat":"NO"},{"gender":"male","name":{"title":"mr","first":"marvin","last":"wood"},"location":{"street":"8428 london road","city":"carlisle","state":"borders","postcode":"E3V 2ND","coordinates":{"latitude":"-29.8441","longitude":"78.2401"},"timezone":{"offset":"+3:00","description":"Baghdad, Riyadh, Moscow, St. Petersburg"}},"email":"marvin.wood@example.com","login":{"uuid":"5ddf34c6-518a-447a-8c07-0a452dc2f42f","username":"heavyleopard923","password":"meathead","salt":"aoDrOGYw","md5":"c5a0678e566da806da000ddcd5f412a3","sha1":"922bd9758afa38407f2e463ee41230db6c8545f6","sha256":"daca3e55e8d896a4a42544f3c2a58d702126bef43d83b4aa138772583cc88dcb"},"dob":{"date":"1985-05-13T12:46:08Z","age":33},"registered":{"date":"2016-02-16T03:26:52Z","age":2},"phone":"016977 3655","cell":"0760-724-225","id":{"name":"NINO","value":"NJ 48 76 05 S"},"picture":{"large":"https://randomuser.me/api/portraits/men/45.jpg","medium":"https://randomuser.me/api/portraits/med/men/45.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/45.jpg"},"nat":"GB"}],"info":{"seed":"b15a75f315182509","results":5,"page":1,"version":"1.2"}}
答案 2 :(得分:1)
malecount = yourobjectname.results.filter(gender =='male')。length; femalecount = yourobjectname.results.filter(gender =='female')。length;