【问题标题】:JS - compare 2 arrays by key, return 4 arrays: matches & unmatches from eachJS - 按键比较2个数组,返回4个数组:每个数组匹配和不匹配
【发布时间】:2022-01-26 10:33:47
【问题描述】:

界面-

interface I {
    name: string;
    age: number;
    city: string;
    address?: string;
}

数组 -

const arr1: I[] = [
  {
    name: "daniel",
    age: 21,
    city: 'NYC'
  },
  {
    name: "kosta",
    age: 28,
    city: "NYC"
  },
  {
    name: "yoav",
    age: 28,
    city: "NYC"
  }
];

const arr2: I[] = [{
    name: "daniel",
    age: 21,
    city: "NYC",
    address: 'E. 43'
  },
  {
    name: "simon",
    age: 24,
    city: "NYC",
    address: 'E. 43'
  },
  {
    name: "david",
    age: 22,
    city: "NYC",
    address: 'E. 43'
  },
  {
    name: "kosta",
    age: 28,
    city: "NYC",
    address: 'E. 43'
  }
];

获取数组的键 -

const arr1Map: ReadonlyMap<string, string | undefined> = new Map(
    arr1.map(
        ({
            name, age, city, address
        }) => [
            `${name}|${age}|${city}`,
            address
        ]
    )
);

const arr2Map: ReadonlyMap<string, string | undefined> = new Map(
    arr2.map(
        ({
            name, age, city, address
        }) => [
            `${name}|${age}|${city}`,
            address
        ]
    )
);

空数组 -

let arr1Match: I[] = []
let arr1Unmatch: I[] = []
let arr2Match: I[] = []
let arr2Unmatch: I[] = []

我现在需要做的是将arr1中的所有值存储到arr2中,如果有匹配项,则将arr1中的匹配项存储在arr1Match中,并将arr2中的匹配项存储在@987654331中@。如果存在不匹配,我需要将不匹配的arr1 存储在arr1Unmatch 中,并将来自arr2 的不匹配存储在arr2Unmatch 中。 如果有匹配项,我需要将addressarr2 存储到arr1

想要的输出 -

arr1Match:[{ name: "daniel", age: 21, city: "NYC", address: 'E. 43' }, { name: "kosta", age: 28, city: "NYC", address: 'E. 43' } ]

arr2Match:[{ name: "daniel", age: 21, city: "NYC", address: 'E. 43' }, { name: "kosta", age: 28, city: "NYC", address: 'E. 43' }]

arr1Unmatch:[{ name: "yoav", age: 28, city: "NYC" }]

arr2Unmatch: [{ name: "simon", age: 24, city: "NYC", address: 'E. 43' }, { name: "david", age: 22, city: "NYC", address: 'E. 43' }]

【问题讨论】:

  • 什么是匹配?无论哪种方式,您都应该创建一个函数来检查 2 个值是否匹配,然后在两个数组上循环两次并检查每对值
  • 今天不是已经问过这个问题了吗? - Here?
  • 旧答案是怎么回事?什么不合适?
  • @evolutionxbox 不,这不是同一个问题
  • @heyheyhey 你说得对,不是(我傻了),但你今天早些时候肯定已经问过了。

标签: javascript arrays typescript key


【解决方案1】:

答案取决于有关您需求的一些问题:什么构成匹配?如果匹配之间有不同的数据,应该在匹配数组中放入什么?数组应该指向原始对象还是它们的副本?
另外,arr1Matcharr2Match 之间似乎没有区别,所以它们可以合二为一

无论哪种方式,解决方案都是遍历一个数组,并在另一个数组中搜索每个值的匹配项。任何不匹配的项目都将进入不匹配数组

// Replace with real match logic
const isMatch = <T>(a: T, b: T) => Math.random() < 0.5;

const getMatches = <T>(arrOne: T[], arrTwo: T[]) => {
  const matches: T[] = [];
  const arrOneUnmatches: T[] = [];
  let arrTwoUnmatches: T[];

  // Copying for comfortability's sake
  const arrTwoCopy = [...arrTwo];

  arrOne.forEach(item => {
    // Find a match in array two
    const arrTwoMatchIndex = arrTwoCopy.findIndex(arrTwoItem => isMatch(item, arrTwoItem));
    if (arrTwoMatchIndex) {
      matches.push(item);

      // Remove it from arrTwoCopy, to maintain arrTwoUnmatches
      arrTwoCopy.splice(arrTwoMatchIndex, 1);
    } else {
      // No match = go to arrOneUnmatches
      arrOneUnmatches.push(item);
    }
  })

  // Anyone left in arrTwoCopy didn't match anyone in arrOne, so they have no match
  arrTwoUnmatches = arrTwoCopy;

  return { matches, arrOneUnmatches, arrTwoUnmatches }
}

【讨论】:

  • 没有将addressarr2 分配到arr1。所需输出 - arr1Match:[{ name: "daniel", age: 21, city: "NYC", address: 'E. 43' }, { name: "kosta", age: 28, city: "NYC", address: 'E. 43' } ] 当前输出 - [ { "name": "daniel", "age": 21, "city": "NYC" }, { "name": "kosta", "age": 28, "city": "NYC" } ]
  • @heyheyhey 好吗?所以改变它以满足您的需求。我给了你几乎所有你需要的东西
猜你喜欢
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2020-01-02
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
相关资源
最近更新 更多