从匹配的键添加到数组 [英] Add to an array from matched key

查看:69
本文介绍了从匹配的键添加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何为问题加上标题,但是我正在尝试根据索引0上的匹配字符串从第二个数组添加到一个数组.这是在Google Apps脚本网络应用程序中,这就是为什么它全部基于JavaScript而不是PHP的原因.

I'm not entirely sure how to title the question, but I'm trying to add to one Array from a second based on a matched string at index 0. This is in a Google Apps Script web app, which is why it's all JavaScript-based rather than PHP.

// Sent from client
Array1: ["A"]

// Stored on server
Array2: [["A", "June","15"], ["B","October","30"]]

我想基于客户端提交的表单对象返回整个匹配数组.

I'd like to return the entire matching array based on the client-submitted form object.

我尝试过:

Array1 = Array1.filter(function(val) {
  return Array2.indexOf(val) !== -1;    // I thought this would return indices that matched
});

但是我得到的是一个空数组,而不是["A","June","15"].有什么想法吗?

but I get an empty array returned instead of ["A","June","15"]. Any ideas?

推荐答案

您使用了错误的filter函数.您必须根据Array1filter Array2.

You used wrong filter function.You have to filter Array2 based on Array1 values.

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);

这篇关于从匹配的键添加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