0

I have a json object which looks like this and i have and random parameters for "skus" value in my json object

like

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

i wrote a function to get the matched combinations of skus with variable i have

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

i call this function like this

     var combinationresults=getSkuCombinations(myobj,mytestvalue)

when i do this i should get all the skus which have the combination "mytestvalue" which i passed but this function now only returns me

       var myresult= [{
                "index":1,
                "title":"Foo2",
                "skus":"Fit:Regular,Color:Coyote (120),Size:65",
                  }]

can any one help me in doing this

this function solved my problem,

     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
  • Your function getSkuCombinations not return nothing. Must be return combres? Commented Oct 24, 2013 at 15:26
  • Yes i forgot to addit here yes it returns the combres Commented Oct 24, 2013 at 15:39
  • @Jhonathan i changed that,can you check this now Commented Oct 24, 2013 at 15:40
  • @Jhonathan thank you for showing a way to solve my issue, was able to solve this with solution provided by the jonathan Commented Oct 24, 2013 at 18:48

5 Answers 5

1
var checkSKU = function (obj, testValue) {
    var results = [];
    obj.forEach(function (item) {
        if (item.skus.indexOf(testValue) > -1) {
            results.push(item);
        }
    });
    return results;
};

JSFiddle test case: http://jsfiddle.net/N9V9q/3/

Sign up to request clarification or add additional context in comments.

4 Comments

this is returning all the objects , i mean the same object again
Either be clearer with your question or start upvoting. This solves the problem you've presented.
there are objects with different fit, which shouldn't be as i specified
Updated, just for you.
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";

Comments

0

Try this :

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

1 Comment

This option not consider distinct order in characteristic of skus
0

Tested it with Node.js. If you change SKUOptions (dunno waht that is) to skus (attribute of the object in the myobj JSON), then it works:

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

Returns:

[ { 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 (look for console.log): http://jsfiddle.net/Saran/Y8wwE/

2 Comments

This option not consider distinct order in characteristic of skus in comparision
Sorry, I have no idea what SKUOptions is. Can you paste that part of your code?
0

This is the method which solved my problem

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

1 Comment

Well, if you articulated you wanted the matching to be made based on the quasi-attributes (name:value separated by , as part of a plain String) to be matched against the skus from array elements regardless of the order of those quasi-attributes, then maybe someone would have thought of that, too. Regardless of our wasted efforts, here's the advice: refactor the quasi-attribute String (mytestvalue) to an actual Object and then change the getSkuCombinations() to work with it as such. That way someone other than you will make some sense from the code later on :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.