Re: Stripping [ ] from JSON arrays
[email protected] (The Doctor) Wed, 11 Jan 2023 16:18:28 -0000 (UTC)
| Newsgroups | comp.lang.php,comp.lang.javascript |
|---|---|
| Organization | NetKnow News |
| Message-ID | <[email protected]> |
In article <[email protected]>, Elhwen Dico <[email protected]> wrote: >Le 10/01/2023 à 05:29, JJ a écrit : >> On Tue, 10 Jan 2023 01:17:47 -0000 (UTC), The Doctor wrote: >>> >>> How do I strip unnecessary [] ? >> >> I'd use array's `reduce()` for that. e.g. >> >> var resultObj = theArray.reduce((res, obj) => Object.assign(res, obj), {}); > > const array = [ > [0, 1, 2], [], ["tutu"], undefined > ]; > > const result = array.filter( > (tab) => (tab && tab.length)); > > console.log(result); > >-> [ [ 0, 1, 2 ], [ 'tutu' ] ] > > you could improve filter callback to check that array members are arrays. giving an update, I finally found someone at Moneris to work with. I hate bad doumentation. -- Member - Liberal International This is [email protected] Ici [email protected] Yahweh, King & country!Never Satan President Republic!Beware AntiChrist rising! Look at Psalms 14 and 53 on Atheism https://www.empire.kred/ROOTNK?t=94a1f39b Birthdate: 29 Jan 1969 Redhill, Surrey, England Beware https://mindspring.com