如何使用js比较数组并创建最终数组?

时间:2019-08-24 12:40:44

标签: javascript arrays json object compare

我有两个对象数组:

对象1:

    [  
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Screenprint Textile"
   }
]

对象2: [
   {
      “ sku”:“ 30772”,       “ qty”:“ 1”    },    {
      “ position”:“ Position Arm Left”,       “ tech”:“刺绣”    },    {
      “ position”:“ Position Chest”,       “ tech”:“丝网印刷纺织品”    } ]

对象2:

[  
   {  
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   }
]

我需要比较对象参数,即position和tech,并需要获得最终的数组,其中该位置和对象如下所示

最终输出:

[  

   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Embroidery"
   }
]

3 个答案:

答案 0 :(得分:2)

如果使用lodash,则可以使用intersectionWith方法,因为很直观地希望基于两个键进行交点。

const object1 = [
  {
    id: "30772",
    posimage: "/b/l/blue-shirt_1_1.jpg",
    position: "Position Chest",
    tech: "Embroidery"
  },
  {
    id: "30772",
    posimage: "/b/l/blue-shirt_1_1.jpg",
    position: "Position Chest",
    tech: "Screenprint Textile"
  },
  {
    id: "30772",
    posimage: "/b/l/blue-shirt_1_1.jpg",
    position: "Position Arm Left",
    tech: "Embroidery"
  },
  {
    id: "30772",
    posimage: "/b/l/blue-shirt_1_1.jpg",
    position: "Position Arm Left",
    tech: "Screenprint Textile"
  }
];

const object2 = [
  {
    position: "Position Arm Left",
    tech: "Embroidery"
  },
  {
    position: "Position Chest",
    tech: "Screenprint Textile"
  }
];

const result = _.intersectionWith(
  object1,
  object2,
  (o1, o2) => o1.position === o2.position && o1.tech === o2.tech
);

console.log(result);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

答案 1 :(得分:1)

尝试一下,我想这将帮助您获得想要的答案。

const object1 = [  
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Screenprint Textile"
   }
];

const object2 = [  
   {  
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   }
];

const findObject = object1.filter(obj1 => {
  const mathObject = object2.find(obj2 => {
    return obj2.tech === obj1.tech && obj2.position === obj1.position;
  });
  return mathObject;
});

console.log(findObject);

答案 2 :(得分:1)

尝试一下:

代码很不言自明。

编辑: 代码现在更加高效,我们确定了两个数组的长度,并使用更少的对象数运行了循环。

var obj1 =   [  
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "id":"30772",
      "posimage":"/b/l/blue-shirt_1_1.jpg",
      "position":"Position Arm Left",
      "tech":"Screenprint Textile"
   }
]

var obj2 = [  
   {  
      "position":"Position Arm Left",
      "tech":"Embroidery"
   },
   {  
      "position":"Position Chest",
      "tech":"Screenprint Textile"
   }
]

const doer = (ob1, ob2) => {
  
  let final = [];
  ob1.map((one) => {
    // let tobepushed = one.hasOwnPropery('id') ? one : two;
    ob2.map(two => {
      if(two.hasOwnProperty('position') && 
          two.hasOwnProperty('tech') && 
          two['position'] === one['position'] && 
          two['tech'] === one['tech']
        ) {
        final.push('id' in one ? one : two);
      }
    })
  })

  return final;
}

let l1 = obj1.length;
let l2 = obj2.length

if(l1 < l2) {
  console.log(doer(obj2, obj1))
} else if (l2 < l1) {
  console.log(doer(obj1, obj2))
}

// console.log(doer(obj2, obj1))