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",
    }
      ]

これはあなたの新しい機能です:

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;
}

関数 getSkuCombinations が両方のケースを考慮する必要があります:

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

これを試して :

function getSkuCombinations(obj,prodskuval) { var Combres = $.each(obj, function (key, value) { //すべての一致を返します return val.skus.indexOf(prodskuval) > -1; }); }

于 2013-10-24T15:55:36.663 に答える
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

Node.jsでテストしました。SKUOptions(何だか分からない) を( JSONskus内のオブジェクトの属性) に変更すると、次のように動作します。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 に答える