2015-12-21 55 views
2

我收到以下函數的意外標識符錯誤。javascript中的意外標識符錯誤

function merge (one, two) { 
    one.forEach(function(assign){ 

    //////////this next line is throwing the error//////////// 
     if (two.some(function(req) req.related == assign.rid)) { 
      if (one.some(function(iter) iter.rid == req.rid)) { 
       iter.quantity++; 
      } else { 
       one.push(req); 
      } 
     } 
    }); 

    return one; 
} 

該函數旨在對一組對象進行操作。

+0

我2/3DS確保您有使用匿名函數體周圍的括號,而且,混淆解析器爲試圖評估'req':'function(req){req.related == assign.rid}' – millimoose

+1

爲了讓你的問題更容易回答,你應該考慮[做一個最小化,完整,可驗證的例子](https:// stackoverflow。 com/help/mcve) – millimoose

+0

@millimoose你是對的,我害怕如果我省略了任何代碼,它會刪除必要的信息。我已經足夠新到JavaScript來犯這個錯誤。 – Suavocado

回答

4

你已經錯過了一些{ }各地.some(function()...

function merge (one, two) { 
    one.forEach(function(assign){ 

     if (two.some(function(req){ req.related == assign.rid})) { 
           // ^-- This one you missed 
      if (one.some(function(iter){ iter.rid == req.rid})) { 
            // ^-- This one you missed as well 
       iter.quantity++; 
      } else { 
       one.push(req); 
      } 
     } 
    }); 

    return one; 
} 
1

查看一些函數調用。它肯定是一個函數作爲參數傳遞,所以大括號是必要的。

1

.some()預計函數作爲參數。
如果你想通過匿名函數,然後使用大括號:

if (two.some(function(req) { return req.related == assign.rid; })) 
{ 
    if (one.some(function(iter) { return iter.rid == req.rid; })) 
    { 
     iter.quantity++; 
    } else { 
     one.push(req); 
    } 
} 
1

你缺少open/close braces {}內部功能

寫一個函數應該是這樣的:

$(function(){ 
//code 
}) 
1

你沒有正確地調用你的函數。定義函數參數後需要括號。

例如,這是two.some應該是什麼樣子:

two.some(function(req){ req.related == assign.rid })