Javascript 如何比较数组并使用js创建最终数组?
我有两个对象数组,它们是: 目标1:Javascript 如何比较数组并使用js创建最终数组?,javascript,arrays,json,object,compare,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", "positio
[
{
"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"
}
]
试试这个,我想这会帮助你得到你想要的答案
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);
试试这个:
这段代码很容易解释
编辑:现在的代码效率更高,我们可以确定两个数组的长度,并使用更少的对象运行循环。
var obj1=[
{
“id”:“30772”,
“posimage”:“/b/l/blue-shirt_1_1.jpg”,
“位置”:“胸部位置”,
“技术”:“刺绣”
},
{
“id”:“30772”,
“posimage”:“/b/l/blue-shirt_1_1.jpg”,
“位置”:“胸部位置”,
“技术”:“丝印纺织品”
},
{
“id”:“30772”,
“posimage”:“/b/l/blue-shirt_1_1.jpg”,
“位置”:“左摆臂位置”,
“技术”:“刺绣”
},
{
“id”:“30772”,
“posimage”:“/b/l/blue-shirt_1_1.jpg”,
“位置”:“左摆臂位置”,
“技术”:“丝印纺织品”
}
]
变量obj2=[
{
“位置”:“左摆臂位置”,
“技术”:“刺绣”
},
{
“位置”:“胸部位置”,
“技术”:“丝印纺织品”
}
]
const doer=(ob1,ob2)=>{
设final=[];
ob1.地图((一)=>{
//让tobepushed=1.hasownproperty('id')?1:2;
ob2.map(两个=>{
if(两个hasOwnProperty('position')&&
二、hasOwnProperty(“技术”)和
两个['position']==一个['position']&&
两个['tech']==一个['tech']
) {
最终推送(“id”在一个?一:二);
}
})
})
返回最终结果;
}
设l1=obj1.1长度;
设l2=obj2.length
如果(l1 //console.log(doer(obj2,obj1))
如果使用lodash,则可以使用intersectionWith
方法,因为直观地说,您希望基于两个键进行兴趣切分
const object1=[
{
身份证号码:“30772”,
posimage:“/b/l/blue-shirt_1_1.jpg”,
体位:“胸部体位”,
技术:“刺绣”
},
{
身份证号码:“30772”,
posimage:“/b/l/blue-shirt_1_1.jpg”,
体位:“胸部体位”,
技术:“丝印纺织品”
},
{
身份证号码:“30772”,
posimage:“/b/l/blue-shirt_1_1.jpg”,
位置:“左摆臂”,
技术:“刺绣”
},
{
身份证号码:“30772”,
posimage:“/b/l/blue-shirt_1_1.jpg”,
位置:“左摆臂”,
技术:“丝印纺织品”
}
];
常量对象2=[
{
位置:“左摆臂”,
技术:“刺绣”
},
{
体位:“胸部体位”,
技术:“丝印纺织品”
}
];
const result=u.intersectionWith(
反对意见1,
反对意见2,
(o1,o2)=>o1.position==o2.position&&o1.tech==o2.tech
);
控制台日志(结果)代码>
第一个对象和最后一个对象完全相同,只是键位置名称更改为位置。这是你想要的吗?如果position和positionName在两个对象中都匹配,则将positionName替换为position?抱歉,我编辑了我的问题..这只是一个粗心的错误。因此,现在,最终输出必须只包含对象1和对象2中的位置匹配,对吗?是的,对象1中对象2的匹配对象应该返回