Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 8th, 2004, 07:10 PM
Registered User
 
Join Date: Nov 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to perform set related operations?

Hi, Does anyone have recommendation of the best way to achieve the following in javascript?
I have two set, one contains some numbers, the other contains some other numbers, e.g
set 1: contains 1, 3, 4, 5
set 2: contains 1, 2, 3, 4, 5
I want to know if set 1 contains all the numbers in set 2, if not, it is an error and I will alert the user.
What I really wanted to achieve is that I have several (identical) selection boxes on the left, and several (identical) selection boxes on the right, and I am mapping each item from the left selection box to something in the right selection boxes. And I want to make sure each and every item in the left selection box is chosen to be mapped to something on the right before I click submit.
Thanks for your help.

 
Old November 9th, 2004, 08:00 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can't think of anything other than nested loops:
Code:
function isSubset(set, subset)
{
  for (var i = 0 ; i < subset.length; i++)
  {
    var currentItem = subset[i];
    var bFound = false;
    for (var j = 0; j < set.length; j++)
    {
      if (set[j] == currentItem)
      {
        bFound = true;
        break;
      }  
    }
    if (!bFound)
    {
      return false;
    }
  }
  return true;
}
Checks if all values in second parameter are in first.
Code:
var arr1 = [1, 2, 3, 4];
var arr2 = [1, 2, 3, 4, 6];
alert(isSubset(arr2, arr1));

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
In Cursor set and execute statement related proble param99 SQL Server 2000 0 December 15th, 2006 02:55 AM
Set based operations in Biztalk rule server pacman56 Biztalk 1 April 12th, 2006 07:37 AM
Table operations Paaji Beginning VB 6 0 August 14th, 2005 03:22 PM
transformation on set of related tables? peckli SQL Server DTS 1 March 3rd, 2004 12:21 PM
2 ListBox operations melvik C# 1 August 19th, 2003 06:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.