gpt4 book ai didi

javascript - 用户数据管理和比较指南。 JavaScript

转载 作者:行者123 更新时间:2023-12-03 02:43:21 28 4
gpt4 key购买 nike

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

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

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

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

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

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

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

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

我希望结果如下:

结果:(打印到页面)

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>

关于javascript - 用户数据管理和比较指南。 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48216260/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com