How to check if a condition is met twice?

Asked

Viewed 49 times

0

Hello, I have the following question... Is there a way for me to check if a condition has been answered more than 1 time in an array? For example, I have an array of people, and each person can have 1 or more children. And I want to return only those people who have at least 2 children with a name started with "B"... I was just using filter, map, some of the JS

var pessoas = [{"nome":"Savannah Klocko","idade":56,"sexo":"N","carro":"Audi","telefones":["909-027-6223 x92614","1-033-396-3498","971.889.3321 x439","(897) 637-3628 x842"],"salario":101225,"filhos":["Mr. Calista Veum"]},{"nome":"William Lang","idade":58,"sexo":"M","carro":"Citroen","telefones":["541.969.0398","945-578-8533 x216","740.395.6464 x91481","191-645-4375"],"salario":26937,"filhos":["Richard Fadel","Victor Flatley"]},{"nome":"Dr. Bella Waelchi","idade":36,"sexo":"M","carro":"Fiat","telefones":["1-406-480-7729 x53442","(541) 833-9896 x28588"],"salario":84981,"filhos":["Felipe Kilback","Krystel Welch","Seamus Mitchell"]},{"nome":"Vickie Marks","idade":16,"sexo":"N","carro":"Volskwagen","telefones":["950-690-5581 x3117","578.060.9599 x0592","(723) 830-7655","1-620-551-7946 x836"],"salario":74494,"filhos":["Vickie Sporer"]},{"nome":"Joelle Leuschke","idade":8,"sexo":"F","carro":"Volskwagen","telefones":["296-692-4640","641.370.2916 x00477","1-825-867-3192 x83321","560-301-0645 x037"],"salario":24015,"filhos":["Edna Kunde","Raymundo Conn"]},{"nome":"Elian Kovacek","idade":66,"sexo":"M","carro":"Renault","telefones":["1-483-736-4221 x912","(677) 019-4699 x204","205-838-0516"],"salario":50481,"filhos":["Arvid Auer","Dr. Dawson McKenzie","Ms. Glenda Hagenes"]},{"nome":"Lila Ward","idade":79,"sexo":"F","carro":"Chevrolet","telefones":["734-729-1545 x61430","(045) 388-2376 x97203","658-103-8550","577-939-2047 x821"],"salario":39967,"filhos":["Max Veum"]},{"nome":"Nicholaus Effertz","idade":35,"sexo":"F","carro":"Fiat","telefones":["(548) 429-5189 x31417","(606) 557-7055","253-925-3619 x85455","1-416-789-1760 x74198"],"salario":48298,"filhos":["Arvel Okuneva","Gail Lehner","Clark Kuhlman"]},{"nome":"Madilyn Conn","idade":79,"sexo":"F","carro":"Renault","telefones":["952.822.2340 x9383","528.908.5456","1-543-080-9753 x8943"],"salario":62383,"filhos":["Odell Wiza MD","Ezekiel Price III","Ebba Klocko"]},{"nome":"Elisa Ebert DVM","idade":8,"sexo":"M","carro":"Renault","telefones":["1-686-113-7589","222-450-4324","145-347-5043 x805"],"salario":4061,"filhos":["Gina Lind","Jules Hirthe","Dariana Gaylord","Alexandria Stroman"]},{"nome":"Daisy Muller","idade":18,"sexo":"N","carro":"Fiat","telefones":["1-792-209-5812","(928) 084-9359 x505","1-365-429-9039 x2408","879.135.1457"],"salario":116428,"filhos":[]},{"nome":"Miss Amely Hintz","idade":59,"sexo":"F","carro":"Citroen","telefones":["592.679.9918 x4665","1-038-228-9629","621.535.7853"],"salario":78396,"filhos":[]},{"nome":"Stephanie Mann","idade":10,"sexo":"M","carro":"Audi","telefones":["(066) 818-5319 x2298","072-976-7727","1-479-833-1590 x13444","429.969.8786"],"salario":23977,"filhos":["Freda Prohaska","Mariela Windler V","Stefanie Goyette"]},{"nome":"Keyshawn O'Reilly","idade":57,"sexo":"M","carro":"Renault","telefones":["(582) 562-4569 x239","983-950-5959","(240) 827-5045","1-485-105-2589 x7475"],"salario":23548,"filhos":["Santiago Corkery","Martin Kuhic","Chelsey Murphy"]},{"nome":"Kristina Mayert","idade":59,"sexo":"M","carro":"Ford","telefones":["585.685.6779 x437","(512) 003-7641 x092","595-317-8919 x89305"],"salario":101084,"filhos":[]},{"nome":"Rita Kunde","idade":75,"sexo":"F","carro":"Chevrolet","telefones":["779.204.2660","(461) 148-3265 x7644","006-286-8304 x34678"],"salario":56657,"filhos":["Elise Bayer"]},{"nome":"Garry King","idade":23,"sexo":"F","carro":"Chevrolet","telefones":["027.495.6818 x160","1-707-729-3412","1-294-371-3251 x3096","1-072-944-7599 x124"],"salario":136868,"filhos":["Mary Bednar","Darron Bogan","Miss Jordane Leuschke","Jarred Goldner"]},{"nome":"Edythe Haley","idade":12,"sexo":"N","carro":"Fiat","telefones":["1-024-275-9188 x0687"],"salario":90163,"filhos":[]},{"nome":"Chadd Nolan","idade":17,"sexo":"F","carro":"Volskwagen","telefones":["1-919-596-1089 x06552","(305) 866-6223","1-732-739-5437"],"salario":4061,"filhos":["Ms. Marlen Stracke","Justine Fisher"]},{"nome":"Merlin Hoppe","idade":24,"sexo":"F","carro":"Audi","telefones":["540-511-0282 x29382","1-706-430-0853 x84861","969-705-9753 x7561","041-930-5867 x3013"],"salario":47241,"filhos":["Grace Murray","Jaqueline Mills","Kale Rau"]},{"nome":"Rene Daugherty III","idade":13,"sexo":"M","carro":"BMW","telefones":["1-798-216-7430 x40461","1-760-651-5552 x3922","743-876-0482","(954) 595-2961 x695"],"salario":63665,"filhos":["Mr. Josh Reynolds","Halie Heathcote","Zackary Hills","Antonetta Bechtelar"]},{"nome":"Camilla DuBuque Sr.","idade":37,"sexo":"N","carro":"Fiat","telefones":["104-652-1564 x83025","1-017-425-1727 x6471","991-248-7473"],"salario":76489,"filhos":[]},{"nome":"Jameson Gleason","idade":55,"sexo":"F","carro":"Ford","telefones":["540.870.8276","1-746-453-3383 x8744","332.320.5064 x98237"],"salario":140639,"filhos":[]},{"nome":"Erika Tillman","idade":5,"sexo":"F","carro":"Chevrolet","telefones":["1-496-506-2310","580.157.0699 x263","(044) 176-6620","1-097-851-3590 x569"],"salario":54087,"filhos":["Nayeli Gerlach","Lilly Lockman"]},{"nome":"Doyle Mueller","idade":10,"sexo":"N","carro":"Renault","telefones":["(800) 712-5478","(561) 751-6791","240.500.2357 x60245","004.616.0111"],"salario":61973,"filhos":["Daija Ward","Dr. Nakia Fritsch","Rhett Nader"]},{"nome":"Renee Bogisich Jr.","idade":39,"sexo":"F","carro":"Ford","telefones":["(884) 347-7186 x017","1-136-997-5641 x37857","271-243-4705 x1402","1-654-687-1440 x8973"],"salario":95450,"filhos":["Pattie Heaney","Johathan Renner","Estel Mertz","Javon Romaguera II","Javier Schuster"]}]

I thought I’d do it this way:

pessoas.filter(item =>(((item.filhos).filter(item => item[0] == "A")).length >= 2))

Where I want you to return me, only people who have more than two children, who start with the letter A

  • Of course. You can iterate over the vector and, at each positive condition, increment a counter.

  • 1

    That bunch of parentheses complicates the reading a little bit, but it looks okay. This code is to return an empty array with this data (no one has 2 children with names starting with A).

  • I understood, it’s just that I pasted here exactly as I thought, then I was putting myself to organize.... kkk

  • I tested here with 1 or more children starting with V and returned the expected.

  • Thank you very much, ended up working msm

  • the guy posted the question with the answer ja? hm

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.