繁体   English   中英

通过引用另一个数组过滤Javascript子数组元素

[英]Filtering Javascript SubArray Elements By Referencing Another Array

我有两个数组:mainArray和allowedArray。 数组具有以下格式:

起始阵列

mainArray

Array(18) [ "Bill", "AGLUK", "JAW", … ]
Array(18) [ "Bill", "AKI", "MONEY", … ]
Array(18) [ "Tom", "AGLUK", "JAW", … ]
Array(18) [ "Tom", "AKI", "MONEY", … ]
Array(18) [ "Cathy", "AGLUK", "JAW", … ]
Array(18) [ "Cathy", "AKI", "MONEY", … ]

allowedArray

Array(2) [ "Tom", "Cathy" ]

期望的输出

goodArray

Array(18) [ "Tom", "AGLUK", "JAW", … ]
Array(18) [ "Tom", "AKI", "MONEY", … ]
Array(18) [ "Cathy", "AGLUK", "JAW", … ]
Array(18) [ "Cathy", "AKI", "MONEY", … ]

badArray

Array(18) [ "Bill", "AGLUK", "JAW", … ]
Array(18) [ "Bill", "AKI", "MONEY", … ]

我尝试了各种代码,但都失败了:

mainArray.forEach(function(line) {
     allowedArray.forEach(function(ID) {
       if (line[0] == ID[0]) {
       goodArray.push(line);
     }
   });
});

问题 :goodArray始终为空

您可以只使用some方法。 (请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some )。

var answer = mainArray.some(a => allowedArray.indexOf(a) != -1) ? mainArray : [];

如果其任何元素包含在allowedArray中,则将保留整个mainArray。

编辑:将“任意”替换为“某些”。

您可以使用一个辅助对象,并将检查数组作为对应数组的键。

 var array = [["Bill", "AGLUK", "JAW"], [ "Bill", "AKI", "MONEY"], [ "Tom", "AGLUK", "JAW"], [ "Tom", "AKI", "MONEY"], [ "Cathy", "AGLUK", "JAW"], [ "Cathy", "AKI", "MONEY"]], allowedArray = ["Tom", "Cathy"], good = [], bad = [], temp = { true: good, false: bad }; array.forEach(function(line) { temp[allowedArray.indexOf(line[0]) !== -1].push(line); }); console.log(good); console.log(bad); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

对于更新的ES,使用Array#includes短一些。

 var array = [["Bill", "AGLUK", "JAW"], [ "Bill", "AKI", "MONEY"], [ "Tom", "AGLUK", "JAW"], [ "Tom", "AKI", "MONEY"], [ "Cathy", "AGLUK", "JAW"], [ "Cathy", "AKI", "MONEY"]], allowedArray = ["Tom", "Cathy"], good = [], bad = [], temp = { true: good, false: bad }; array.forEach(line => temp[allowedArray.includes(line[0])].push(line)); console.log(good); console.log(bad); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

如果我们将问题翻译成英文,我们会得到如下信息:

对于mainArray每个项目,检查其索引0的值是否存在于allowedArray 如果是这样,则将项目添加到goodArray ,否则将项目添加到badArray

这几乎可以从字面上翻译回javascript:

 const mainArray = [ [ "Bill", "AGLUK", "JAW"], [ "Bill", "AKI", "MONEY"], [ "Tom", "AGLUK", "JAW"], [ "Tom", "AKI", "MONEY"], [ "Cathy", "AGLUK", "JAW"], [ "Cathy", "AKI", "MONEY"], ]; const allowedArray = ["Tom", "Cathy"]; const goodArray = []; const badArray = []; // For each item in mainArray.. mainArray.forEach(function(item) { // check if the value for index 0 is present in allowedArray. // If so... if (allowedArray.indexOf(item[0]) !== -1) { // add the item to goodArray, goodArray.push(item); // otherwise... } else { // add the item to badArray badArray.push(item); } }); console.log({ goodArray, badArray }); 
 .as-console-wrapper { max-height: 100% !important; height: auto; } 

如果缩短一点,并且没有注释,它可能看起来像这样:

mainArray.forEach((record) => allowedArray.indexOf(record[0]) !== -1
    ? goodArray.push(record)
    : badArray.push(record));

暂无
暂无

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

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