双层map循环,返回数据问题,内层map定义一个变量接收内层数组的返回值
问题描述:后台返回一个数组,改变数组里每一项的一个属性值,将这个属性值由字符串改为数组,里层的map要定义一个变量接收返回的数组
有问题代码:
后台返回的response字段是一个字符串,将其转化为数组
patinetRes.map(item => {
item.response.split("。").map((item1, index) => {
if (index < item.response.split("。").length - 1) {
item1 = item1 + “。”;
}
return item1;
});
return item;
});
解决:
patinetRes.map(item => {
let arr2 = item.response.split("。").map((item1, index) => {
if (index < item.response.split("。").length - 1) {
item1 = item1 + “。”;
}
return item1;
});
item.response = arr2;此处不能直接返回item,定义一个变量接收内层循环的返回值,item.response等于这个变量
return item;
});