1、两个简单数组对比
// 获取两个数组的相同项(简单数据格式)
export const getArrEqual = (arr1, arr2) => {
let newArr = [];
for (let i = 0; i < arr2.length; i++) {
for (let j = 0; j < arr1.length; j++) {
if (arr1[j] === arr2[i]) {
newArr.push(arr1[j]);
}
}
}
return newArr;
};
2、两个json数组对比
// 获取两个JSON数组的相同项
export const getJsonArrEqual = (arr1, arr2) => {
var newArr = [], kvIndex = {};
for (var i = 0; i < arr1.length; i++) {
for (var j = 0; j < arr2.length; j++) {
if (arr1[i].title == arr2[j].title) {
var item
if (kvIndex[arr1[i].title] == undefined) {
kvIndex[arr1[i].title] = newArr.length;
item = {};
for (var attr in arr1[i]) item[attr] = arr1[i][attr];
newArr[kvIndex[arr1[i].title]] = item;
} else {
item = newArr[kvIndex[arr1[i].title]];
for (var attr in arr2[j]) item[attr] = arr2[j][attr];
}
}
}
}
return newArr
}
人间四月天, 跨越山河拥抱你。
|