7 Replies - 332 Views - Last Post: 06 November 2017 - 09:55 AM

#1 jwh20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 17

Matching up Javascript lists

Posted 02 November 2017 - 12:57 PM

I have two lists that I want to display side-by-side on the screen so that the differences between them are clearly visible to the user. Each list would have a blank where the other list has an entry something like this:

[img]https://s3.amazonaws.com/jwhance/lists.JPG[/img]

So I have two sorted arrays like:
var A0 = ['a', 'b', 'c', 'd', 'f'];
var B0 = ['a', 'b', 'c', 'e', 'f'];


and I want to end up with something like the following so that I can make it display as above:

var A1 = ['a', 'b', 'c', 'd', '', 'f'];
var B1 = ['a', 'b', 'c', '', 'e', 'f'];


I'm just drawing a blank on a clean way to do this.

Is This A Good Question/Topic? 0
  • +

Replies To: Matching up Javascript lists

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Matching up Javascript lists

Posted 02 November 2017 - 01:04 PM

I guess one way to do it would be to create a union, an array with all of the values in either list, sort that, and then loop through it and check if each item is in either array and either add the item or add a blank value.
Was This Post Helpful? 1
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4136
  • View blog
  • Posts: 13,051
  • Joined: 08-June 10

Re: Matching up Javascript lists

Posted 03 November 2017 - 01:09 AM

https://www.codingfo...ript-lists.html
Was This Post Helpful? 0
  • +
  • -

#4 jwh20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 17

Re: Matching up Javascript lists

Posted 03 November 2017 - 12:59 PM

Seems like this site merely have people who search the internet for other postings. Not really all that helpful. I did find a solution:

		function xxx(){
			
			const A0 = ['a', 'b', 'c', 'd'];
			const B0 = ['b', 'd', 'e', 'f', 'g', 'q', 'r'];
			
			var completed = false;
			var i = 0;
			while(!completed){
				if(A0[i] === B0[i] || A0[i] === '' || B0[i] === ''){
					i++;
					
					if(i === Math.max(A0.length, B0.length)){
						completed = true;
					}
					
					continue;
				}
				
				if(A0[i] < B0[i] || B0[i] === undefined){
					// Add one to B
					B0.splice(i, 0, '');
				} else if(A0[i] > B0[i] || A0[i] === undefined){
					// Add one to A
					A0.splice(i, 0, '');
				}
				
				i = 0;
			}
			
			console.log(A0, B0)			
		}

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,758
  • Joined: 12-December 12

Re: Matching up Javascript lists

Posted 03 November 2017 - 01:09 PM

Do you understand this solution that you found?
Was This Post Helpful? 0
  • +
  • -

#6 jwh20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 17

Re: Matching up Javascript lists

Posted 03 November 2017 - 01:32 PM

Yes, I wrote this myself (I know you probably find that difficult to believe) based on some other input.

This post has been edited by Dormilich: 07 November 2017 - 01:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1828
  • View blog
  • Posts: 5,755
  • Joined: 15-January 14

Re: Matching up Javascript lists

Posted 06 November 2017 - 09:53 AM

That's going to change the original arrays, based on your first post it sounded like you wanted to create new arrays. That's why I suggested what I did.
Was This Post Helpful? 0
  • +
  • -

#8 jwh20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 02-November 17

Re: Matching up Javascript lists

Posted 06 November 2017 - 09:55 AM

Thanks, yes you're correct, it does change the original arrays but that doesn't really matter.

This post has been edited by Dormilich: 07 November 2017 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1