简体   繁体   English

如何按元素首字母过滤 JavaScript 数组?

[英]How can I filter a JavaScript array by element first letter?

Let say I want to search the tickers array and return all of the items in the array that start with S and then write them to the sCompanies = [].假设我想搜索 tickers 数组并返回数组中以 S 开头的所有项目,然后将它们写入 sCompanies = []。

Anyone have any idea how I go about this using a for or while loop?任何人都知道我 go 如何使用 for 或 while 循环?

// Iterate through this list of tickers to build your new array:
let tickers = ['A', 'SAS', 'SADS' 'ZUMZ'];

//console.log(tickers);



// Define your empty sCompanies array here:

//Maybe need to use const sComapnies = [] ?
let sCompanies = []


// Write your loop here:


for (i = 0; i < tickers.length; i++) {
  console.log(tickers[i]);
  
}



// Define sLength here:

sLength = 'test';

/*
// These lines will log your new array and its length to the console:
console.log(sCompanies);
console.log(sLength);*/

Why don't you just use the filter function like this?你为什么不像这样使用过滤器 function 呢?

// Only return companies starting by "S"
const sCompanies = tickers.filter((companyName) => companyName.startsWith('S')) 

But if you want to do it with a for loop, you can check this:但是如果你想用 for 循环来做,你可以检查一下:

 // Iterate through this list of tickers to build your new array: const tickers = ["A", "SAS", "SADS", "ZUMZ"]; //console.log(tickers); // Define your empty sCompanies array here: const sCompanies = []; // Write your loop here: for (let i = 0; i < tickers.length; i++) { tickers[i].startsWith("S") && sCompanies.push(tickers[i]); } // Define sLength here: const sLength = sCompanies.length; /* // These lines will log your new array and its length to the console: */ console.log(sCompanies); console.log(sLength);

with your loop it would be like this:在你的循环中它会是这样的:

for (i = 0; i < tickers.length; i++) {
  if (tickers[i].startsWith('S')) {
    sCompanies.push(tickers[i]);
  }
}

OR a bit more modern或者更现代一点

for (const i in tickers) {
  if (tickers[i].startsWith('S')) {
    sCompanies.push(tickers[i]);
  }
}

Better yet using for...of which is for looping arrays.更好的是使用for...of循环 arrays。

for (const ticker of tickers) {
  if (ticker.startsWith('S')) {
    sCompanies.push(ticker);
  }
}

Or you could do a oneliner like the answer above.或者你可以像上面的答案一样做一个 oneliner。

This would go through the tickers array and if it starts with "S", add it to the sCompanies array.这将 go 通过代码数组,如果它以“S”开头,则将其添加到 sCompanies 数组。

tickers.forEach(function (item, index, array) {
    if (item.startsWith('S')) {
        sCompanies.push(item);
    }
})

I also got the following code back as the model solution, I understood the reason to use this format is incase I wanted to target something other than first letter:我还得到了以下代码作为 model 解决方案,我理解使用这种格式的原因是因为我想针对首字母以外的其他内容:

if(tickers[i][0] == 'S')

Then I could use [1] instead of [0] to target the second letter.然后我可以使用 [1] 而不是 [0] 来定位第二个字母。

暂无
暂无

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

相关问题 如何根据所包含属性的首字母过滤数组? - How can I filter an array based on the first letter of a contained property? 如何在 react/javascript 中通过首字母过滤对象数组 - How to filter an array of objects by their first letter in react/javascript 使用 filter 方法检查数组每个元素的第一个字母是否大写。 但我不断得到整个数组 - Check if the first letter of each element of an array is capital using filter method. But I keep getting the whole array 如何使这个 javascript 大写字段的所有首字母? - How can I make this javascript to capitilize all first letter of a field? 如何根据数组的字符串元素的第一个字符在Javascript中对数组进行排序? -JavaScript - How can i sort array in Javascript based on the first characters of the string element of the array ? - javascript 如何创建目录列表,使用javascript将项目的每个首字母排序到一个字母组 - How can I create a Directory listing that sorts each first letter of a item to a letter-group with javascript 如何返回第一个字母为大写字母的数组的所有元素 - how can I return all the elements of an array with the first letter as a capital letter Javascript 每个数组元素的首字母只显示一次 - Javascript Display the first letter of every array element only once 取出数组中每个元素的第一个和最后一个字母(javascript) - Take out first and last letter in every element in array(javascript) 使用JavaScript将每个数组元素的首字母大写 - Capitalize first letter of each array element using JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM