0

我有这个代码:

$('#find').click(function(){
    if(first.val() != '' && second.val() != ''){
        $.getJSON(urlPersonSearch + first.val() + "?callback=?", function(json) {
            firstId = (json[0].id != undefined) ? json[0].id : '';
            if(firstId != '') result = grabMovies(firstId);
            var i = result[0].filmography.length;
            while(i--){
                movies[result[0].filmography[i].id] = result[0].filmography[i].name;
            }
        });
        $.getJSON(urlPersonSearch + second.val() + "?callback=?", function(json) {
            secondId = (json[0].id != undefined) ? json[0].id : '';
            if(secondId != '') result = grabMovies(secondId);
            var i = result[0].filmography.length;
            while(i--){
                movies[result[0].filmography[i].id] = result[0].filmography[i].name;
            }
        });

    }

});


function grabMovies(id){
    $.getJSON(urlPersonResult + id + "?callback=?", function(json) {
        return json;
    });
}

现在,我想要做的是最终得到一个以 ID 和电影名称作为键/值的对象。最终,我试图获得一个仅包含两个 json 结果之间匹配项的对象。IE。

如果第一个结果是

23 = 你好,283 = 再见,

第二个的结果是

23 = 你好,294 = 再见

我最终会得到一个对象

23 = 你好

有没有人对如何做到这一点有任何建议?

4

2 回答 2

0

让我理解:您正在寻找两组之间的交集。我不完全确定您的代码中发生了什么-您有这些循环:

movies[result[0].filmography[i].id] = result[0].filmography[i].name;

我认为这些是您寻找交叉点的尝试。如果是这种情况,我要做的是设置两个对象,而不是一个“电影”对象,:

$('#find').click(function(){
    var initalMatches = {}; //so this is available to the callbacks below
    if(first.val() != '' && second.val() != ''){
     $.getJSON(urlPersonSearch + first.val() + "?callback=?", function(json) {
        var filmogradphy, i;
        firstId = (json[0].id != undefined) ? json[0].id : '';
        if(firstId != '') result = grabMovies(firstId);
        filmogradphy = result[0].filmogradphy;
        i = filmography.length;
        while(i--){
            initalMatches[filmogradphy[i].id] = filmogradphy[i].name;
        }
    });
    $.getJSON(urlPersonSearch + second.val() + "?callback=?", function(json) {
        var filmogradphy, i, id;
        secondId = (json[0].id != undefined) ? json[0].id : '';
        if(secondId != '') result = grabMovies(secondId);
        filmogradphy = result[0].filmogradphy;
        i = filmography.length;
        while(i--){
            id = filmogradphy[i].id;
            if( "undefined" !== initalMatches[id] ){
                movies[id] = filmography[i].name;
            }
        }
    });

}

});

您可以在这里进行一些重构,但这应该可行:-)

于 2012-04-14T19:28:36.520 回答
0

您正在寻找一个数组交集函数。Underscore.js有一个。所以我已经准备好告诉你使用它,但它不会用于此目的。看看我尝试了什么:

var a = [{23:'hello'},{283:'goodbye'}],
    b = [{23:'hello'},{294:'bye'}];

_.intersection(a,b); // produces []

// investigating further
a.indexOf({23:'hello'}); // -1

这里的根本问题是 JavaScript 中的对象等效性。另请注意

({23:'hello'} === {23:'hello'}); // false
({} === {}) // false

两个 JavaScript 对象并不等价,即使它们具有相同的键和值。这与 Javascript 中的对象文字有关,它在底层调用构造函数,从而在内存中保留一个新的位置。

解决方案

Underscore 的 _.indexOf 方法似乎有一个错误。除此之外,它是一个很棒的库,我建议你使用它。我将使用以下内容对其进行扩展:

_.mixin({
    deepIndexOf: function (array, item, isSorted) {
        if (array == null) return -1;
        var i, l;
        if (isSorted) {
          i = _.sortedIndex(array, item);
          return _.isEqual(array[i],item) ? i : -1;
        }
        for (i = 0, l = array.length; i < l; i++) if (i in array && _.isEqual(array[i],item)) return i;
        return -1;
    },
    deepIntersection: function (array) {
        var rest = Array.prototype.slice.call(arguments, 1);
        return _.filter(_.uniq(array), function(item) {
          return _.every(rest, function(other) {
            return _.deepIndexOf(other, item) >= 0;
          });
        });
      }
});

现在,您可以_.deepIntersection(a,b)像这样运行:

var a = [{23:'hello'},{283:'goodbye'}],
    b = [{23:'hello'},{294:'bye'}];

_.deepIntersection(a,b); // produces [{23:'hello'}]
于 2012-04-14T19:47:41.350 回答