Angular中无法从对象数组提取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

