代码之家  ›  专栏  ›  技术社区  ›  Muirik

比较嵌入式数组值并获取目标值

  •  1
  • Muirik  · 技术社区  · 7 年前

    我正在使用以下代码检查嵌入在另一个数组中的数组是否相等:

    const equal = take("services", compare(
      take("history", compare(
        (a, b) => a._id === b._id
      ))
    ))(obj, obj2);
    

    const obj = {
      _id: 1,
      services: [
        {
          _id: 23,
          service: "serviceOne",
          history: [
            {
              _id: 33,
              stage: "stageOne"
            }
          ]
        },
            {
          _id: 24,
          service: "serviceTwo",
          history: [
            {
              _id: 44,
              stage: "stageOne"
            }
          ]
        },
      ]
    };
    
    const obj2 = {
      _id: 1,
      services: [
        {
          _id: 23,
          service: "serviceOne",
          history: [
            {
              _id: 33,
              stage: "stageOne"
            }
          ]
        },
            {
          _id: 24,
          service: "serviceTwo",
          history: [
            {
              _id: 45,
              stage: "stageTwo"
            },
            {
              _id: 44,
              stage: "stageOne"
            }
          ]
        },
      ]
    };
    

    上述措施正在发挥作用。当我跑的时候 console.log('equal: ', equal) 它记录 false history 大堆

    const compare = cb => (a, b) => a.length === b.length && a.every((el, i) => cb(el, b[i]));
    const take = (key, cb) => (a, b) => cb(a[key], b[key]);
    const obj = {
      _id: 1,
      services: [{
          _id: 23,
          service: "serviceOne",
          history: [{
            _id: 33,
            stage: "stageOne"
          }]
        },
        {
          _id: 24,
          service: "serviceTwo",
          history: [{
            _id: 44,
            stage: "stageOne"
          }]
        },
      ]
    };
    
    const obj2 = {
      _id: 1,
      services: [{
          _id: 23,
          service: "serviceOne",
          history: [{
            _id: 33,
            stage: "stageOne"
          }]
        },
        {
          _id: 24,
          service: "serviceTwo",
          history: [{
              _id: 45,
              stage: "stageTwo"
            },
            {
              _id: 44,
              stage: "stageOne"
            }
          ]
        },
      ]
    };
    const targetService = take("services", compare(
      take("history", compare(
        (a, b) => {
          a._id === b._id;
          console.log(a);
        }
      ))
    ))(obj, obj2);
    console.log(targetService);

    在这种情况下,我如何获得“服务。服务”?根据我这里的文件,我想回去 serviceTwo ,因为它是 历史

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jonas Wilms    7 年前

    如果您想保留该功能模式,您可能需要添加一个 and 允许您执行多重比较的帮助器:

    const compare = cb => (a, b) => a.length === b.length && a.every((el, i) => cb(el, b[i]));
    const take = (key, cb) => (a, b) => cb(a[key], b[key]);
    const and = (cb1, cb2) => (a, b) => cb1(a, b) && cb2(a, b);
    const equals = (a, b) => a === b;
    
    const equal = take("services", compare( 
      and(
       take("service", equals),
       take("history", compare(
         take("_id", equals)
       ))
      )
    ))(obj, obj2);
    

     const historyEquals = (a, b) =>
       a._id === b._id;
    
     const serviceEquals = (a, b) => 
        a.service === b.service &&
        a.history.length === b.history.length &&
        a.history.every((el, i) => historyEquals(el, b.history[i]));
    
     const objEquals = (a, b) =>
        a.services.length === b.services.length &&
        a.services.every((el, i) => serviceEquals(el, b.services[i]));