我需要一点帮助。我试图保存用户信息,例如完整的卡号,后四位数字和唯一ID,以便可以使用提取请求来更新它。我在单独的数组中推送此信息。它工作正常,现在给出错误Client id is in invalid TypeError: card_last_digit.push(...).slice is not a function
我正在开发语音机器人项目,并且将卡的最后四位存储在一个阵列中。当用户说出他的最后四位数字时,在将后四位数字与用户卡匹配后,我得到唯一ID的索引。
如何使用Object
实现这一目标。这种方法很麻烦,但是效果很好。请指教谢谢
"objects": [
{
"card_number": "3348721238415678",
"created_at": "2018-10-07T06:42:05.768545",
"cust_id": "32892",
"id": 7,
"resource_uri": "/api/card/7/",
"status": "active"
},
{
"card_number": "9878675683213843",
"created_at": "2018-10-09T04:48:18.296061",
"cust_id": "32892",
"id": 11,
"resource_uri": "/api/card/11/",
"status": "active"
}
];
let user_cards = [];
let card_last_digit = [];
let unique_cust_id = [];
for (let key in val) {
user_cards.push(val[key].card_number);
card_last_digit.push(val[key].card_number).slice(-4);
unique_cust_id.push((val[key].id));
}
答案 0 :(得分:0)
var val = { "objects": [
{
"card_number": "3348721238415678",
"created_at": "2018-10-07T06:42:05.768545",
"cust_id": "32892",
"id": 7,
"resource_uri": "/api/card/7/",
"status": "active"
},
{
"card_number": "9878675683213843",
"created_at": "2018-10-09T04:48:18.296061",
"cust_id": "32892",
"id": 11,
"resource_uri": "/api/card/11/",
"status": "active"
}
}
var user_cards = [];
var card_last_digit = [];
var unique_cust_id = [];
for (var key in val) {
var obj = val[key];
obj.forEach(function(value, index, array) {
user_cards.push(obj[index].card_number);
card_last_digit.push(obj[index].card_number.slice(-4));
unique_cust_id.push((obj[index].id));
});
console.log(user_cards);
console.log(card_last_digit);
console.log(unique_cust_id);
}
答案 1 :(得分:0)
如果您只想基于信用卡号的后四位数字获取卡对象,则更好的方法可能是存储后四位数字到卡对象的映射。这样,您可以通过密钥访问直接获得该卡:
let cards = [{
"card_number": "3348721238415678",
"created_at": "2018-10-07T06:42:05.768545",
"cust_id": "32892",
"id": 7,
"resource_uri": "/api/card/7/",
"status": "active"
},
{
"card_number": "9878675683213843",
"created_at": "2018-10-09T04:48:18.296061",
"cust_id": "32892",
"id": 11,
"resource_uri": "/api/card/11/",
"status": "active"
}
];
let cardMapping = {};
cards.forEach(card => {
let lastFourDigits = card.card_number.slice(card.card_number.length - 4);
cardMapping[lastFourDigits] = card;
});
console.log(cardMapping); // key is last four digits, value is card object
console.log(cardMapping["5678"]); // get card directly from last four digits