Javascript:从包含对象的多维数组返回不匹配的值

时间:2018-04-29 08:37:22

标签: javascript jquery arrays for-loop foreach

我有两个多维数组,每个数组都包含一些对象(键/值)我想要循环抛出对象值来找到不匹配的值,如果找到记录具有不匹配值的对象我试过但我没有得到结果任何帮助请在纯Javascript或Jquery或任何其他框架 下面是我的代码

    var firstarry=[{'name':'alex','age':22,'numbersOne':['111','222','333','444','555'],'location':'iq'},{'name':'jan','age':33,'numbersOne':['999','111','222','333','444'],'location':'in'}];
    var secondarray=[{'name':'aga','age':12,'numbersTwo':['111','222','333','444'],'location':'usa'},{'name':'jan','age':35,'numbersTwo':['111','222','333','444'],'location':'uk'}];
    var un_mached_rows=[]; var tmp_recorder={};
    secondarray.forEach(function(secondarrayElements){
    if(secondarrayElements.hasOwnProperty('numbersTwo'))
        secondarrayElements.numbersTwo.forEach(function(numberoneElements){
            firstarry.forEach(function(firstarryElements){
                if(firstarryElements.hasOwnProperty('numbersOne')){
                    firstarryElements.numbersOne.forEach(function(numbersOneElements){
                        if(secondarrayElements.numbersTwo.indexOf(numbersOneElements)===-1)
                        {
                         tmp_recorder.name = firstarryElements.name;
                          tmp_recorder.age = firstarryElements.age;
                          tmp_recorder.location = firstarryElements.location;
                          tmp_recorder.numbers = numberOneElements;
                          un_mached_rows.push(tmp_recorder);
                          tmp_recorder;  
                        }

                    });
                }
            });
        });
    });
    conslole.log(un_mached_rows);

I循环应返回包含两个对象的数组,如un_mached_rows=[{'name':'alex','age':22,'numbers':'555','location':'iq'},{'name':'jan','age':33,'numbers':'999','location':'in'}]

但它确实无法正常工作,我很困惑,请帮忙

1 个答案:

答案 0 :(得分:0)

    var firstarry=[
    {
    	'name':'alex',
    	'age':22,
    	'numbersOne':['111','222','333','444','555'],
    	'location':'iq'
    },{
    	'name':'jan',
    	'age':33,
    	'numbersOne':['999','111','222','333','444'],
    	'location':'in'
    	}
    ];
    var secondarray=[
    {
    	'name':'aga',
    	'age':12,
    	'numbersTwo':['111','222','333','444'],
    	'location':'usa'
    	},{
    		'name':'jan',
    		'age':35,
    		'numbersTwo':['111','222','333','444'],
    		'location':'uk'
    		}
    ];


    var un_mached=[];

    for(var seckey in secondarray){
    	  var numbersTwo=secondarray[seckey].numbersTwo;
    	  for(var firkey in firstarry){
    	  	  numbersOne=firstarry[firkey].numbersOne;;
              for(var nokey in numbersOne){
              	 var numbersOne_el=numbersOne[nokey];
              	 if($.inArray(numbersOne_el, numbersTwo) === -1) {
              	 	delete firstarry[firkey].numbersOne;
              	 	firstarry[firkey].numbers=numbersOne_el;
                    un_mached.push(firstarry[firkey]);
                    delete firstarry[firkey];
              	 }
              }

    	  }
    }

    console.log(un_mached);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>