简体   繁体   English

比较2个包含逗号分隔值的数组记录

[英]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.

相关问题 Javascript遍历包含逗号分隔值的数组键 - Javascript loop through array keys containing comma seperated values 如何在Javascript中用逗号分隔值匹配数组? - How to match array with comma seperated values in Javascript? 使用包含逗号分隔值的变量填充下拉框 - Populating a drop down box with a variable containing comma seperated values 将逗号分隔值的字符串转换为多维javascript数组? - converting a string of comma seperated values to a multidimensional javascript array? 如何使用ngrepeat在div内使用数组打印逗号分隔值? - How to print comma seperated values using array inside div with ngrepeat? 如何从对象逗号分隔的数组中获取值 - How to get the values from array with object comma seperated 如果数组对象中的值相同,则用逗号分隔值连接 - concat with comma seperated values if value is same in array objects 如何存储字符串值(如数组值以下)而无括号(用逗号分隔纬度和经度) - how to store string values like below array values without parenthesis seperated by comma as lattitude & longitude 如果一个输入仅包含另一个输入内的值,则将两个文本输入与逗号分隔值进行比较返回 true - Compare two text inputs with comma seperated values return true if one input contains only values that are inside the other input 在Javascript中将对象属性转换为逗号分隔值 - converting object properties to comma seperated values in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM