Javascript 获取不同大小的多个数组之间的数组差异

Javascript 获取不同大小的多个数组之间的数组差异,javascript,lodash,underscore.js,ramda.js,Javascript,Lodash,Underscore.js,Ramda.js,这件事已经绕圈子有一段时间了 例如,假设我存储了不同的时隙 let timechunks1 = [["09:00", "10:00", "11:00"], ["09:00", "10:00", "11:00"], ["09:00", "10:00", "11:00", "12:00"]] let

这件事已经绕圈子有一段时间了

例如,假设我存储了不同的时隙

let timechunks1 = [["09:00", "10:00", "11:00"], ["09:00", "10:00", "11:00"], ["09:00", "10:00", "11:00", "12:00"]] 
let timechunks2 = [["09:00", "10:00", "11:00"], ["09:00", "10:00", "11:00"], ["09:00", "10:00"]]


 for (let i=0; i< timechunks.length; i++) {
 differences.push(_.difference(timechunks[0], timechunks[i]))      
}

它将返回[“13:00”,“12:00”]

这里有一种不变的方法

function findTimeSlot(timechunks){
    const slots = _.map(_.zip(...timechunks), _.compact);
    const maxChunk = _.max(_.map(slots, slot => slot.length));

    return _.unique(_.find(slots, (slot) => slot.length < maxChunk));
}
函数findTimeSlot(时间块){
const slots=..map(..zip(…时间块),..compact);
const maxChunk=u.max(u.map(slot,slot=>slot.length));
返回0.unique(0.find(slot,(slot)=>slot.length
返回

[“11:00”]
用于
findTimeSlot(timechunks2)

[“12:00”]
用于
查找时间段(时间块1)


让我们知道它是否有效。

这里有一个不变的方法

function findTimeSlot(timechunks){
    const slots = _.map(_.zip(...timechunks), _.compact);
    const maxChunk = _.max(_.map(slots, slot => slot.length));

    return _.unique(_.find(slots, (slot) => slot.length < maxChunk));
}
function findUniqueTimeSlots(timechunks) {
  const hm = {};
  timechunks.flat(1).forEach(el => {
    if (hm.hasOwnProperty(el)) {
      hm[el] += 1;
    } else {
      hm[el] = 1;
    }
  });
  return Object.keys(hm).filter(key => hm[key] === 1);
}
函数findTimeSlot(时间块){
const slots=..map(..zip(…时间块),..compact);
const maxChunk=u.max(u.map(slot,slot=>slot.length));
返回0.unique(0.find(slot,(slot)=>slot.length
返回

[“11:00”]
用于
findTimeSlot(timechunks2)

[“12:00”]
用于
查找时间段(时间块1)


让我们知道它是否有效。

您可以通过以下步骤来实现:

function findUniqueTimeSlots(timechunks) {
  const hm = {};
  timechunks.flat(1).forEach(el => {
    if (hm.hasOwnProperty(el)) {
      hm[el] += 1;
    } else {
      hm[el] = 1;
    }
  });
  return Object.keys(hm).filter(key => hm[key] === 1);
}
  • 查找公共时间块,
    \交叉点(…时间块)
  • 迭代时间块并找出每个时间块与公共时间块的差异(时间块,公共时间块)
下面的代码片段可以帮助您

const timechunks1=[
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00", "12:00"],
]
常量时间块S2=[
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00"],
["09:00", "10:00"],
]
常量时间块S3=[
["09:00", "10:00", "11:00", "13:00"],
["09:00", "10:00", "11:00", "13:00"],
["09:00", "10:00", "11:00", "12:00"],
]
常量getDifferences=(时间块)=>
_.chain(时间块)
.map((timechunk)=>(timechunk,intersection(…timechunk)))
.flatte()
.uniq()
.value()
console.log(getDifferences(timechunks1))
日志(getDifferences(timechunks2))
console.log(getDifferences(timechunks3))

您可以通过以下步骤实现:

  • 查找公共时间块,
    \交叉点(…时间块)
  • 迭代时间块并找出每个时间块与公共时间块的差异(时间块,公共时间块)
下面的代码片段可以帮助您

const timechunks1=[
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00", "12:00"],
]
常量时间块S2=[
["09:00", "10:00", "11:00"],
["09:00", "10:00", "11:00"],
["09:00", "10:00"],
]
常量时间块S3=[
["09:00", "10:00", "11:00", "13:00"],
["09:00", "10:00", "11:00", "13:00"],
["09:00", "10:00", "11:00", "12:00"],
]
常量getDifferences=(时间块)=>
_.chain(时间块)
.map((timechunk)=>(timechunk,intersection(…timechunk)))
.flatte()
.uniq()
.value()
console.log(getDifferences(timechunks1))
日志(getDifferences(timechunks2))
console.log(getDifferences(timechunks3))

您可以在
交叉点
差异
uniq
的实现基础上构建此功能。以下是我如何使用Ramda的版本:

const notCommon=(xs,common=xs.reduce(intersection))=>
uniq(xs.flatMap(x=>difference(x,common)))
常量时间块1=[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”、“12:00”]]
常量时间块2=[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”]
常量时间块3=[“09:00”、“10:00”、“11:00”、“13:00”]、[“09:00”、“10:00”、“11:00”、“13:00”]、[“09:00”、“10:00”、“11:00”、“12:00”]]
console.log(notCommon(timechunks1))
console.log(notCommon(timechunks2))
console.log(notCommon(timechunks3))


const{intersection,uniq,difference}=R
您可以在
intersection
difference
uniq
的实现上构建它。以下是我如何使用Ramda的版本:

const notCommon=(xs,common=xs.reduce(intersection))=>
uniq(xs.flatMap(x=>difference(x,common)))
常量时间块1=[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”、“12:00”]]
常量时间块2=[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”、“11:00”]、[“09:00”、“10:00”]
常量时间块3=[“09:00”、“10:00”、“11:00”、“13:00”]、[“09:00”、“10:00”、“11:00”、“13:00”]、[“09:00”、“10:00”、“11:00”、“12:00”]]
console.log(notCommon(timechunks1))
console.log(notCommon(timechunks2))
console.log(notCommon(timechunks3))


const{intersection,uniq,difference}=R
在集合论术语中,您要寻找的是。通过取并集并减去交点,可以找到此集合:

function symmetricDifference(...sets) {
    return _.difference(_.union(...sets), _.intersection(...sets));
}

用集合论的术语来说,你要找的是。通过取并集并减去交点,可以找到此集合:

function symmetricDifference(...sets) {
    return _.difference(_.union(...sets), _.intersection(...sets));
}

请注意,您在相同的值上重复调用
intersection
。请注意,您在相同的值上重复调用
intersection
。谢谢!学到了一些新的东西:)看到维恩图有助于更好地可视化它)如果我知道它是如何调用的,我会发现Lodash甚至有一个函数,这个函数叫做uxor(),但没有一路走,你说的“没有一路走”是什么意思?谢谢!学到了一些新的东西:)看到维恩图有助于更好地可视化它)如果我知道它是如何调用的,我会发现Lodash甚至有一个函数,这个函数叫做uxor(),但没有一路走,你说的“没有一路走”是什么意思?谢谢!相当强大的数据操作将更深入地研究ramda!这涵盖了所有