在angular应用程序中,我需要显示数组中的数组数。
示例
{
"city_name": "LAHORE",
"family_members": [
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11128",
"gender": "F",
"name": "IREM NILGUN",
"age": "34",
"relationship": "Wife"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11129",
"gender": "F",
"name": "DEFNE ALINA",
"age": "4",
"relationship": "Daughter"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11130",
"gender": "M",
"name": "OMER KARAN",
"age": "1",
"relationship": "Son"
}
]
},
{
"city_name": "LAHORE",
"family_members": [
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5933",
"gender": "F",
"name": "RAFIA SAQIB",
"age": "2",
"relationship": "Daughter"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5934",
"gender": "M",
"name": "MUHAMMAD MAAZ",
"age": "10",
"relationship": "Son"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5935",
"gender": "F",
"name": "GULZAREEN SAQIB",
"age": "29",
"relationship": "Wife"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5936",
"gender": "M",
"name": "MUSAB SAQIB",
"age": "8",
"relationship": "Son"
}
]
}
由于有2个数组,数组中有家庭成员数组。 在第一个阵列中,我有3个家庭成员,在两个阵列中,我有4个家庭成员。
我只需要计算数组的家庭成员。像这样的结果是7。
答案 0 :(得分:2)
尝试一下:
const data = [{ "city_name": "LAHORE", "family_members": [ { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11128", "gender": "F", "name": "IREM NILGUN", "age": "34", "relationship": "Wife" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11129", "gender": "F", "name": "DEFNE ALINA", "age": "4", "relationship": "Daughter" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11130", "gender": "M", "name": "OMER KARAN", "age": "1", "relationship": "Son" } ] }, { "city_name": "LAHORE", "family_members": [ { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5933", "gender": "F", "name": "RAFIA SAQIB", "age": "2", "relationship": "Daughter" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5934", "gender": "M", "name": "MUHAMMAD MAAZ", "age": "10", "relationship": "Son" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5935", "gender": "F", "name": "GULZAREEN SAQIB", "age": "29", "relationship": "Wife" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5936", "gender": "M", "name": "MUSAB SAQIB", "age": "8", "relationship": "Son" } ] }]
const result = data.map(res=>res.family_members.length).reduce((acc,ele)=>acc+ele,0);
console.log(result);
答案 1 :(得分:1)
let memberCount = 0;
const familyObj = {
"city_name": "LAHORE",
"family_members": [
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11128",
"gender": "F",
"name": "IREM NILGUN",
"age": "34",
"relationship": "Wife"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11129",
"gender": "F",
"name": "DEFNE ALINA",
"age": "4",
"relationship": "Daughter"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11130",
"gender": "M",
"name": "OMER KARAN",
"age": "1",
"relationship": "Son"
}
]
},
{
"city_name": "LAHORE",
"family_members": [
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5933",
"gender": "F",
"name": "RAFIA SAQIB",
"age": "2",
"relationship": "Daughter"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5934",
"gender": "M",
"name": "MUHAMMAD MAAZ",
"age": "10",
"relationship": "Son"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5935",
"gender": "F",
"name": "GULZAREEN SAQIB",
"age": "29",
"relationship": "Wife"
},
{
"user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5936",
"gender": "M",
"name": "MUSAB SAQIB",
"age": "8",
"relationship": "Son"
}
]
}
for (let family in familyObj) {
if (familyObj[familyObj].family_members && familyObj[familyObj].family_members.length) {
memberCount += familyObj[familyObj].family_members.length;
}
}
请尝试使用此代码。
我希望它会有所帮助。
答案 2 :(得分:0)
感谢您的提问。
可以这样-
const data = [{ "city_name": "LAHORE", "family_members": [ { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11128", "gender": "F", "name": "IREM NILGUN", "age": "34", "relationship": "Wife" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11129", "gender": "F", "name": "DEFNE ALINA", "age": "4", "relationship": "Daughter" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-11130", "gender": "M", "name": "OMER KARAN", "age": "1", "relationship": "Son" } ] }, { "city_name": "LAHORE", "family_members": [ { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5933", "gender": "F", "name": "RAFIA SAQIB", "age": "2", "relationship": "Daughter" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5934", "gender": "M", "name": "MUHAMMAD MAAZ", "age": "10", "relationship": "Son" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5935", "gender": "F", "name": "GULZAREEN SAQIB", "age": "29", "relationship": "Wife" }, { "user_id": "IGT\/D\/PHI3\/0000000002\/0118\/003-5936", "gender": "M", "name": "MUSAB SAQIB", "age": "8", "relationship": "Son" } ] }]
const sum = 0;
arrayName.forEach(value => {
sum = sum+value.family_members.length;
console.log(sum);});
答案 3 :(得分:0)
假设您的数据结构是诸如
之类的对象数组{city_name:字符串,family_members:数组}
//First map every element to get an array of "family_members" arrays
const totalLength = data
.map(item => item.family_members || []) // || [] prevents errors when "family_members" is not defined
//Now sum the lengths of all the mapped "family_members" arrays
.reduce((tot, members) => tot + members.length, 0);