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

Angular中无法从对象数组提取testName值,请求技术帮助

解决Angular中提取testList里testName的问题

Let's fix this issue right away! The problem in your code is that you're trying to call the map method directly on the entire response object (this.testListData), but map is a method for arrays—not plain objects. Your response object has a testList property which is the actual array you need to work with.

错误原因分析

Your returned testListData is an object structured like this:

{"test":null,"title":null,"testList":[{"testName":"Test.COM","testId":1},{"testName":"Test1.COM","testId":2}],"count":2}

When you do this.testListData.map(...), you're trying to run an array method on the top-level object, which doesn't exist—hence the failure.

修正后的代码

Here's the corrected version of your code, with proper access to the testList array:

this.testService.getTestList(this.testData).subscribe(
  (data: TestModels.ListTestModel) => {
    this.testListData = data;
    // Access the testList array from the response, then map to extract testName
    this.testListDataValue = data.testList.map(testItem => testItem.testName);
  }
);

额外的空值保护(可选)

To avoid errors in case testList is ever null or undefined (for example, if the API returns no data), you can add a fallback to an empty array:

this.testListDataValue = (data.testList || []).map(testItem => testItem.testName);

After running this corrected code, this.testListDataValue will hold the array ["Test.COM", "Test1.COM"] exactly as you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:31:49