Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户数据管理和比较指南。Javascript_Javascript_Comparison - Fatal编程技术网

用户数据管理和比较指南。Javascript

用户数据管理和比较指南。Javascript,javascript,comparison,Javascript,Comparison,我正在寻找一个能为我指明正确方向的人来解决我正在使用javascript的一个小项目。我的想法是,我希望用户能够输入一些原始数据(已复制和粘贴)从一个网站到一个表单框或某种形式的输入在第一天,然后再次在第二天等 我希望JS做的是比较这两组数据并返回任何更改。比如说 第一天原始数据:(从网站复制粘贴) 第二天原始数据:(从网站复制粘贴) 我唯一要比较的数据是名称+信息行 Andy 29 5'9 $3200 low 比如说。其余的原始数据只是噪音,应该始终保持不变,例如页面顶部的链接和底部的页脚

我正在寻找一个能为我指明正确方向的人来解决我正在使用javascript的一个小项目。我的想法是,我希望用户能够输入一些原始数据(已复制和粘贴)从一个网站到一个表单框或某种形式的输入在第一天,然后再次在第二天等

我希望JS做的是比较这两组数据并返回任何更改。比如说

第一天原始数据:(从网站复制粘贴)

第二天原始数据:(从网站复制粘贴)

我唯一要比较的数据是名称+信息行

 Andy 29 5'9 $3200 low
比如说。其余的原始数据只是噪音,应该始终保持不变,例如页面顶部的链接和底部的页脚也包括字母表链接的A、B、C等

我希望结果如下:

结果:(打印到第页)

结果如何显示与实际数字无关。我在寻找实现这种数据比较的方法的建议,我忽略了原始输入的某些部分,并比较了那些重要的部分。使用新条目和删除的条目以及对重复条目的更改进行报告。唯一会改变的数据是原始数据中的人数——页眉、页脚和字母标记将始终存在

希望我解释得足够好,能指出正确的方向。谢谢您提前提供的帮助。

好的,这很麻烦(已经很晚了),但这可以满足您的需要,我想

有很大的空间来清理这个,所以把它作为一个方向正确的方向。关键是需要正则表达式来分析字符串。然后有相当数量的操纵来比较结果

<script>
var dayOne = `Welcome
Home
Contact
Info
A
Andy 29 5'9 $3000 low
B
Betty 19 4'8 $2800 low
Bella 23 5'2 £4300 medium
C
Charles 43 5'3 $5000 high
Your local date/time is Thu Jan 11 2018 20:58:14 GMT+0000 (GMT Standard Time).
Current server date/time is 11-01-2018 | 21:58 
Logout `;

var dayTwo = `

Welcome
Home
Contact
Info
A
Andy 29 5'9 $3200 low
B
Betty 19 4'8 $2900 low
Bella 23 5'2 £3900 high
C
Charles 43 5'3 $7000 high
Carrie 18 5'8 $1000 medium
Your local date/time is Thu Jan 11 2018 20:58:14 GMT+0000 (GMT Standard Time).
Current server date/time is 11-01-2018 | 21:58 
Logout `;

/**
* Converts an array to an object with keys for later comparison
*/
function convertNamesToKeys(arr){
    var obj = {}
    for(var i=0, j=arr.length; i<j; i+=1){
        var name = arr[i].substring(0,arr[i].indexOf(' '));
        obj[name] = arr[i];
    }
    return obj;
}

/**
* Count object length
*/
function getObjectLength(obj) {
    var length = 0;
    for( var key in obj ) {
        if( obj.hasOwnProperty(key) ) {
            length+=1;
        }
    }
    return length;
};


/**
* Compares two objects for differences in values
* retains objects with different keys
*/
function compareObjectValue(primaryObject, secondaryObject){

    for(var name in primaryObject){
        if( primaryObject.hasOwnProperty(name) 
            && name in secondaryObject){

            if(primaryObject[name] === secondaryObject[name]){
                delete primaryObject[name];
            }
        }
    }

    //This is your final array which should contain just unique values between the two days
    console.log(primaryObject);

}
//split the large string into lines for manageability and simplicity of regex
var dayOneArray = dayOne.match(/[^\r\n]+/g);
var dayTwoArray = dayTwo.match(/[^\r\n]+/g);

