循环并比较javascript对象

时间:2022-07-13 22:56:35

I have two arrays which are created from the inputs of a user like so:

我有两个数组,它们是从用户的输入创建的,如下所示:

var impArray = [];
$('[id^=imp]').on('change', function(){
    var value = $(this).val();
    var name = ($(this).attr('name').replace('imp-',''))
    impArray[name] = value;
    console.log(impArray);
})

var assessArray= [];
$('[id^=assess]').on('change', function(){
    var value = $(this).val();
    var name = ($(this).attr('name').replace('assess-',''))
    assessArray[name] = value;
    console.log(assessArray);
})

These create arrays like

这些创建数组

assessAray
    1-1: 10
    1-2: 15
    1-3: 9

impArray
    1-1: 6
    1-2: 14
    1-3: 2

I then need to do a simple calculation with the matching keys like:

然后,我需要使用匹配的键进行简单的计算,例如:

$('#comp-1-1').val(impArray['1-1'] / assessArray['1-1'] * 100);

Obviously I can't do this with every single one, so,

显然我不能每一个都做到这一点,所以,

Question: How can I loop through the arrays and compare them based on keys then do something with their values?

问题:如何循环遍历数组并根据键进行比较,然后对其值进行操作?

4 个解决方案

#1


1  

Technically, you are working with JavaScript objects, not arrays. Your variable declarations should actually be:

从技术上讲,您使用的是JavaScript对象,而不是数组。您的变量声明实际上应该是:

var impArray = {};
var assessArray = {};

Once you have the correct variable declarations, you can use jQuery.each to iterate through keys (not indexes):

一旦有了正确的变量声明,就可以使用jQuery.each来遍历键(而不是索引):

$.each(impArray, function(key, value){
  $('#comp-'+key).val(assessArray[key]/value*100);
});

#2


1  

If both arrays will always be the same length and have the object property at the same index, this should work:

如果两个数组的长度始终相同并且对象属性在同一索引处,则应该可以:

http://jsfiddle.net/9DBuD/

assessArray = [{'1-1':'10'},{'1-2':'15'},{'1-3':'9'}];
impArray = [{'1-1':'6'},{'1-2':'14'},{'1-3':'2'}];

for(var i=0;i<assessArray.length;i++){
    for(var prop in assessArray[i]){
        for(var property in impArray[i]){
            if(prop == property){
                $('#comp-'+prop).val(impArray[i][property]/assessArray[i][prop]*100)
            }
        }
    }
}

Edit

This modified fiddle and code should produce the same results even if the array indexes and sizes do not match:

即使数组索引和大小不匹配,这个修改过的小提琴和代码应该产生相同的结果:

http://jsfiddle.net/9DBuD/1/

Array.prototype.indexOfProp = function (property) {
    for (var i = 0, len = this.length; i < len; i++) {
        if (this[i][property]!=undefined) return i;
    }
    return -1;
}

assessArray = [{'1-2':'15'},{'1-3':'9'},{'1-1':'10'},{'1-4':'10'}];
impArray = [{'1-1':'6'},{'1-3':'2'},{'1-2':'14'}];


for(var i=0;i<assessArray.length;i++){
    for(var prop in assessArray[i]){
        var index = impArray.indexOfProp(prop)
        if(index!=-1){
             $('#comp-'+prop).val(impArray[index][prop]/assessArray[i][prop]*100)   
        }
    }
}

#3


0  

Try using $.each(), like:

尝试使用$ .each(),如:

$.each(impArray, function(i, v){
  $('#comp-'+i).val(v/assessArray[i]*100);
});

#4


0  

Does this help you?

这对你有帮助吗?

$.each(impArray, function(index, value){
  var result = assessArray[index] / value * 100;
  $('#comp-1-'+index).val(result);
});

#1


1  

Technically, you are working with JavaScript objects, not arrays. Your variable declarations should actually be:

从技术上讲,您使用的是JavaScript对象,而不是数组。您的变量声明实际上应该是:

var impArray = {};
var assessArray = {};

Once you have the correct variable declarations, you can use jQuery.each to iterate through keys (not indexes):

一旦有了正确的变量声明,就可以使用jQuery.each来遍历键(而不是索引):

$.each(impArray, function(key, value){
  $('#comp-'+key).val(assessArray[key]/value*100);
});

#2


1  

If both arrays will always be the same length and have the object property at the same index, this should work:

如果两个数组的长度始终相同并且对象属性在同一索引处,则应该可以:

http://jsfiddle.net/9DBuD/

assessArray = [{'1-1':'10'},{'1-2':'15'},{'1-3':'9'}];
impArray = [{'1-1':'6'},{'1-2':'14'},{'1-3':'2'}];

for(var i=0;i<assessArray.length;i++){
    for(var prop in assessArray[i]){
        for(var property in impArray[i]){
            if(prop == property){
                $('#comp-'+prop).val(impArray[i][property]/assessArray[i][prop]*100)
            }
        }
    }
}

Edit

This modified fiddle and code should produce the same results even if the array indexes and sizes do not match:

即使数组索引和大小不匹配,这个修改过的小提琴和代码应该产生相同的结果:

http://jsfiddle.net/9DBuD/1/

Array.prototype.indexOfProp = function (property) {
    for (var i = 0, len = this.length; i < len; i++) {
        if (this[i][property]!=undefined) return i;
    }
    return -1;
}

assessArray = [{'1-2':'15'},{'1-3':'9'},{'1-1':'10'},{'1-4':'10'}];
impArray = [{'1-1':'6'},{'1-3':'2'},{'1-2':'14'}];


for(var i=0;i<assessArray.length;i++){
    for(var prop in assessArray[i]){
        var index = impArray.indexOfProp(prop)
        if(index!=-1){
             $('#comp-'+prop).val(impArray[index][prop]/assessArray[i][prop]*100)   
        }
    }
}

#3


0  

Try using $.each(), like:

尝试使用$ .each(),如:

$.each(impArray, function(i, v){
  $('#comp-'+i).val(v/assessArray[i]*100);
});

#4


0  

Does this help you?

这对你有帮助吗?

$.each(impArray, function(index, value){
  var result = assessArray[index] / value * 100;
  $('#comp-1-'+index).val(result);
});