0

我有一个看起来像这样的 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;
         });
    }
4

5 回答 5

1
var mytestvalue="Fit:Regular,Color:Coyote (120)"; or
      var mytestvalue="Color:Coyote (120),Fit:Regular";     

var myobj = [
           {
                "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",
    }
      ]

This is you new function:

function getSkuCombinations(obj, prodskuval) {
         var combres = $.grep(obj, function (o) { //Will give you all the matches
                        return o.skus.split(',').sort().toString() == prodskuval.split(',').sort().toString();
                 });
         return combres;
}

Is necessary that function getSkuCombinations consider both case :

var mytestvalue="Fit:Regular,Color:Coyote (120)"; or
var mytestvalue="Color:Coyote (120),Fit:Regular";
于 2013-10-24T16:09:08.333 回答
1
var checkSKU = function (obj, testValue) {
    var results = [];
    obj.forEach(function (item) {
        if (item.skus.indexOf(testValue) > -1) {
            results.push(item);
        }
    });
    return results;
};

JSFiddle 测试用例:http: //jsfiddle.net/N9V9q/3/

于 2013-10-24T18:15:23.187 回答
0

使用 Node.js 对其进行了测试。如果您将SKUOptions(dunno waht that is) 更改为skus(JSON 中对象的属性myobj),那么它可以工作:

var $ = require('jquery');

var mytestvalue = "Fit:Regular,Color:Coyote (120)";
// var mytestvalue = "Color:Coyote (120),Fit:Regular";

var myobj = [{
  "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",
}];

function getSkuCombinations(obj, prodskuval) {
  var combres = $.grep(obj, function(o) { //Will give you all the matches
    return o.skus.indexOf(prodskuval) > -1;
  });
  return combres;
}

var combinationresults = getSkuCombinations(myobj, mytestvalue)
console.log(combinationresults);

回报:

[ { index: 1,
    title: 'Foo2',
    skus: 'Fit:Regular,Color:Coyote (120),Size:65' },
  { index: 2,
    title: 'Foo12',
    skus: 'Fit:Regular,Color:Coyote (120),Size:13' } ]

jsFiddle (查找 console.log):http: //jsfiddle.net/Saran/Y8wwE/

于 2013-10-24T16:00:57.423 回答
0

这是解决我问题的方法

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;
     });
}
于 2013-10-24T18:50:45.180 回答
0

尝试这个 :

function getSkuCombinations(obj,prodskuval) { var combres = $.each(obj, function (key, value) { //会给你所有的匹配 return val.skus.indexOf(prodskuval) > -1; }); }

于 2013-10-24T15:55:36.663 回答