我有一个如下对象:
{"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [
{
"type": "card",
"accountId": "5299367",
},
{
"type": "Loan",
"accountId": "5299365",
},
{
"type": "card",
"accountId": "8299388",
},
]}
}
如果用户同时拥有贷款和卡,或只是作为用户产品贷款,我需要知道。
是否有任何内置函数在javascript或angular中找到它。
有人有任何建议如何做到这一点。请帮忙。
答案 0 :(得分:2)
您可以使用filter
数组方法。
var obj = {
"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [
{
"type": "card",
"accountId": "5299367",
},
{
"type": "Loan",
"accountId": "5299365",
},
{
"type": "card",
"accountId": "8299388",
},
]
}
};
var loans = obj.user.products.filter(function(product){
return product.type === "Loan";
});
console.log("Loans: " + loans.length);
// supposing that the user has either a Loan or a card. You could
// easily now find out if the user has only loans as below:
if(loans.length === obj.user.products.length){
console.log("The user has only loans");
}else{
var cards = obj.user.products.length - loans.length;
console.log("The user has "+loans.length+" Loan(s) and "+ cards+ " Card(s).");
}
有关此方法的详情,请查看here。
我需要知道用户是否同时拥有贷款和信用卡 贷款作为用户产品。
根据上述代码段,使用filter
方法并将length
的{{1}}与loans
的{{1}}进行比较,您可以回答你的问题。
答案 1 :(得分:0)
您可以循环products
并获取唯一类型
var data = {
"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [{
"type": "card",
"accountId": "5299367",
}, {
"type": "Loan",
"accountId": "5299365",
}, {
"type": "card",
"accountId": "8299388",
}, ]
}
}
var result= data.user.products.reduce(function(p,c){
if(p.indexOf(c.type)<0) p.push(c.type)
return p;
}, [])
console.log(result)
答案 2 :(得分:0)
您可以尝试以下
var obj = {
"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [{
"type": "card",
"accountId": "5299367",
}, {
"type": "Loan",
"accountId": "5299365",
}, {
"type": "card",
"accountId": "8299388",
}, ]
}
};
var results = {};
obj.user.products.reduce(function(oldval, item) {
oldval[item.type] = true;
return oldval;
}, results);
console.log(results.card && results.Loan); // paints true
// Additionally, results have all the information about the unique value for type, hence, can be used as per the need
&#13;
答案 3 :(得分:0)
相同的迭代解决方案,但如果type
与Loan
不同,会立即为您提供。
var obj = {"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [
{
"type": "card",
"accountId": "5299367",
},
{
"type": "Loan",
"accountId": "5299365",
},
{
"type": "card",
"accountId": "8299388",
},
]}
};
function haveDifferentProducts(products){
var isDiff = false;
products.forEach(function(product){
isDiff = product.type != "Loan"; //checks if type not loan
});
return isDiff;
}
console.log(haveDifferentProducts(obj.user.products))
&#13;
答案 4 :(得分:0)
工作演示:
var jsonObj = {
"user": {
"name": "Harry Peter",
"phoneNumber": "12345",
"products": [
{
"type": "card",
"accountId": "5299367",
},
{
"type": "Loan",
"accountId": "5299365",
},
{
"type": "card",
"accountId": "8299388",
},
]
}
};
var totalLoan = jsonObj.user.products.filter(function(item){
return item.type == "Loan";
});
var totalCards = jsonObj.user.products.filter(function(item){
return item.type == "card";
});
if(totalCards.length < 1 && totalLoan.length < 0) {
console.log("Only loan");
} else if (totalCards.length > 0 && totalLoan.length < 1) {
console.log("Only card");
} else {
console.log("Both card as well as loan");
}