我有两个对象数组,它们是:
对象 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", "数量":"1" }, {"位置">
:"位置臂左", "科技":"刺绣" }, {"位置">
:"位置胸部", "科技":"丝网印刷纺织品" } ]
对象 2:
[
{
"position":"Position Arm Left",
"tech":"Embroidery"
},
{
"position":"Position Chest",
"tech":"Screenprint Textile"
}
]
我需要比较对象参数,即位置和技术,并需要获得该位置和对象可用的最终数组,如下所示
最终输出:
[
{
"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"
}
]
如果您使用 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>
试试这个:
代码非常不言自明。
编辑: 代码现在更有效率,我们确定两个数组的长度,并以更少的对象运行循环。
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))
试试这个,我认为这将帮助您获得您想要的答案。
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);