Javascript JS:比较没有隐藏字段的字典

Javascript JS:比较没有隐藏字段的字典,javascript,dictionary,Javascript,Dictionary,我有一组来自服务器的字典。然后我从数据库中得到一个单字典,我需要从数组中的数据库中找到一个单字典 array.indexOfdict-它不起作用,我认为这是因为字典有不同的隐藏字段 获取字符串并将其与字符串进行比较-不起作用 如果使用过滤器: array.filter(function(dictFromArr){ return dictFromArr.id == dict.id && ... }); 不适合,因为字段太多 有人知道如何比较字典吗?要搜索数组,必须使用On操作

我有一组来自服务器的字典。然后我从数据库中得到一个单字典,我需要从数组中的数据库中找到一个单字典

array.indexOfdict-它不起作用,我认为这是因为字典有不同的隐藏字段

获取字符串并将其与字符串进行比较-不起作用

如果使用过滤器:

array.filter(function(dictFromArr){
  return dictFromArr.id == dict.id && ...
});
不适合,因为字段太多


有人知道如何比较字典吗?

要搜索数组,必须使用On操作遍历它,所以filter是一个可行的选项。在找到对象时返回的算法有时会提高性能,但“未找到的最差投射方案”项仍处于启用状态:

function getObjectFromArray(array, object) {
    for(var i = 0; i < array.length; i++) { // traverse the array
        if(array[i].id === object.id) { // compare ids 
            return array[i]; // if id exist return element
        }
    }

    return null; // if not found return null
}

我想您可以尝试将每个数组字典的键和值数组与您的模型字典进行比较。效率不高,但能完成工作:

// return the object keys
function getKeys(obj) {
    return Object.keys(obj);
}

// using the keys, return the object values
function getVals(keys, obj) {
    return keys.map(function (el) {
        return obj[el];
    });
}

function isMatch(arr1, arr2) {

  // match the array lengths and that the values at each
  // index in arr1 match those in arr2
  return arr1.length === arr2.length && arr1.every(function (el, i) {
    return arr1[i] === arr2[i];
  });
}

function find(arr, obj) {

    // grab the object keys and values
    var oKeys = getKeys(obj);
    var oVals = getVals(oKeys, obj);

    // filter out the object where both the keys and
    // the values match
    return arr.filter(function (c) {
      var cKeys = getKeys(c);
      var cVals = getVals(cKeys, c);
      return isMatch(oKeys, cKeys) && isMatch(oVals, cVals);
    })[0] || 'no match';
}

find(arr, obj);
这是因为===so偶数[{a:1}].indexOf{a:1}返回-1

比较字符串是个坏主意,因为toString默认返回[Object]。甚至比较JSON字符串也是一个坏主意,因为JSON.stringify{b:1,a:1}!==stringify{a:1,b:1}虽然对象明显相等

如果你愿意在你的项目中加入一个新的库,你可以使用。请注意,如果不想,您不必拉取整个lodash—您可以使用,并且lodash中的每个其他函数都有一个单独的包

或者,您可以编写一个递归函数来比较它们。函数将遍历对象中的所有键,如果值都是对象,则应该递归。但是,imho,这种方式只有作为一种编程练习才有意义,一种好的编程练习


有关比较JS中对象的更多详细信息,请参见注释代码有多大!这就是为什么您可能不想自己实现它。

您可以添加一个示例,说明这组字典的外观吗?或者更重要的是,难道你不能在你的过滤函数中只比较一个唯一的id而不是所有的id吗?@Andy举个例子,说明这个字典数组非常大,结构复杂,需要占用太多空间here@Maria,看起来你不明白安迪的问题。如果这些ID是唯一的数据库ID,那么您可以查看这些ID来判断它是否是同一个对象。如果ID是唯一的,那么大而复杂的结构就无关紧要了。我将使用这段代码,我需要比较所有字段-这不是meit模拟的filterIt的方式。它只返回一个对象,它的最佳情况和平均情况比filter好得多。@OriDori平均情况仍然启用。@m01平均情况为+1/2。虽然我们通常会删除+1和/2,但它在长列表中确实很重要。
// return the object keys
function getKeys(obj) {
    return Object.keys(obj);
}

// using the keys, return the object values
function getVals(keys, obj) {
    return keys.map(function (el) {
        return obj[el];
    });
}

function isMatch(arr1, arr2) {

  // match the array lengths and that the values at each
  // index in arr1 match those in arr2
  return arr1.length === arr2.length && arr1.every(function (el, i) {
    return arr1[i] === arr2[i];
  });
}

function find(arr, obj) {

    // grab the object keys and values
    var oKeys = getKeys(obj);
    var oVals = getVals(oKeys, obj);

    // filter out the object where both the keys and
    // the values match
    return arr.filter(function (c) {
      var cKeys = getKeys(c);
      var cVals = getVals(cKeys, c);
      return isMatch(oKeys, cKeys) && isMatch(oVals, cVals);
    })[0] || 'no match';
}

find(arr, obj);