1

I have:

    var data = [
    {"a":some_value, "b":some_value},
    {"a":some_value, "b":some_value, "c":some_value},
    {"a":some_value, "b":some_value},

then:

    var keywords = [
    [{key: "a",value: 1}],
    [{key: "b",value: 1},{key: "b",value: 2}]
    ];

so i need to find data has ((a=1) AND (b=1 or b=2)):

    filteredData = $.grep(data, function (value, i) {
        if (value[keywords[0][0].key] !== keywords[0][0].value) return false;
        if (value[keywords[1][0].key] !== keywords[1][0].value) && (value[keywords[1][1].key] !== keywords[1][1].value)return false;
        return true;
    });

everything is ok, but now i need work by dynamic data, keywords, i can't do this!

0

3 Answers 3

3

Try

var array  = $.grep(data, function(obj, idx){
    return keywords.every(function(list, idx, src){
        return list.some(function(item, idx, src){
            return obj[item.key] == item.value
        });
    });
});
console.log(array)

Demo: Fiddle

For IE < 9 support, you may have to include the pollyfills for

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

5 Comments

I like this better than my answer. :)
@Arun P Johny let me check.
@Arun P Johny how should i (download and) include pollyfillls?
@mohammadadibi github.com/es-shims/es5-shim, this is the polyfill I personally use.
@OneOfOne if i include es5-shim, i can use every and some feature in ie<9 ?
0
filteredData = $.grep(data, function(value, i) {
    for (var k = 0; k < keywords.length; k++) {
        var matched = false;
        var orlist = keywords[k];
        for (var l = 0; l < orlist.length; l++) {
            if (value[orlist[l].key] == orlist[l].value) {
                matched = true;
                break;
        }
        if (!matched) {
            return false;
        }
    }
    return true;
});

Comments

0

jQuery is great but it's not the answer to everything, for this specific case, it can be simply done with just good ol' plain Javascript :

var data = [
    {"a":1, "b":100},
    {"a":1, "b":2, "c":3},
    {"a":2, "b":10}
];
var keywords = [
    [{key: "a",value: 1}],
    [{key: "b",value: 1},{key: "b",value: 2}]
];
function filter(data, keywords) {
    var ret = [];
    for(var i = 0, d, m; i < data.length; ++i) {
        d = data[i];
        m = true;
        for(var x = 0; x < keywords.length && m; ++x) {
            for(var k = 0; k < keywords[x].length && m; ++k) {
                var rule = keywords[x][k];
                console.log(d, d[rule.key] === rule.value, rule);
                m = m && rule.key in d && d[rule.key] === rule.value;
            }
            if(m) break;
        }
        if(m) ret.push(d);
    }
    return ret;
}
console.log(filter(data, keywords));

This is the es5 version, without jQuery (can be used with the es5-shim for older browsers) :

function es5_grep(data, keywords) {
    return data.map(function(row) {
        return keywords.every(function(list){
            return list.some(function(item){
                return row[item.key] === item.value
            });
        }) ? null : row;
    }).filter(function(v) { return !!v; });
}

This is just for future reference, https://stackoverflow.com/a/22512882/145587 is the proper answer for using jQuery.

6 Comments

what do you think of Arun P Johny solution? that's very short.
His answer is the proper one for jquery, however it depends on javascript features that will need extra files to run on older / broken browsers.
i test your code in my real program, it returns wrong answer.
I tested it against the dataset you provided, maybe give more code to test against?
i'll check it again,but before that tell me which of them is better include es5-shim and use it, or first code?
|

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.