使用javascript比较对象中的对象值和数组长度

compare object value with length of array in an object with javascript

本文关键字:对象 数组 javascript 比较 使用      更新时间:2023-09-26

编辑:已解决参见下面的最终解决方案

编辑:值名称不同于

目标1

var obj1 = {
    val1a : 4,
    val2a : 3 ,
    val3a : 7
}

具有阵列的对象2

var obj2 = {
    val1b : [one, two, three],
    val2b : [oneA, twoA, threeA],
    val3b : [oneB]
}

我正在尝试做的是以下

if(obj1.val1a  === obj2.val1b.length){
// code
}

但我不希望它如此具体。有没有办法在每个对象上循环并返回与obj1 不匹配的obj2值

得分偏低的解决方案

   function checkData(obj1, obj2) {
        result = []
        var keys_obj1 = Object.keys( obj1)
        var keys_obj2 = Object.keys( obj2)         
        _.each(keys_obj1, function(num, i){
            if(obj1[keys_obj1[i]].length  !== obj2[keys_obj2[i]]) {
               result.push(keys_obj1[i]);
            }
        })
        return result;
    }

实现这一点的最佳方法是,如果两个对象都有相同的名称(对于给定的对),则使用for/In Loop迭代其中一个对象并返回两者的值,然后进行比较

使用Object.keys重新制作fiddle,为两个对象制作一个键数组,现在即使键不相同(遵循对象索引)也能工作

var obj1 = {
    val1a : 4,
    val2a : 3 ,
    val3a : 7
}
var obj2 = {
    val1b : ['one', 'two', 'three'],
    val2b : ['oneA', 'twoA', 'threeA'],
    val3b : ['oneB']
}
var keys_obj1 = Object.keys( obj1 )
var keys_obj2 = Object.keys( obj2 )
for (i = 0; i < keys_obj1.length; i++) {
    console.log(keys_obj1[i]+'-'+obj1[keys_obj1[i]])
    console.log(keys_obj2[i]+'-'+obj2[keys_obj2[i]].length);
    if(obj1[keys_obj1[i]]  === obj2[keys_obj2[i]].length) {
        //match
    }
}

控制台输出:

"val1a-4"
"val1b-3"
"val2a-3"
"val2b-3"
"val3a-7"
"val3b-1"

如果你的模型不是决定性的,你可以使用一个对象数组,比如:

var object = [
   {table : [one, two, three], length : 3},
   {table : [one, two, three], length : 4},
... ];

并使用比较值

object[i].table.length === object[i].length

它和你的型号有点不同

但我希望它能有所帮助。

很冗长,但我能想到的唯一方法是:

// for each object you are going to...
function pullData(obj) {
    var out = {};
    var token;
    // grab the keys
    var keys = Object.keys(obj).map(function (el) {
      // token is the character at the end of the key
      // the key that is returned is the key without the token
      token = el.slice(-1)
      return el.slice(0, 4);
    });
    // for each key, add it to the output object
    for (var i = 0, l = keys.length; i < l; i++) {
     out[keys[i]] = obj[keys[i] + token]
    }
    // return an object containing the data and the token
    return {data: out, token: token};
}
// take both the output from both objects being parsed
function isNotMatch(obj1, obj2) {
    var out = [];
    // loop over the first object using the now identical keys
    for (var p in obj1.data) {
        // check the values and lengths
        // if they're not the same push the key and the token as a string
        // to the output array
        if (obj1.data[p] !== obj2.data[p].length) {
          out.push(p + obj2.token);
        }
    }
    // return the array of non-matches
    return out;
}
isNotMatch(pullData(obj1), pullData(obj2));

演示

更新:也许是这样的。您必须确保属性名称相同:

var obj1 = {
    val1a : 4,
    val2a : 3 ,
    val3a : 7
};
var obj2 = {
    val1a : ['one', 'two', 'three'],
    val2a : ['oneA', 'twoA', 'threeA'],
    val3a : ['oneB']
};
for(var name in obj1) {
    if(obj1[name]  === obj2[name].length) {
        alert("match");
    }
}