[英]Compare 2 array records containing comma seperated values
Here i am trying to compare Cars1 record with myArray record and if they are matching i want to set selected property to true of MyArray for matching record. 在这里,我试图将Cars1记录与myArray记录进行比较,如果它们匹配,我想将MyArray的selected属性设置为true来匹配记录。
Abc.mp3,lmn.mp3,ggg.mp3 of myArray is matching with Abc.mp3,lmn.mp3 of cars1 as because Abc.mp3 is matching in both the array. myArray的Abc.mp3,lmn.mp3,ggg.mp3与cars1的Abc.mp3,lmn.mp3匹配,因为Abc.mp3在两个数组中均匹配。
Here Abc.mp3,lmn.mp3,ggg.mp3 are 3 different records so after splitting by comma if any of the string is matching in cars1(split by comma) then set flag to true. 这里的Abc.mp3,lmn.mp3,ggg.mp3是3个不同的记录,因此,如果在cars1中将任何字符串匹配(用逗号分割),则用逗号分割后,将标志设置为true。
Expected Output : 预期产量:
var myArray = [
{field: 'Abc.mp3,lmn.mp3,ggg.mp3', flag: true}, //Abc.mp3 is matching
{field: 'kkk.mp3', flag: false},
{field: 'Xyz.mp3', flag: true}, //Xyz.mp3 is matching
{field: 'MMM.mp3,UUU.mp3', flag: true}, //UUU.mp3 is matching
];
var myArray = [ {field: 'Abc.mp3,lmn.mp3,ggg.mp3', flag: false}, {field: 'kkk.mp3', flag: false}, {field: 'Xyz.mp3', flag: false}, {field: 'MMM.mp3,UUU.mp3', flag: false}, ]; var cars1 = ["Abc.mp3,lmn.mp3", "Xyz.mp3","UUU.mp3"]; var cars2 = ["S1","S2","S3" ]; var array2 =[]; var map = Object.create(null); cars1.forEach(function (entry, index) { map[entry] = index; }); array2 = myArray.map(function (item) { var index = map[item.field]; if (index === undefined) { return undefined; } item.flag = true; return cars2[index]; }); console.log(myArray);
Update: Now cars1 and cars2 records have 1 to 1 relationship with each other that means Abc.mp3,lmn.mp3 of cars1 points to S1 of cars2 respectively. 更新:现在cars1和cars2记录彼此之间具有1到1的关系,这意味着cars1的Abc.mp3,lmn.mp3分别指向cars2的S1。
While matching myarray with Cars1 if match found then i want to note index of cars1 and based on that index i would like to get record from cars2 and assign to array2 at that index position. 在将myarray与Cars1匹配时(如果找到匹配项),则我想记录cars1的索引,并基于该索引,我想从cars2获取记录并在该索引位置分配给array2。
For eg:Abc.mp3,lmn.mp3,ggg.mp3 of myArray and Abc.mp3,lmn.mp3 of cars1 is matching so index of Abc.mp3,lmn.mp3 is 0 so based on this index get 0th index record from cars2 and assign to array2 at 0th index.for Xyz.mp3 S2 will be at 2nd position in array2. 例如:myArray的Abc.mp3,lmn.mp3,ggg.mp3和cars1的Abc.mp3,lmn.mp3匹配,因此Abc.mp3,lmn.mp3的索引为0,因此基于该索引从cars2并分配给array2的第0个索引。对于Xyz.mp3,S2将位于array2的第二个位置。
Expected output in array2 : 数组2中的预期输出:
array2 = ["S1" ,undefined, "S2","S3" ];
Are you looking for something like this? 您是否正在寻找这样的东西?
var myArray = [ {field: 'Abc.mp3,lmn.mp3,ggg.mp3', flag: false}, {field: 'kkk.mp3', flag: false}, {field: 'Xyz.mp3', flag: false}, {field: 'MMM.mp3,UUU.mp3', flag: false}, ]; var cars1 = ["Abc.mp3,lmn.mp3", "Xyz.mp3","UUU.mp3"]; var carsHash = cars1.reduce(function(p,c) { var split = c.split(","); split.forEach(function(i) { p[i] = true; }); return p; },{}); myArray.forEach(function(i) { var split = i.field.split(","); i.flag = split.some(function(v) { return carsHash[v]; }); }); console.log(myArray);
First we create a hash will all the substrings from cars1
. 首先,我们将创建一个哈希将所有来自
cars1
的子字符串。 Then we just loop through myArray
and if any of the substrings in field
are present in carsHash
we set the i.flag
to true. 然后,我们通过公正循环
myArray
,如果任何的子字符串的field
存在于carsHash
我们设置i.flag
为true。
So you want something like this i think: 所以你想这样的事情我想:
function match(data, selectors) { return selectors.map(function(selector) { let matchedIndex = -1; return Object.assign({}, selector, { flag: data.some(function(item, index) { return selector.field.split(',').some(function(s) { if(item.split(',').indexOf(s) > -1) { matchedIndex = index; return true; } return false; }); }), matchedIndex: matchedIndex }); }); } var myArray = [ {field: 'Abc.mp3,lmn.mp3,ggg.mp3', flag: false}, {field: 'kkk.mp3', flag: false}, {field: 'Xyz.mp3', flag: false}, {field: 'MMM.mp3,UUU.mp3', flag: false}, ]; var cars = ["Abc.mp3,lmn.mp3", "Xyz.mp3","UUU.mp3"]; var cars2 = ["S1", "S2", "S3"]; var matched = match(cars, myArray); var matchedFields = matched.filter(function(item) { return item.flag }); console.log(matched); console.log(matchedFields); var matchedCars2 = matched.map(function(item) { return cars2[item.matchedIndex]}); console.log('matched cars 2', matchedCars2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.