【问题标题】:How can I filter a JavaScript array by element first letter?如何按元素首字母过滤 JavaScript 数组?
【发布时间】:2020-11-23 18:00:57
【问题描述】:

假设我要搜索ticks数组并返回数组中以S开头的所有项目,然后将它们写入sCompanies = []。

有人知道我如何使用 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);*/

【问题讨论】:

  • 这个问题与许多其他关于 SO 上的 JavaScript 数组的问题非常相似。你尝试过什么来实现你的目标?我没有看到任何代码可以做到这一点。
  • 您特别要求提供涉及“for 或 while 循环”的解决方案。这是(似乎是)家庭作业的规定要求吗?从更大的角度来看,您似乎没有尝试自己解决此问题。对于未来的问题,您应该始终这样做,因为 Stack Overflow 不是代码编写或家庭作业解决服务。

标签: javascript arrays string loops search


【解决方案1】:

这将通过ticters 数组,如果它以“S”开头,则将其添加到sCompanies 数组中。

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

【讨论】:

    【解决方案2】:

    你的循环会是这样的:

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

    或者更现代一点

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

    最好使用for...of,它用于循环数组。

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

    或者你可以像上面的答案那样做一个单行。

    【讨论】:

      【解决方案3】:

      我还得到了以下代码作为模型解决方案,我理解使用这种格式的原因是因为我想定位第一个字母以外的东西:

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

      然后我可以使用 [1] 而不是 [0] 来定位第二个字母。

      【讨论】:

        【解决方案4】:

        为什么不直接使用这样的过滤功能呢?

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

        但如果你想用 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);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-12-25
          • 2019-02-08
          • 1970-01-01
          • 2020-09-25
          • 2015-06-09
          • 2019-04-20
          • 1970-01-01
          • 2016-09-04
          相关资源
          最近更新 更多