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, 2022 in Node-js by Vaani
• 7,020 points
327 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, 2022 by Neha
• 9,060 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,880 points
831 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, 2022 in Node-js by Kichu
• 19,050 points
2,016 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, 2022 in Node-js by Vaani
• 7,020 points
921 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
1,663 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
0 votes
1 answer

Start script missing error when running npm start

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

answered Feb 10, 2022 in Java by Soham
• 9,700 points
4,035 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,060 points
3,392 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,060 points
2,122 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP