Javascript 计算两个数组中出现的相同元素数

Javascript 计算两个数组中出现的相同元素数,javascript,arrays,checkbox,Javascript,Arrays,Checkbox,我有一个表单,作为表单的结果,我有两个数组——一个用于角色的细节,另一个用于个人的细节。表单的一部分是日期的复选框-日期是固定价格,但在同一日期预订两个日期是降价的 我想做的是比较personA的数组和personB的数组,对于任何重复的问题,降低发布率 费率将分为两个变量计算——以英镑为单位的总价日期和以英镑为单位的总降价日期 以下是我目前为止的代码,单击复选框时会出现这种情况: function processDates(){ var contentsA, valsA = [], d

我有一个表单,作为表单的结果,我有两个数组——一个用于角色的细节,另一个用于个人的细节。表单的一部分是日期的复选框-日期是固定价格,但在同一日期预订两个日期是降价的

我想做的是比较personA的数组和personB的数组,对于任何重复的问题,降低发布率

费率将分为两个变量计算——以英镑为单位的总价日期和以英镑为单位的总降价日期

以下是我目前为止的代码,单击复选框时会出现这种情况:

function processDates(){
    var contentsA, valsA = [], dates_A =  document.forms['registerForm']['personADates[]'];
    for(var iA=0,elmA;elmA = dates_A[iA];iA++) {
        if(elmA.checked) {
            valsA.push(elmA.value);
        }
    }
    contentsA = valsA.join(', ');

    var contentsB, valsB = [], dates_B =  document.forms['registerForm']['personBDates[]'];
    for(var iB=0,elmB;elmB = dates_B[iB];iB++) {
        if(elmB.checked) {
            valsB.push(elmB.value);
        }
    }
    contentsB = valsB.join(', ');
}
用我自己的,你可以做到:

// If the original order of the elements is not important
var common = array1.intersect( array2 );

// If the original order of the elements is important
var common = array1.unsortedIntersect( array2 );
// If the original order of the elements is not important
var common = (new JS.SortedSet(array1)).intersect(new JS.SortedSet(array2));

// If the original order of the elements is important
var common = (new JS.Set(array1)).intersect(new JS.Set(array2));
使用更大的尺寸,您可以执行以下操作:

// If the original order of the elements is not important
var common = array1.intersect( array2 );

// If the original order of the elements is important
var common = array1.unsortedIntersect( array2 );
// If the original order of the elements is not important
var common = (new JS.SortedSet(array1)).intersect(new JS.SortedSet(array2));

// If the original order of the elements is important
var common = (new JS.Set(array1)).intersect(new JS.Set(array2));
我不清楚数组中的值实际上是什么;用户是否以纯文本形式输入这些日期?如果是这样的话,您需要确保它们被规范化,以便它们具有可比性并保证匹配

如果这些对象不仅仅是简单的可比较对象,例如,您创建的不是字符串或日期实例,而是复杂对象,则“我的库”允许您提供一个比较函数,以便您可以确定哪些是相等的。有关这方面的更多详细信息,请参阅库

用我自己的,你可以做到:

// If the original order of the elements is not important
var common = array1.intersect( array2 );

// If the original order of the elements is important
var common = array1.unsortedIntersect( array2 );
// If the original order of the elements is not important
var common = (new JS.SortedSet(array1)).intersect(new JS.SortedSet(array2));

// If the original order of the elements is important
var common = (new JS.Set(array1)).intersect(new JS.Set(array2));
使用更大的尺寸,您可以执行以下操作:

// If the original order of the elements is not important
var common = array1.intersect( array2 );

// If the original order of the elements is important
var common = array1.unsortedIntersect( array2 );
// If the original order of the elements is not important
var common = (new JS.SortedSet(array1)).intersect(new JS.SortedSet(array2));

// If the original order of the elements is important
var common = (new JS.Set(array1)).intersect(new JS.Set(array2));
我不清楚数组中的值实际上是什么;用户是否以纯文本形式输入这些日期?如果是这样的话,您需要确保它们被规范化,以便它们具有可比性并保证匹配


如果这些对象不仅仅是简单的可比较对象,例如,您创建的不是字符串或日期实例,而是复杂对象,则“我的库”允许您提供一个比较函数,以便您可以确定哪些是相等的。有关这方面的详细信息,请参阅库。

如果功能性javascript是一个选项:

_.sum = function(array) {
    return _.reduce(array, function(memo, val) {
        return memo + val;
    }, 0);
};

var duplicates = _.intersect(valsA, valsB);
var fullPrice = _.sum(valsA.concat(valsB));
var reducedPrice = fullPrice - discountScalar * _.sum(duplicates);

依赖于易于跨浏览器实现的功能方法。在现代浏览器上,大多数功能都可以通过array.reduce和array.map实现。

如果功能性javascript是一个选项:

_.sum = function(array) {
    return _.reduce(array, function(memo, val) {
        return memo + val;
    }, 0);
};

var duplicates = _.intersect(valsA, valsB);
var fullPrice = _.sum(valsA.concat(valsB));
var reducedPrice = fullPrice - discountScalar * _.sum(duplicates);

依赖于易于跨浏览器实现的功能方法。在现代浏览器上,大多数都可以通过array.reduce和array.map实现。

可以使用对象警告来代替使用数组。。。未测试代码:

function processDates(){
    var valsA = {}, dates_A =  document.forms['registerForm']['personADates[]'];
    for(var iA=0,elmA;elmA = dates_A[iA];iA++) {
        if(elmA.checked) {
            valsA[elmA.value] = 1;  // Store values
        }
    }

    var samedate = [];
    var dates_B = document.forms['registerForm']['personBDates[]'];
    for(var iB=0,elmB;elmB = dates_B[iB];iB++) {
        if(elmB.checked) {
            if (valsB[elmB.value])
            {
                // Here we found a date that's present in both
                samedate.push(elmB.value);
            }
        }
    }
    // Here samedate is a list of all dates listed in both A and B
}

您可以使用对象警告,而不是使用数组。。。未测试代码:

function processDates(){
    var valsA = {}, dates_A =  document.forms['registerForm']['personADates[]'];
    for(var iA=0,elmA;elmA = dates_A[iA];iA++) {
        if(elmA.checked) {
            valsA[elmA.value] = 1;  // Store values
        }
    }

    var samedate = [];
    var dates_B = document.forms['registerForm']['personBDates[]'];
    for(var iB=0,elmB;elmB = dates_B[iB];iB++) {
        if(elmB.checked) {
            if (valsB[elmB.value])
            {
                // Here we found a date that's present in both
                samedate.push(elmB.value);
            }
        }
    }
    // Here samedate is a list of all dates listed in both A and B
}

common会检索一个编号的值吗?@DanielHanly否,common是一个数组,其中包含两个数组共有的所有值。您可以使用common.length来确定可能有多少个。@phogz这样扩展array.prototype,如果在大块代码中使用,将导致一些兼容性问题。我建议您不要这样做。@Raynos它只会导致新手级代码中的兼容性问题,而愚蠢地尝试迭代使用for…in和b的数组,这样做并不意味着我们拥有自己的属性。非常好。在回答您的问题时,有一系列可用日期的复选框,然后复选框值存储在4月25日、4月26日等。common检索一个编号的值?@DanielHanly否,common是一个数组,其中包含两个数组共有的所有值。您可以使用common.length来确定可能有多少个。@phogz这样扩展array.prototype,如果在大块代码中使用,将导致一些兼容性问题。我建议您不要这样做。@Raynos它只会导致新手级代码中的兼容性问题,而愚蠢地尝试迭代使用for…in和b的数组,这样做并不意味着我们拥有自己的属性。非常好。在回答你的问题时,有一系列可用日期的复选框,然后复选框值存储在4月25日、4月26日等等。为了安全起见,我注意到你说了预订。假设这不是一个学术项目,您需要确保验证在服务器端进行。如果你相信javascript是权威的输入,我会去你的网站,给自己任何我想要的价格。不,这不是学术性的。费率是服务器端计算的,但我需要在客户端向他们显示总价,只是为了安全起见,我注意到你说的预订。假设这不是一个学术项目,您需要确保验证在服务器端进行。如果你相信javascript是权威的输入,我会去你的网站,给自己任何我想要的价格。不,这不是学术性的。费率是服务器端计算的,但我需要在客户端向他们显示总价