Javascript:从包含对象的多维数组中返回不匹配的值
Posted
技术标签:
【中文标题】Javascript:从包含对象的多维数组中返回不匹配的值【英文标题】:Javascript : Return Unmatched value from multidimensional arrays that contains object 【发布时间】:2018-10-09 14:52:20 【问题描述】:我有两个多维数组,每个数组都包含一些对象(键/值)我希望循环抛出对象值以找到不匹配的值,如果找到记录具有不匹配值的对象我试过但我没有得到任何帮助请使用纯 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);
我循环应该返回包含两个对象的数组,比如un_mached_rows=['name':'alex','age':22,'numbers':'555','location':'iq','name':'jan','age':33,'numbers':'999','location':'in']
但它不能正常工作,我很困惑,请帮助
【问题讨论】:
【参考方案1】: 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>
【讨论】:
以上是关于Javascript:从包含对象的多维数组中返回不匹配的值的主要内容,如果未能解决你的问题,请参考以下文章