1. 程式人生 > >【JavaScript】牛客程式設計:在陣列 arr 中,查詢值與 item 相等的元素出現的所有位置

【JavaScript】牛客程式設計:在陣列 arr 中,查詢值與 item 相等的元素出現的所有位置

function findAllOccurrences(arr, target) {
    var a = []
    arr.forEach(function(item, index) {
        if(item === target) {
            a.push(index)
        }
    })
    return a
}
function findAllOccurrences(arr, target) {
    var result = []
    arr.filter(function(elem, index) {
        return target === elem && result.push(index)
    })
    return result
}

相關推薦

JavaScript程式設計陣列 arr 查詢 item 相等元素出現所有位置

function findAllOccurrences(arr, target) { var a = [] arr.forEach(function(item, index) {

JavaScript程式設計實現一個打點計時器

要求 1、從 start 到 end(包含 start 和 end),每隔 100 毫秒 console.log 一個數字,每次數字增幅為 1 2、返回的物件中需要包含一個 cancel 方法,用於停止

JavaScript程式設計使用閉包實現函式 makeClosures

呼叫之後滿足如下條件: 1、返回一個函式陣列 result,長度與 arr 相同 2、執行 result 中第 i 個函式,即 resulti,結果與 fn(arr[i]) 相同 我第一次寫法是這樣的:

JavaScript動畫-小案例小球運動

rand ner res onf mage borde 技術 star 初始 最近的講js運動框架時,做了一個小案例,隨機生成幾個小球,然後讓他們在頁面中跳動,碰到邊界就彈回來並修改顏色。效果如下: 代碼如下: 1 <!doctype html>

Java併發程式設計同步容器

  為了方便編寫出執行緒安全的程式,Java裡面提供了一些執行緒安全類和併發工具,比如:同步容器、併發容器、阻塞佇列、Synchronizer(比如CountDownLatch)。今天我們就來討論下同步容器。   一、為什麼會出現同步容器?   在Java的集合容器框架中,主要有四大類別:Li

題解[網NOIP賽前集訓營-提高組(第五場)]A.同餘方程 位運算

#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const ll mod=998244353; ll m,l1,l2,r1,r2; ll

題解[網NOIP賽前集訓營-提高組(第四場)]C.滅蟲 線性DP+堆優化

題目連結 #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int N=3e3+10; struct node{ int

題解[網NOIP賽前集訓營-提高組(第四場)]B.區間 亂搞

題目連結 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int MAXN=1e7+10; ll a[MAXN]; int

題解[網NOIP賽前集訓營-提高組(第四場)]A.動態點分治 模擬

題目連結 #include<cstdio> typedef long long ll; int t,find; ll l,r,k,x; int main() { //freopen("in.txt","r",stdin); scanf("%d",&a

題解[網NOIP賽前集訓營-提高組(第三場)]C.急開鎖 博弈論+打表

題目連結 #include<cstdio> typedef long long ll; int t,k,l,r; ll len,f[4000010]; int main() { //freopen("in.txt","r",stdin); scanf("

題解[網NOIP賽前集訓營-提高組(第三場)]B.公平競賽 bfs

題目連結 #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; inline int rea

題解[網NOIP賽前集訓營-提高組(第三場)]A.管道維修 數學期望

題目連結 #include<cstdio> #include<algorithm> #include<cmath> using namespace std; typedef long long ll; #define re regist

題解[網NOIP賽前集訓營-提高組(第二場)]C.集合劃分 狀壓DP

題目連結 看了題解後還是沒寫對,只能去看Komachi大佬咋寫的了。 #include<cstdio> #include<cstring> const int N=18,MX=(1<<18)+5; int n,m,k,ban[N]

題解[網NOIP賽前集訓營-提高組(第二場)]B.分糖果 單調棧優化線性DP+容斥原理

題目連結 #include<cstdio> #define re register typedef long long ll; const int N=1e6+10; const int INF=0x3f3f3f3f; const int mod=1e9

題解[網NOIP賽前集訓營-提高組(第二場)]A.方差 字首和

題目連結 我們把方差公式進行化簡。記 s u m

題解[網NOIP賽前集訓營-提高組(第一場)]C.保護 LCA+線段樹動態開點+線段樹合併

題目連結 ___ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=2e5+10; int n,m,hd[N],to

題解[網NOIP賽前集訓營-提高組(第一場)]B.數數字 線性DP

題目連結 #include<cstdio> #include<cstring> typedef long long ll; #define _rep(i,a,b) for(int i=(a);i<=(b);i++) #define _for(

題解[網NOIP賽前集訓營-提高組(第五場)]B.旅遊 最小生成樹

題目連結 #include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int N=5e5+10,mod=998244353; inlin