2017-02-04 35 views
0

我有一個普遍的問題,我想知道如果任何人有解決它,然後一個更好的方式是我迄今已做的事情。做的東西,如果for循環沒有找到我想要的

我想遍歷數組找到一個對象,並對其進行更新。如果該對象不存在,我想將它追加到數組中。我通常使用更復雜的對象,這使問題更加複雜。

var movies = [ 
    { _id: 1, title:"Movie 1" }, 
    { _id: 2, title: "Another Movie" } 
]; 

var myTheater = { location:"here", current_movies: movies }; 

// A movie to check. The _id may or may not be in movies. 
var aMovie = { _id: 3, title: "Something New" }; 

// boolean flag to let us know if something happened 
var updated = false; 

// look through all movies and my theater and update as needed 
for(var i = 0, len = myTheater.current_movies.length; i<len; i++){ 
    if(myTheater.current_movies[i]._id === aMovie._id){ 
     console.log("Updating movie name."); 
     myTheater.current_movies[i].title = aMovie.title; 
     updated = true; 
     break; 
    } 
} 

// check if anything was updated and add the movie if not 
if(!updated){ 
    myTheater.current_movies.push(aMovie); 
} 

有沒有更好的方法來做到這一點,或使用標誌的最佳選擇?謝謝!

+0

你應該使用'break'結束循環您找到對象之後,除非可以有多個匹配。 – Barmar

回答

1

使用本地Array.prototype.find這樣的:

var movies = [{ _id: 1, title:"Movie 1" }, { _id: 2, title: "Another Movie" }]; 
 
var myTheater = { location:"here", current_movies: movies }; 
 
var aMovie = { _id: 3, title: "Something New" }; 
 

 

 

 
// check if there is a movie with the same id as aMovie 
 
var found = myTheater.current_movies.find(m => m._id == aMovie._id); 
 

 
if(found) // if we found something 
 
    found.title = aMovie.title; // update it's title 
 
else 
 
    /*things to do if nothing was found*/ 
 
    console.log("nothing found!");

箭功能:

的函數(回調),我傳遞給find被稱爲Arrow Function。您可以使用常規的功能這樣叫find

var found = myTheater.current_movies.find(function(m) { 
    return m._id == aMovie._id; 
}); 
+0

我對'=>'不太熟悉。有效地找到函數'currentMovies.find(function(m){return m._id == aMovie._id}'? – jrose

+0

@jrose請參閱我的答案中的**箭頭函數**部分我添加了一個指向參考的鏈接在Mozilla上也是如此。 –

-1

只要使用身份證或其他屬性標識,所以如果最後一項是100,那麼你不必須尋找101可以只檢查過它的陣列或環的大小和比較,如果ID和值它不存在,那麼你可以插入它。雖然JavaScript可能已經有這樣做的一些功能。

+0

請回答這個問題,而不是給出建議。然後,如果答案是好的,你會得到你需要註釋,而不是 – mplungjan

+0

也許不是評論了你專注於我寫的,你會明白,它回答問題的代表。而不是基於OP的基於標題的比較,基於id的會產生正確的結果。除非你希望我複製他的整個代碼並改變一條自我解釋的路線,否則我不認爲這是錯誤的。 –

+0

歡迎來到SO。請注意:a)我沒有投票並且b)是,使用'<>'按鈕將代碼複製到[mcve]中,如接受的答案。請訪問[幫助]瞭解如何回答。我也可以對自己留下評論,讓你想知道爲什麼你的評論被拒絕。 – mplungjan

0

可以使用Array.find檢查和更新,如果影片存在和更新,如果它不

var movies = [{ _id: 1, title:"Movie 1" }, 
     { _id: 2, title: "Another Movie" } 
    ]; 
    var myTheater = { location:"here", current_movies: movies }; 
    var aMovie = { _id: 1, title: "Something New" }; 
    var movie = myTheater.current_movies.find(function(x){ 
    if(x._id===aMovie._id){ 
     console.log("Updating movie name."); 
     x.title = aMovie.title; 
     return true; 
    } 
    }); 
    if(!movie){ 
    myTheater.current_movies.push(aMovie); 
    } 
1

我使用.find()同意,但你不應該發生變異測試函數中的任何數據。相反,使用它來查找該項目並根據它返回的內容(電影或undefined)執行操作。

function createOrUpdate(draftMovie) { 
    var film = movies.find(function(savedMovie){ 
    return savedMovie._id == draftMovie._id; 
    }) 

    if(film){ 
    return film.title = draftMovie.title; 
    } else { 
    return myTheater.currentMovies.push(draftMovie); 
    } 
} 

更多.find()這裏:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

1

首先你會發現,如果電影包含ID
如果它包含您更新,否則你只是推aMovie

以前,你dynamicaly創建ID的數組easyly知道電影包含一個ID和所在:

var movies = [ 
 
     { id: 1, title:"Movie 1" }, 
 
     { id: 2, title: "Another Movie" } 
 
    ]; 
 
var aMovie = { id: 3, title: "Something New" }; 
 
var index = movies.map(x => x.id).indexOf(aMovie.id); 
 
if (index !== -1) movies[index].title = aMovie.title; 
 
else movies.push(aMovie); 
 
console.log(movies);

相關問題