[英]Compare elements of array
var getJSON = require('get-json')
var dateToday = new Date();
var dayToday = dateToday.getDate();
var monthToday = dateToday.getMonth() + 1;
var yearToday = dateToday.getFullYear();
const date = yearToday + "-" + monthToday + "-" + dayToday;
getJSON('https://betwatch.fr/getMoney?choice=Match%20Odds&date=' + date)
.then(function (response) {
getJSON('https://betwatch.fr/getMoney?choice=Over%2FUnder%202.5%20Goals&date=' + date)
.then(function (response2) {
getJSON('https://betwatch.fr/getMoney?choice=Both%20teams%20to%20Score%3F&date=' + date)
.then(function (response3) {
const IsEmptyArray = arr => !(typeof (arr) === 'object' && arr instanceof Array && arr.length > 0);
const getFieldMap = (arr, field) => {
arr = (!IsEmptyArray(arr)) ? arr : [];
const result = arr.reduce((result, elm) => {
const key = elm[field];
if (key) {
result[key] = elm;
}
return result;
}, {});
return result;
};
var array1 = JSON.parse(response);
var array2 = JSON.parse(response2);
var array3 = JSON.parse(response3);
const arr1Map = getFieldMap(array1, 'm');
const arr2Map = getFieldMap(array2, 'm');
const arr3Map = getFieldMap(array3, 'm');
const maxArr = (array1.length > array2.length) ? ((array1.length > array3.length) ? array1 : array2) : ((array2.length > array3.length) ? array2 : array3);
delete array1;
delete array2;
delete array3;
const matchingObjects = maxArr.reduce((matchingObjects, ele) => {
key = ele.m;
if (arr1Map[key] && arr2Map[key] && arr3Map[key]) {
ele['n_arr'] = [arr1Map[key]['i'], arr2Map[key]['i'], arr3Map[key]['i']]
ele['n_arr_vm'] = [arr1Map[key]['vm'], arr2Map[key]['vm'], arr3Map[key]['vm']]
matchingObjects.push(ele);
}
return matchingObjects;
}, [])
console.log(matchingObjects);
}).catch(function (error) {
console.log(error);
});
}).catch(function (error) {
console.log(error);
});
}).catch(function (error) {
console.log(error);
});
Given these 3 arrays (GOT FROM API) I would like to check for 'm' in array2 and array3.给定这 3 个 arrays(从 API 获取)我想检查 array2 和 array3 中的“m” 。
If 'm' is present in all three then return the matching objects.如果“m”在所有三个中都存在,则返回匹配的对象。
I tried to do this with the loop but I only get the position matches.我试图用循环来做到这一点,但我只得到 position 场比赛。
Example: FIRST ARRAY 'm' = Juventus - Roma present in all three arrays.示例:FIRST ARRAY 'm' = Juventus - Roma 出现在所有三个 arrays 中。
I would like to derive the objects 'i' from the other two always in correspondence with the example matches.我想从其他两个始终与示例匹配对应的对象中派生出对象“i” 。
var getJSON = require('get-json')
const IsEmptyArray = arr => !(typeof (arr) === 'object' && arr instanceof Array && arr.length > 0);
const getFieldMap = (arr, field) => {
arr = (!IsEmptyArray(arr)) ? arr : [];
const result = arr.reduce((result, elm) => {
const key = elm[field];
if (key) {
result[key] = elm;
}
return result;
}, {});
return result;
};
const getMatchingObjects = async () => {
var dateToday = new Date();
var dayToday = dateToday.getDate();
var monthToday = dateToday.getMonth() + 1;
var yearToday = dateToday.getFullYear();
const date = yearToday + "-" + monthToday + "-" + dayToday;
const [array1, array2, array3] = await Promise.all(
[
getJSON('https://betwatch.fr/getMoney?choice=Match%20Odds&date=' + date),
getJSON('https://betwatch.fr/getMoney?choice=Over%2FUnder%202.5%20Goals&date=' + date),
getJSON('https://betwatch.fr/getMoney?choice=Both%20teams%20to%20Score%3F&date=' + date)
]
)
const arr1Map = getFieldMap(array1, 'm');
const arr2Map = getFieldMap(array2, 'm');
const arr3Map = getFieldMap(array3, 'm');
const maxArr = (array1.length > array2.length) ? ((array1.length > array3.length) ? array1 : array2) : ((array2.length > array3.length) ? array2 : array3);
delete array1;
delete array2;
delete array3;
const matchingObjects = maxArr.reduce((matchingObjects, ele) => {
key = ele.m;
if (arr1Map[key] && arr2Map[key] && arr3Map[key]) {
const obj = {};
for (const [k, value] of Object.entries(ele)) {
let fieldValues;
if(value instanceof Array){
fieldValues = new Set([...arr1Map[key][k], ...arr2Map[key][k], ...arr3Map[key][k]]);
}else{
fieldValues = new Set([arr1Map[key][k], arr2Map[key][k], arr3Map[key][k]]);
}
if (fieldValues.size === 1) {
obj[k] = [...fieldValues][0];
} else {
obj[`${k}_arr`] = [...fieldValues];
}
}
matchingObjects.push(obj);
}
return matchingObjects;
}, [])
return matchingObjects;
}
getMatchingObjects().then(resp => {
console.log(resp);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.