Explanation of the problem statement:我有两个数组A和B:
A = [
{
"isAvailable" : true,
"productCode" : "12103977",
"size" : "UK/IND-3",
"url" : "/asics-mens-gel-kayano-28-harmony-blue-running-shoes/12103977"
},
{
"isAvailable" : true,
"productCode" : “12103978",
"size" : "UK/IND-4",
"url" : "/asics-mens-gel-kayano-28-harmony-blue-running-shoes/12103978"
},
{
"isAvailable" : true,
"productCode" : "12103979",
"size" : "UK/IND-8",
"url" : "/asics-mens-gel-kayano-28-harmony-blue-running-shoes/12103979"
},
]
B = [
{
"dimensionSize" : "3",
"euroSize" : "36",
"usSize" : "4"
},
{
"dimensionSize" : "4",
"euroSize" : "36",
"usSize" : "4"
},
{
"dimensionSize" : "10",
"euroSize" : "36",
"usSize" : "4"
},
]
我需要在以下条件下创建new array C基准:
Condition : "If the value contained in "101" key of any object in 102 includes the value contained in key "103" of any object in 104 then add the corresponding matching object from 105".
在满足上述条件后,数组C将是这样的-
C = [
{
"dimensionSize" : "3",
"euroSize" : "36",
"usSize" : "4"
},
{
"dimensionSize" : "4",
"euroSize" : "36",
"usSize" : "4"
},
]
Explanation of how array C is obtained :
My motive to find the solution to this problem:我有非常长的数组,我需要运行类似于上面的条件来过滤出新数组中的一些数据.我在堆栈溢出中看到了其他解决方案,并且我使用了数组方法(如filter、includes、some、map等)导出了一个解决方案,但我需要一个对实时数据有效的有效解决方案,如长度巨大的array.
非常感谢在共享有效算法或路径转发方面提供的任何帮助.提前感谢您!
Edit:我现有的以O(n^2)运行的解决方案.假设A和B是问题陈述中的array.matchArray是结果array.
let matchArray;
matchArray = B.filter(objB => {
let match = false;
A.forEach(objA => {
if (objA.size.includes(objB.dimensionSize) {
match = true;
}
});
return match;
});
Edit 2:数组B和数组C的 struct 不正确.我现在已经改正了.