【问题标题】:compare two Object array and match value比较两个对象数组并匹配值
【发布时间】:2019-04-03 14:49:13
【问题描述】:

我有两个数组对象,如下所示

var a={
  firstObj: 
    [
      {
        Ref: "5ca3cd6aefbc9f1782b5db53",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      }
    ]  
};

var b={
  secondObj: [
    {
      _id: "5ca3cd6aefbc9f1782b5db53"
    },
    {
      _id: "5ca3cdc6efbc9f1782b5db5c"
    },    
  ]
}

我想检查 a.firstObj 是否有匹配的 Ref 到 b.secondObj._id 如果有,那么我试图分配给 firstObj 元素以匹配 b.secondObj.new 但不知何故 _id 不匹配

我正在尝试通过地图

a.firstObj.map(item =>
               b.secondObj.map((_item, index) => {
               console.log(_item._id.toString());
               console.log(item.Ref.toString());
               if (_item._id == item.Ref) {
                   b.secondObj[index].new = item;
                    }
                })
);

【问题讨论】:

  • item.orderRefitem.Ref
  • JSON 是 JavaScript 对象表示法,是 JavaScript 对象的字符串表示。我在这里没有看到任何 JSON,只有 JavaScript 对象。
  • 如果可能的话,您可以在 b.secondObj 中使用字符串数组而不是对象数组,对 a 中的每个项目执行 b.secondObj.includes("refFromA") 会变得更简单。第一个对象。
  • @Ab.Progr 我无法从对象更改为来自 API 的数组
  • 你想比较来自 b.secondObj 的 _id 和来自 a.firstObj 的 Ref 吗?

标签: javascript arrays json mongodb


【解决方案1】:

AFAI 测试...是的,它是匹配的。看看:

var a={
  firstObj: 
    [
      {
        Ref: "5ca3cd6aefbc9f1782b5db53",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      }
    ]  
};

var b={
  secondObj: [
    {
      _id: "5ca3cd6aefbc9f1782b5db53"
    },
    {
      _id: "5ca3cdc6efbc9f1782b5db5c"
    },    
  ]
}


a.firstObj.map(item =>
               b.secondObj.map((_item, index) => {
               //console.log(_item._id.toString());
               //console.log(item.Ref.toString());
               if (_item._id == item.Ref) {
                   b.secondObj[index].new = item;
                    }
                })
);

console.log(b)

你可能错过了什么。如果您还有什么想知道的,请重新表述,我会更改答案。

请理解,使用此代码,由于一个对象有许多相等的 Ref 元素,因此关联将与最后一个 Ref 关联。

【讨论】:

  • 我也验证过,您的 sn-p 似乎工作正常。如果还有其他需要,您可以修改您的问题。
【解决方案2】:

你可以用一个内部的reduce 做一个map

var a={
  firstObj: 
    [
      {
        Ref: "5ca3cd6aefbc9f1782b5db53",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      },
      {
        Ref: "5ca3cdc6efbc9f1782b5db5c",
        status: "hhhh"
      }
    ]  
};

var b={
  secondObj: [
    {
      _id: "5ca3cd6aefbc9f1782b5db53"
    },
    {
      _id: "5ca3cdc6efbc9f1782b5db5c"
    },    
  ]
}


function mergeByEqProp (propA, propB, listA, listB) {
  return listB.map(function (b) {
    return listA.reduce(function (acc, a) {
      if (!acc && a[propA] === b[propB]) {
        return Object.assign({}, b, {new: a});
      }
      return acc;
    }, null);
  });
}


console.log(mergeByEqProp('Ref', '_id', a.firstObj, b.secondObj));

它首先maps 遍历第二个列表,然后reduces 遍历第一个列表中的项目,直到找到匹配项。一旦找到匹配项,它总是返回。我添加了该功能,因为它看起来在a.firstObj 中有一些重复(仔细查看Ref 属性)。如果没问题,只需将if (!acc && a[propA] === b[propB]) { 部分更改为if (a[propA] === b[propB]) {

Object.assign 部分也值得解释一下:

首先,如果您在 ES6/ES2015+ 环境中工作,则需要另一个函数作为替代 - 例如 $.extend 效果很好。或者自己写,比如:

function assign (a /*, ...rest */) {
  var r = Array.prototype.slice.call(arguments, 1);
  return r.reduce(function (acc, x) {
    for (var y in x) {
      if (x.hasOwnProperty(y)) { acc[y] = x[y]; }
    }
    return acc;
  }, a);
}

要解释的另一件事是空的{}:它只是首先创建一个副本并更改该副本而不是原始项目。如果这不打扰您,只需使用Object.assign(b, a)(或您自己的assign(b, a))。

【讨论】:

    【解决方案3】:

    /* 此代码将识别所有与 Ref id 匹配的项目,然后将它们作为数组分配给 secondObj 内的新属性 */

    <script type="text/javascript">
    var a={
      firstObj: 
        [
          {
            Ref: "5ca3cd6aefbc9f1782b5db53",
            status: "abcd"
          },
          {
            Ref: "5ca3cdc6efbc9f1782b5db5c",
            status: "efgh"
          },
          {
            Ref: "5ca3cdc6efbc9f1782b5db5c",
            status: "hijk"
          },
          {
            Ref: "5ca3cdc6efbc9f1782b5db5c",
            status: "lmno"
          }
        ]  
    };
    
    var b={
      secondObj: [
        {
          _id: "5ca3cd6aefbc9f1782b5db53"
        },
        {
          _id: "5ca3cdc6efbc9f1782b5db5c"
        },    
      ]
    }
    
    let equality = [];
    
    for(let i=0; i<b.secondObj.length ; i++){
        let itemB = b.secondObj[i];
        equality.push(a.firstObj.filter(itemA => itemA.Ref == itemB._id)) ;
    }
    equality.map((value, index) => b.secondObj[index].new = value); //<=== in case if there are more than one equal items in firstObj
    
    //equality.map((value, index) => b.secondObj[index].new = value[0]); <== only asign one item to 'new' 
     
    console.log(b);
    
    </script>

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2019-01-04
      • 1970-01-01
      • 2021-11-02
      • 1970-01-01
      • 2018-05-18
      相关资源
      最近更新 更多