无法在 TypeScript 中执行连接
Not able to perform join in TypeScript
我有一个对象数组,如下所示
var tempObj = [{
"isAvailable": true,
"receipent": [{
"id": "a6aedf0c34",
"receipentName": "ABC"
}, {
"id": "a6aedbc34",
"receipentName": "XYZ"
}] }]
而且我想要以逗号分隔的字符串形式的名称值。
我使用下面的代码来实现:
var b = Array.prototype.map.call(tempObj.receipent, function (item) {
return item.receipentName;
}).join(",");
但我收到以下错误:
未捕获(承诺)TypeError:Array.prototype.map 调用 null 或 undefined
我也试过这个:
var to = tempObj.receipent;
var b = to.map(e => e.receipentName).join(",");
为此,我收到以下错误:
无法读取未定义的 属性'map'
您的 tempObj
本身就是一个数组。如果你只想处理它的第一个元素,那么你必须使用 tempObj[0]
然后按如下方式做
var tempObj = [{
"isAvailable": true,
"receipent": [{
"id": "a6aedf0c34",
"receipentName": "ABC"
}, {
"id": "a6aedbc34",
"receipentName": "XYZ"
}] }];
//tempObj = JSON.parse(tempObj);
var b = tempObj[0].receipent.map(o => o.receipentName).join(",");
console.log(b);
// In case you want to do it for every tempObj then do as follows
tempObj.forEach(obj => {
const _b = obj.receipent.map(o => o.receipentName).join(",");
console.log(_b);
});
我有一个对象数组,如下所示
var tempObj = [{
"isAvailable": true,
"receipent": [{
"id": "a6aedf0c34",
"receipentName": "ABC"
}, {
"id": "a6aedbc34",
"receipentName": "XYZ"
}] }]
而且我想要以逗号分隔的字符串形式的名称值。
我使用下面的代码来实现:
var b = Array.prototype.map.call(tempObj.receipent, function (item) {
return item.receipentName;
}).join(",");
但我收到以下错误:
未捕获(承诺)TypeError:Array.prototype.map 调用 null 或 undefined
我也试过这个:
var to = tempObj.receipent;
var b = to.map(e => e.receipentName).join(",");
为此,我收到以下错误:
无法读取未定义的 属性'map'
您的 tempObj
本身就是一个数组。如果你只想处理它的第一个元素,那么你必须使用 tempObj[0]
然后按如下方式做
var tempObj = [{
"isAvailable": true,
"receipent": [{
"id": "a6aedf0c34",
"receipentName": "ABC"
}, {
"id": "a6aedbc34",
"receipentName": "XYZ"
}] }];
//tempObj = JSON.parse(tempObj);
var b = tempObj[0].receipent.map(o => o.receipentName).join(",");
console.log(b);
// In case you want to do it for every tempObj then do as follows
tempObj.forEach(obj => {
const _b = obj.receipent.map(o => o.receipentName).join(",");
console.log(_b);
});