【问题标题】:jQuery compare two arrays objects to have one final array objectsjQuery 比较两个数组对象有一个最终的数组对象
【发布时间】:2017-10-26 01:29:11
【问题描述】:

任何人都可以在以下情况下提供帮助吗? 我有两个数组对象要根据模型和序列进行比较,并且只需要得出一个结果。 请参考以下示例。谢谢。

ArrayObject1 = [{model:'M1', serial:'S1', file:'F1', other:null},
                {model:'M2', serial:'S2', file:'F2', other:null}];

ArrayObject2 = [{model:'M1', serial:'S1', file:null, other:'F3'},
                {model:'M3', serial:'S3', file:null, other:'F4'}];

ExpectedResult = [{model:'M1', serial:'S1', file:'F1', other:'F3'},
                 {model:'M2', serial:'S2', file:'F2', other:null},
                 {model:'M3', serial:'S3', file:null, other:'F4'}];

【问题讨论】:

  • 如果两个输入数组都有一个具有相同modelserial 但不同的非空fileother 的对象会发生什么? (顺便说一句,edit 这个问题显示有效的对象文字语法并没有什么坏处,即使用: 而不是=。)
  • 输入数组总是喜欢样本。每个输入数组将只有一个模型和序列,因为模型和序列将使该对象唯一。谢谢。
  • 你试过lodash吗?我建议使用它,因为它具有称为 unionunionBy 的功能,您可以将它们用于您的目的。这是文档:lodash.com/docs/4.17.4#union

标签: javascript jquery arrayobject


【解决方案1】:

我不认为 jquery 提供了一种简单的方法来解决您的问题。这是我的解决方案:

var arr1 = [
	{ model: "M1", serial: "S1", file: "F1", other: null },
	{ model: "M2", serial: "S2", file: "F2", other: null }
];

var arr2 = [
	{ model: "M1", serial: "S1", file: null, other: "F3" },
	{ model: "M3", serial: "S3", file: null, other: "F4" }
];

var arr3 = arr1.concat(arr2);
var result = [];

arr3.forEach(function(item1, index){
	var arr4 = result.filter(function(item2, index){
		return item1.model === item2.model;
	});
	if (arr4.length === 1) {
		for(var prop in item1){
			if (item1.hasOwnProperty(prop)) {
				arr4[0][prop] = (item1[prop] || arr4[0][prop]);
			}
		}
	}else{
		result.push(item1);
	}
});

console.log(result);

这仅适用于最多有 2 个具有相同模型名称的模型要合并的情况,因为如果有三个“M1”并且其中两个有非空“文件”,那么我不知道要合并哪个选择..

【讨论】:

    【解决方案2】:
    var ExpectedResult = [];
    //loop through either of the object array. Since they are of same length, it wouldn't matter which one you choose to loop though.
     for(var i = 0; i < ArrayObject2.length; i++) {
       //check to see if the array element(which are objects) have the same model property
       if(ArrayObject2[i].model === ArrayObject1[i].model){
         //if they are the same, starting switching the values of the file and other properties in either one of the array 
         //I choose ArrayObject2, it won't matter which one you decide to use
    
         //this chooses which is truthy between file property of ArrayObject2 at index i and file property of ArrayObject1 at index i and assigns it to the file property of ArrayObject2 at index i
         ArrayObject2[i].file = ArrayObject2[i].file || ArrayObject1[i].file;
    
          //this chooses which is truthy between other property of ArrayObject2 at index i and other property of ArrayObject1 at index i and assigns it to the other property of ArrayObject2 at index i
         ArrayObject2[i].other = ArrayObject2[i].other || ArrayObject1[i].other;
    
         //push the ArrayObject2 at position i into the ExpectedResult array
         ExpectedResult.push(ArrayObject2[i]);
       }
       //other wise; if the model property differs, just push the ArrayObject1 and ArrayObject2 at index i
       else {
         ExpectedResult.push(ArrayObject1[i]);
         ExpectedResult.push(ArrayObject2[i]);
       }
     }
    
     ExpectedResult;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      • 2013-05-17
      • 2020-01-17
      • 1970-01-01
      相关资源
      最近更新 更多