使用javascript将对象值与对象中数组的长度进行比较

时间:2015-04-07 15:37:59

标签: javascript

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

编辑:值名称不相同

对象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的值

使用underScore解决方案

   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;

    }

4 个答案:

答案 0 :(得分:0)

如果您的模型不是最终模型,您可以使用像:

这样的对象数组
var object = [
   {table : [one, two, three], length : 3},
   {table : [one, two, three], length : 4},
... ];

并使用以下方法比较值:

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

它与您的模型有点不同

但我希望它可能会有所帮助。

答案 1 :(得分:0)

执行此操作的最佳方法是,如果两个对象具有相同的名称(对于给定对),则使用The For/In Loop迭代其中一个并返回两者的值,然后进行比较。

使用Object.keys重新制作小提琴,为两个对象创建一个键数组,现在即使键不相同(跟在对象索引之后)也能正常工作



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
    }
}
&#13;
&#13;
&#13;

控制台输出:

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

答案 2 :(得分:0)

相当啰嗦,但这是我能想到的唯一方法:

// 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));

DEMO

答案 3 :(得分:-1)

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

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");
    }
}