我有一个看起来像这样的 json 对象,并且我的 json 对象中有“skus”值的随机参数
喜欢
var mytestvalue="Fit:Regular,Color:Coyote (120)"; or
var mytestvalue="Color:Coyote (120),Fit:Regular";
[
{
"index":0,
"title":"Foo1",
"skus":"Fit:Regular,Size:6,Color:Coyote (120)",
},
{
"index":1,
"title":"Foo2",
"skus":"Fit:Regular,Color:Coyote (120),Size:65",
},
{
"index":2,
"title":"Foo3",
"skus":"Fit:Regular,Size:7,Color:Coyote (120)",
},
{
"index":0,
"title":"Foo4",
"skus":"Color:Coyote (120),Fit:Regular,Size:7.5",
},
{
"index":1,
"title":"Foo5",
"skus":"Fit:Slim,Size:8,Color:Coyote (120)",
},
{
"index":2,
"title":"Foo6",
"skus":"Fit:Regular,Size:9,Color:Coyote (120)",
},
{
"index":0,
"title":"Foo7",
"skus":"Fit:Regular,Size:8.5,Color:Coyote (120)",
},
{
"index":1,
"title":"Foo8",
"skus":"Fit:Regular,Size:10,Color:Coyote (120)",
},
{
"index":2,
"title":"Foo9",
"skus":"Fit:Slim,Color:Coyote (120),Size:13",
},
{
"index":0,
"title":"Foo10",
"skus":"Fit:Regular,Size:8.5,Color:Coyote (120)",
},
{
"index":1,
"title":"Foo11",
"skus":"Fit:Regular,Size:10,Color:Coyote (120)",
},
{
"index":2,
"title":"Foo12",
"skus":"Fit:Regular,Color:Coyote (120),Size:13",
}
]
我编写了一个函数来获取 skus 与我拥有的变量的匹配组合
function getSkuCombinations(obj,prodskuval) {
var combres = $.grep(obj, function (o) { //Will give you all the matches
return o.SKUOptions.indexOf(prodskuval) > -1;
});
return combres;
}
我这样称呼这个函数
var combinationresults=getSkuCombinations(myobj,mytestvalue)
当我这样做时,我应该得到所有具有我通过的组合“mytestvalue”的sku,但这个函数现在只返回我
var myresult= [{
"index":1,
"title":"Foo2",
"skus":"Fit:Regular,Color:Coyote (120),Size:65",
}]
谁能帮我做这件事
这个功能解决了我的问题,
function getSkuCombinations(obj,prodskuval) {
var combres = $.grep(obj, function (o) { //Will give you all the matches
var newskus=o.skus.split(',').sort().join(',');
var newprdskus=prodskuval.split(',').sort().join(',');
return newskus.indexOf(newprdskus) > -1;
});
}