有这样一个数组,[‘前端小王’,’后端小张’,’前端小李’,’测试小徐’],如何找到所有以‘前端’开头的元素?请写出代码。
时间: 2023-11-01 12:06:32 浏览: 26
要找到所有以'前端'开头的元素,可以使用数组的filter方法结合startsWith方法来筛选元素。以下是示例代码:
```javascript
const arr = ['前端小王', '后端小张', '前端小李', '测试小徐'];
const result = arr.filter(item => item.startsWith('前端'));
console.log(result);
```
运行以上代码,会输出结果为:['前端小王', '前端小李'],即找到了所有以'前端'开头的元素。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![mp4](https://img-home.csdnimg.cn/images/20210720083504.png)