flatting and sorting nested arrays In javascript nodejs

0 votes

I have an array that outputs numerous arrays, but if some of the codes are the same, they all output in the same array. How do I separate them?

[
[ [ '0011', 6, 96, '2021-08-16' ], [ '0011', 2, 4, '2021-08-17' ] ],
  [ [ '0032', 20, 26, '2021-08-16' ] ],
  [ [ '0098', 6, 246, '2021-08-16' ], [ '0098', 0, 0, '2021-08-17' ] ],
  [ [ '0093', 2, 30, '2021-08-16' ] ],
  [ [ '0051', 32, 229, '2021-08-16' ] ],
  [ [ '0053', 27, 63, '2021-08-16' ], [ '0053', 2, 38, '2021-08-17' ] ],
  [ [ '0022', 4, 2, '2021-08-16' ], [ '0022', 6, 12, '2021-08-17' ] ],
  [ [ '0067', 2, 144, '2021-08-16' ] ],
  [ [ '0166', 4, 102, '2021-08-16' ] ],
  [ [ '0015', 4, 96, '2021-08-16' ] ],
]

How do I retrieve the values that have two or more values in their array and put them in so that each one is distinct within the array, if that makes sense?

[
[ [ '0011', 6, 96, '2021-08-16' ]], 
[[ '0011', 2, 4, '2021-08-17' ] ],
  [ [ '0032', 20, 26, '2021-08-16' ] ],
  [ [ '0098', 6, 246, '2021-08-16' ],
[ [ '0098', 0, 0, '2021-08-17' ] ],
  [ [ '0093', 2, 30, '2021-08-16' ] ],
  [ [ '0051', 32, 229, '2021-08-16' ] ],
  [ [ '0053', 27, 63, '2021-08-16' ]],
  [ [ '0053', 2, 38, '2021-08-17' ] ],
  [ [ '0022', 4, 2, '2021-08-16' ]], 
  [ [ '0022', 6, 12, '2021-08-17' ] ],
  [ [ '0067', 2, 144, '2021-08-16' ] ],
  [ [ '0166', 4, 102, '2021-08-16' ] ],
  [ [ '0015', 4, 96, '2021-08-16' ] ],
]

Or

[
[  '0011', 6, 96, '2021-08-16' ], 
[ '0011', 2, 4, '2021-08-17' ],
  [  '0032', 20, 26, '2021-08-16'  ],
  [  '0098', 6, 246, '2021-08-16' ],
[  '0098', 0, 0, '2021-08-17' ],
  [  '0093', 2, 30, '2021-08-16'  ],
  [ '0051', 32, 229, '2021-08-16'  ],
  [ '0053', 27, 63, '2021-08-16' ],
  [ '0053', 2, 38, '2021-08-17' ],
  [ '0022', 4, 2, '2021-08-16' ], 
  [  '0022', 6, 12, '2021-08-17'  ],
  [  '0067', 2, 144, '2021-08-16' ],
  [ '0166', 4, 102, '2021-08-16'  ],
  [ '0015', 4, 96, '2021-08-16'  ],
]

I tried flatten, but it only added the arrays in the same array together, which isn't what I wanted.

Jun 8 in Node-js by Vaani
• 7,020 points
15 views

1 answer to this question.

0 votes

Try this:

let myarray = [
 [ [ '0011', 6, 96, '2021-08-16' ], [ '0011', 2, 4, '2021-08-17' ] ],
 [ [ '0032', 20, 26, '2021-08-16' ] ],
 [ [ '0098', 6, 246, '2021-08-16' ], [ '0098', 0, 0, '2021-08-17' ] ],
 [ [ '0093', 2, 30, '2021-08-16' ] ],
 [ [ '0051', 32, 229, '2021-08-16' ] ],
 [ [ '0053', 27, 63, '2021-08-16' ], [ '0053', 2, 38, '2021-08-17' ] ],
 [ [ '0022', 4, 2, '2021-08-16' ], [ '0022', 6, 12, '2021-08-17' ] ],
 [ [ '0067', 2, 144, '2021-08-16' ] ],
 [ [ '0166', 4, 102, '2021-08-16' ] ],
 [ [ '0015', 4, 96, '2021-08-16' ] ],
 ];




  let cleanArry = [];

  myarray.forEach(item=>{
  item.forEach(nItem=>{
    cleanArry.push(nItem)
  })
  })
  
  //this is the array that you want 
  console.log(cleanArry);
answered Jun 9 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

How to split and modify a string in NodeJS?

Hello @kartik, Use split and map function: var str = "123, 124, 234,252"; var ...READ MORE

answered Oct 16, 2020 in Node-js by Niroj
• 82,720 points
363 views
0 votes
0 answers

How can I completely uninstall nodejs, npm and node in Ubuntu

I installed it using: sudo apt-get install ...READ MORE

May 15 in Node-js by Kichu
• 19,040 points
176 views
0 votes
1 answer

Installing angularjs with NPM and NODEJS brings an error in ubuntu 16.04

Rename your angular folder to angular-test or ...READ MORE

answered May 30 in Node-js by Vaani
• 7,020 points
13 views
0 votes
1 answer
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
967 views
0 votes
1 answer
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10 in Java by Soham
• 8,730 points
1,778 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP