0

If the "id":"236" exists in spConfig['attributs'][125]['options'] get the array that is contained in.

How would you do this in jQuery ?

var spConfig = {
    "attributes": {
        "125": {
            "id": "125",
            "code": "pos_colours",
            "label": "Colour",
            "options": [{
                "id": "236",
                "label": "Dazzling Blue",
                "price": "0",
                "oldPrice": "0",
                "products": ["11148"]
            }, {
                "id": "305",
                "label": "Vintage Brown",
                "price": "0",
                "oldPrice": "0",
                "products": ["11786", "11787", "11788", "11789", "11790", "11791", "11792", "11793"]
            }]
        }

    }
1
  • Which array do you want to get, the options array? Or do you mean the object whose property "id" is 236? Commented Aug 3, 2012 at 4:19

3 Answers 3

1

Your data structure is slightly complicated, but assuming options.ids are unique, you can use

function foo(arg) {
    var filtered;
    $.each(spConfig.attributes, function() {
        filtered = $(this.options).filter(function() {
            return this.id == arg;
        });
    });
    return (filtered.length) ? filtered[0].products : [];
}

Fiddle

The function to return an empty array when a nonexistent key is passed.

Also, if you have more than one attribute property (other than 125) and want to iterate over them:

function foo(arg) {
    var filtered=[];
    $.each(spConfig.attributes, function() {
        filtered.push($(this.options).filter(function() {
            return this.id == arg;
        }));
    });
    filtered = $(filtered).filter(function() {
        return this.length;
    });
    return (filtered.length) ? filtered[0][0].products : [];
}

Fiddle

Or, if you'll always access the property attribute[125], you may as well leave it hard-coded for simplicity:

function foo(arg) {
    var filtered = $(spConfig.attributes[125].options).filter(function() {
        return this.id == arg;
    });
    return (filtered.length) ? filtered[0].products : [];
}

Fiddle

Or passing the attribute property name if you need more customization.

function foo(arg, attr) {
    var filtered = $(spConfig.attributes[attr].options).filter(function() {
        return this.id == arg;
    });
    return (filtered.length) ? filtered[0].products : [];
}

Fiddle

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

Comments

1

http://api.jquery.com/jQuery.inArray/

if ($.inArray('yourmom', myArray) !== -1) ...

2 Comments

Hi thanks for the reply however the code that you have entered only returns whether or not yourmom is found in myArray it doesn't then tell me what key that contains the array yourmom is in... any ideas
This function is of no help for that task.
1

DEMO

function findMe(searchTerm, location) {
    for ( var i = 0; i < location.length; i++ ) {
        if(location[i]['id'] == searchTerm) {
            return location[i];        
        }
    }
    return null;
}

var searchTerm = '236';
var location = spConfig['attributes']['125']['options'];

var requiredObject = findMe( searchTerm, location );
​alert( requiredObject ? requiredObject['label'] : 'Not Found');​​​​​​

4 Comments

location is an array, not an object. Use the appropriate loop.
@bergi An array is an object, and it's contents are enumerable. Sure you can pick up prototype extensions, but that doesn't matter in this case since ['id'] == searchTerm fails if it's null, and the prototype shouldn't be that cluttered anyway. Isn't this faster than a plain for loop anyway?
No, it is not faster. And yes, the prototype can have enumerable properties, e.g. when polyfilling es5 methods or indexOf for IE.
Yes you can enumerate the the prototype properties, but adding the ['id'] reference would return undefined anyway, which would fail the equality check and move on. And by cluttered I mean there wouldn't be enough extra properties to affect performance. If it's no faster though I suppose it makes no difference (updated it to regular).

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.