简体   繁体   中英

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

EDIT: SOLVED See final solution below

EDIT: The value names are not the same

object 1

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

object 2 with arrays

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

I am trying to do is the following

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

but I do not want it to be so specific. Is there a way to loop over each object and return the values of obj2 that do not match obj1

SOLUTION with 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;

    }

If your model is not the definitive one, you could use an array of object like :

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

and compare values using :

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

It's a little bit different from your model

but i hope it may helps.

The best way to do this is if both objects have the same name (for a given pair), then using The For/In Loop iterate over one of them and return the value of both, and then compare.

Remade the fiddle using Object.keys to make an array of keys for both objects, now it works even when the keys aren't the same (following the object index)

 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 } } 

console output :

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

Quite long-winded, but the only way I could think to do it:

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

UPDATED: Maybe something like this. You would have to make sure the property names are the same:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM