Javascript 比较两个对象数组并返回该项

Javascript 比较两个对象数组并返回该项,javascript,ecmascript-6,Javascript,Ecmascript 6,我有一个如下所示的对象数组: let products = [ { "id": 7, "name": "product 1", "description": "description product 1", "images": [ { "id": 1, "path": "image1-product1.jpeg", }, { "id"

我有一个如下所示的对象数组:

let products = [
    {
      "id": 7,
      "name": "product 1",
      "description": "description product 1",
      "images": [
        {
          "id": 1,
          "path": "image1-product1.jpeg",
        },
        {
          "id": 2,
          "path": "image2-product1.jpeg",
        }
      ]
     },
     {
      "id": 20,
      "name": "product 2",
      "description": "description product 2",
      "images": [
        {
          "id": 3,
          "path": "image1-product2.jpeg",
        },
        {
          "id": 4,
          "path": "image2-product2.jpeg",
        }
      ]
     }
  ]
每个产品都有一个图像阵列,我需要将这个图像阵列与我将收到的作为参数的图像阵列进行比较,它看起来与其中一个完全相同,以了解它属于哪个产品并返回该产品。 例如,如果我收到此阵列:

[
  {
    "id": 3,
    "path": "image1-product2.jpeg",
  },
  {
    "id": 4,
    "path": "image2-product2.jpeg",
  }
]

等于产品2的图像数组,那么如何比较这些图像并返回该产品?

如果我理解正确的话。您将得到一个包含id和图像的对象。此对象位于某个产品图像中的某个位置,是否要查找/返回包含此图像的产品

如果是这样,您可以组合.find数组原型来查找通过条件的对象,其中一个图像就是您提供的图像

让产品=[{ id:7, 名称:产品1, 描述:描述产品1, 图像:[{ id:1, 路径:image1-product1.jpeg, }, { id:2, 路径:image2-product1.jpeg, } ] }, { 身份证号码:20, 名称:产品2, 描述:描述产品2, 图像:[{ id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] } ] const compare=imageObjToFind=>{ return products.findproduct=>product.images.findimage=>JSON.stringifyimage===JSON.stringifyimageObjToFind } const Iwant比较此={ id:4, 路径:image2-product2.jpeg, }
console.logcompareIwantToCompareThis如果我理解正确的话。您将得到一个包含id和图像的对象。此对象位于某个产品图像中的某个位置,是否要查找/返回包含此图像的产品

如果是这样,您可以组合.find数组原型来查找通过条件的对象,其中一个图像就是您提供的图像

让产品=[{ id:7, 名称:产品1, 描述:描述产品1, 图像:[{ id:1, 路径:image1-product1.jpeg, }, { id:2, 路径:image2-product1.jpeg, } ] }, { 身份证号码:20, 名称:产品2, 描述:描述产品2, 图像:[{ id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] } ] const compare=imageObjToFind=>{ return products.findproduct=>product.images.findimage=>JSON.stringifyimage===JSON.stringifyimageObjToFind } const Iwant比较此={ id:4, 路径:image2-product2.jpeg, }
console.logcompareIwantToCompareThis如果顺序无关紧要,并且可以假设所有id和路径都匹配并且需要精确。。。然后您可以比较每个数组的stringify。如果您需要更轻松的comapre,那么您必须在数组中循环更多的内容并比较各个值

让产品=[ { id:7, 名称:产品1, 描述:描述产品1, 图像:[ { id:1, 路径:image1-product1.jpeg, }, { id:2, 路径:image2-product1.jpeg, } ] }, { 身份证号码:20, 名称:产品2, 描述:描述产品2, 图像:[ { id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] } ] 让传入=[ { id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] 让结果为空 结果=产品。findproduct=>{ 返回JSON.stringifyproduct.images===JSON.stringifyincoming }
如果顺序不重要,并且可以假设所有id和路径都匹配,并且需要精确,则logresult。。。然后您可以比较每个数组的stringify。如果您需要更轻松的comapre,那么您必须在数组中循环更多的内容并比较各个值

让产品=[ { id:7, 名称:产品1, 描述:描述产品1, 图像:[ { id:1, 路径:image1-product1.jpeg, }, { id:2, 路径:image2-product1.jpeg, } ] }, { 身份证号码:20, 名称:产品2, 描述:描述产品2, 图像:[ { id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] } ] 让传入=[ { id:3, 路径:image1-product2.jpeg, }, { id:4, 路径:image2-product2.jpeg, } ] 让结果为空 结果=产品。findproduct=>{ 返回JSON.stringifyproduct.images===JSON.stringifyincoming } console.logresult如果通过id进行比较就足够了,那么使用比使用JSON.stringify更有效:

const productHasImages=images=>product=> product.images.length==images.length&& 产品、图像、每 image,i=>image.id==images[i].id ; const products=[{id:7,名称:'product 1',描述:'description product 1',图像:[{id:1,路径:'image1-product1.jpeg'},{id:2,路径:'image2-product1.jpeg'}],{id:20,名称:'product 2',描述:'description product 2',图像:[{id:3,路径:'image1-product2.jpeg'},{id:4,路径:'image2-product2.jpeg'}]; const images=[{id:3,路径:'image1-product2.jpeg'},{id:4,路径:'image2-product2.jpeg'}]; const product=products.findproductHasImagesimages; console.logproduct 如果通过id进行比较就足够了,那么使用比使用JSON.stringify更有效:

const productHasImages=images=>product=> product.images.length==images.length&& 产品、图像、每 image,i=>image.id==images[i].id ; const products=[{id:7,名称:'product 1',描述:'description product 1',图像:[{id:1,路径:'image1-product1.jpeg'},{id:2,路径:'image2-product1.jpeg'}],{id:20,名称:'product 2',描述:'description product 2',图像:[{id:3,路径:'image1-product2.jpeg'},{id:4,路径:'image2-product2.jpeg'}]; const images=[{id:3,路径:'image1-product2.jpeg'},{id:4,路径:'image2-product2.jpeg'}]; const product=products.findproductHasImagesimages; console.logproduct;