//discard any lines which are noise
var regex = /^[a-z\s0-9']+(\$|£)[0-9\sa-z]+$/i
var dayOneFiltered = dayOneArray.filter(line => regex.test(line));
var dayTwoFiltered = dayTwoArray.filter(line => regex.test(line));

//convert the arrays into objects using name as key for easy comparison
var dayOneConverted = convertNamesToKeys(dayOneFiltered);
var dayTwoConverted = convertNamesToKeys(dayTwoFiltered);

//Determine which of the two objects is the larger and loop that one
//We will unset keys which have values that are the same and leave keys 
//in the larger array which must be unique - not sure if you want that?
if( getObjectLength(dayOneConverted) > getObjectLength(dayTwoConverted)){
    compareObjectValue(dayOneConverted, dayTwoConverted)
}
else {
    compareObjectValue(dayTwoConverted, dayOneConverted);
}

</script>

var dayOne=`欢迎
家
接触
信息
A.
安迪29 5尺9寸3000美元低
B
贝蒂19 4尺8低2800美元
贝拉23.5英尺2英寸,4300英镑中等
C
查尔斯43.5英尺3美元高
您当地的日期/时间是2018年1月11日星期四20:58:14 GMT+0000(GMT标准时间)。
当前服务器日期/时间为2018年1月11日| 21:58
注销`;
变量第二天=`
欢迎
家
接触
信息
A.
安迪29 5尺9寸3200美元低
B
贝蒂19 4尺8寸2900美元低
贝拉23.5英尺2,3900英镑高
C
查尔斯43.5英尺3美元高
嘉莉18.5尺8寸1000美元中等
您当地的日期/时间是2018年1月11日星期四20:58:14 GMT+0000(GMT标准时间)。
当前服务器日期/时间为2018年1月11日| 21:58
注销`;
/**
*将数组转换为具有键的对象,以便以后比较
*/
功能转换器(arr){
var obj={}
对于(变量i=0,j=arr.length;i
 Andy 29 5'9 $3200 low
Andy 29 5'9 $3200 low --- (+ $200)
Betty 19 4'8 $2900 low --- (+ $100)
Bella 23 5'2 £3900 high --- (- $400 medium)
Charles 43 5'3 $7000 high --- (+ $2000)
Carrie 18 5'8 $1000 medium --- (**New Entry**)
<script>
var dayOne = `Welcome
Home
Contact
Info
A
Andy 29 5'9 $3000 low
B
Betty 19 4'8 $2800 low
Bella 23 5'2 £4300 medium
C
Charles 43 5'3 $5000 high
Your local date/time is Thu Jan 11 2018 20:58:14 GMT+0000 (GMT Standard Time).
Current server date/time is 11-01-2018 | 21:58 
Logout `;

var dayTwo = `

Welcome
Home
Contact
Info
A
Andy 29 5'9 $3200 low
B
Betty 19 4'8 $2900 low
Bella 23 5'2 £3900 high
C
Charles 43 5'3 $7000 high
Carrie 18 5'8 $1000 medium
Your local date/time is Thu Jan 11 2018 20:58:14 GMT+0000 (GMT Standard Time).
Current server date/time is 11-01-2018 | 21:58 
Logout `;

/**
* Converts an array to an object with keys for later comparison
*/
function convertNamesToKeys(arr){
    var obj = {}
    for(var i=0, j=arr.length; i<j; i+=1){
        var name = arr[i].substring(0,arr[i].indexOf(' '));
        obj[name] = arr[i];
    }
    return obj;
}

/**
* Count object length
*/
function getObjectLength(obj) {
    var length = 0;
    for( var key in obj ) {
        if( obj.hasOwnProperty(key) ) {
            length+=1;
        }
    }
    return length;
};


/**
* Compares two objects for differences in values
* retains objects with different keys
*/
function compareObjectValue(primaryObject, secondaryObject){

    for(var name in primaryObject){
        if( primaryObject.hasOwnProperty(name) 
            && name in secondaryObject){

            if(primaryObject[name] === secondaryObject[name]){
                delete primaryObject[name];
            }
        }
    }

    //This is your final array which should contain just unique values between the two days
    console.log(primaryObject);

}
//split the large string into lines for manageability and simplicity of regex
var dayOneArray = dayOne.match(/[^\r\n]+/g);
var dayTwoArray = dayTwo.match(/[^\r\n]+/g);

//discard any lines which are noise
var regex = /^[a-z\s0-9']+(\$|£)[0-9\sa-z]+$/i
var dayOneFiltered = dayOneArray.filter(line => regex.test(line));
var dayTwoFiltered = dayTwoArray.filter(line => regex.test(line));

//convert the arrays into objects using name as key for easy comparison
var dayOneConverted = convertNamesToKeys(dayOneFiltered);
var dayTwoConverted = convertNamesToKeys(dayTwoFiltered);

//Determine which of the two objects is the larger and loop that one
//We will unset keys which have values that are the same and leave keys 
//in the larger array which must be unique - not sure if you want that?
if( getObjectLength(dayOneConverted) > getObjectLength(dayTwoConverted)){
    compareObjectValue(dayOneConverted, dayTwoConverted)
}
else {
    compareObjectValue(dayTwoConverted, dayOneConverted);
}

</script>