You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用三元运算符时遇「Expected 0 arguments, but got 2」错误求助

Got it, let's break down why you're seeing that "Expected 0 arguments, but got 2" error. The issue is that the value parameter being passed to prepareTickerIn isn't always an array—but you're calling .map() on it directly. When value is a single object (not an array), you're either trying to call a non-array map method that doesn't accept arguments, or the value is something that doesn't have the standard array .map() method at all.

Here's how to fix this, with two versions of the solution:

Quick Fix: Ensure value is always an array first

prepareTickerIn(value: any){ 
  let valueToReturn = ''; 
  // Convert value to an array if it's a single item
  const items = Array.isArray(value) ? value : [value];
  
  items.map((item, i, arr) => { 
    valueToReturn += (arr.length - 1 === i) 
      ? (i === 0 ? item.id : `tickerId.in=${item.id}`) 
      : (i === 0 ? `${item.id}&` : `tickerId.in=${item.id}&`); 
  }) 
  console.log(valueToReturn); 
  return valueToReturn; 
}

Cleaner, More Robust Version

We can simplify the string concatenation using Array.join() to avoid manual & handling (which is easier to read and less error-prone):

prepareTickerIn(value: any){ 
  // Guarantee we're working with an array
  const items = Array.isArray(value) ? value : [value];
  
  // Build each query part
  const queryParts = items.map((item, index) => 
    index === 0 ? item.id : `tickerId.in=${item.id}`
  );
  
  // Join all parts with "&" to form the final string
  const valueToReturn = queryParts.join('&');
  
  console.log(valueToReturn); 
  return valueToReturn; 
}

Why this solves the error:

  • The Array.isArray() check ensures we never call .map() on a non-array value. If item (from openSearched) is a single object instead of an array, we wrap it in an array so the standard array .map() method works as intended.
  • Using join('&') removes the need for conditional checks to avoid trailing & characters—it handles all the concatenation automatically.

You could also adjust openSearched to pass an array explicitly (like this.prepareTickerIn([item])), but handling it inside prepareTickerIn makes the function more flexible (it can accept both single items and arrays as input).

内容的提问来源于stack exchange,提问作者Black Mamba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:48:44