[英]array intersection with nested arrays
Pretty new to functional javascript, so please bear with me.功能性 javascript 相当新,所以请多多包涵。
Edited for more information.编辑以获取更多信息。
I have an array that is being created from a csv file using:我有一个使用 csv 文件创建的数组:
var url = "URL-TO-CSV/report.csv";
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send(null);
var csvData = new Array();
var jsonObject = request.responseText.split(/\r?\n|\r/);
for (var i = 0; i < jsonObject.length; i++) {
csvData.push(jsonObject[i].split(','));
}
Which results in an array of arrays, not keyed pairs.这会导致 arrays 数组,而不是密钥对。
[
['stringID-1',"string","string","string","string"],
['stringID-2',"string","string","string","string"],
['stringID-3',"string","string","string","string"]
]
And another array generated from a list of id's to be matched which is currently using jQuery (because I haven't figured out raw Javascript for it yet), like so:从要匹配的 id 列表生成的另一个数组当前正在使用 jQuery (因为我还没有找到原始的 Javascript ),如下所示:
$("input:checkbox:not(:checked)").each(function () {
var $this = $(this);
matchData.push($this.attr("id"));
})
Which results in the below type array.这导致以下类型数组。
['stringID-1','stringID-3']
I have tried varying filter syntax but to no avail.我尝试了不同的过滤器语法,但无济于事。 If I don't get an error I get an empty array.
如果我没有收到错误,我会得到一个空数组。
I need to intersect the two and return an array of the matching arrays from csvData so I can then parse it to HTML in an each loop.我需要将两者相交并从 csvData 返回匹配的 arrays 的数组,以便我可以在每个循环中将其解析为 HTML。
I cannot seem to be able to figure this out and would love some direction.我似乎无法弄清楚这一点,并且会喜欢一些方向。 Thanks in advance
提前致谢
I think all you need is a filter
to restrict to rows where the first indexed is found in the other array:我认为您只需要一个
filter
来限制在另一个数组中找到第一个索引的行:
let csvData = [ ['item-to-match-string-id-1', "string data", "string data", "string data"], ['item-to-match-string-id-2', "string data", "string data", "string data"], ['item-to-match-string-id-3', "string data", "string data", "string data"], ['item-to-match-string-id-4', "string data", "string data", "string data"], ['item-to-match-string-id-5', "string data", "string data", "string data"], ] let matchData = ['item-to-match-string-id-2','item-to-match-string-id-5'] let matching = csvData.filter(row => matchData.includes(row[0])) console.log(matching)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.