简体   繁体   English

基本的Javascript 2d数组问题

[英]Basic Javascript 2d Array issue

I need to find the intercept between two arrays. 我需要找到两个数组之间的截距。 Sometime one of them is empty. 有时其中之一是空的。 For this I use underscore. 为此,我使用下划线。

var fromDB = [[123],[]];
var result = _.intersection(fromDB[0]);

This does not work because fromDB[0] is [123] and not [123],[] . 这不起作用,因为fromDB[0][123]而不是[123],[] Do you know any way of getting [123],[] ? 您知道获得[123],[]任何方法吗?

You can either specify both of the indexes or use the spread operator . 您既可以指定两个索引,也可以使用spread operator

 var fromDB = [ [123], [] ]; console.log(_.intersection(...fromDB)); console.log(_.intersection(fromDB[0], fromDB[1])); fromDB = [ [123], [123] ] console.log(_.intersection(...fromDB)); console.log(_.intersection(fromDB[0], fromDB[1])); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script> 

If you want to access the empty array that you created in fromDB , you need fromDB[1] , that is in this code snippet 如果要访问在fromDB中创建的空数组,则需要fromDB [1],即此代码段中

_.intersection(fromDB[0],fromDB[1]);

I guess this is what you wanted . 我想这就是你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM