我不确定要如何为这个问题加上标题,但是我正在尝试根据index处的匹配字符串从第二个数组添加到一个Array中0
。这位于Google Apps脚本网络应用程序中,这就是为什么它全部基于JavaScript而不是PHP的原因。
// Sent from client
Array1: ["A"]
// Stored on server
Array2: [["A", "June","15"], ["B","October","30"]]
我想基于客户端提交的表单对象返回整个匹配数组。
我试过了:
Array1 = Array1.filter(function(val) {
return Array2.indexOf(val) !== -1; // I thought this would return indices that matched
});
但我得到的是一个空数组,而不是["A","June","15"]
。有任何想法吗?
您使用了错误的filter
函数。您必须filter
Array2
基于Array1
值。
var Array1= ["A","B"]
var Array2=[["A", "June","15"], ["B","October","30"],["C","September","16"]]
var result = Array2.filter(function(val) {
return Array1.indexOf(val[0])!=-1;
});
console.log(result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句