【发布时间】:2022-08-19 16:57:04
【问题描述】:
我有以下 JSON 数组我想在每个对象中创建一个新字段,这将是对象的计数
我们必须根据状态、商店和名称(所有者详细信息)进行计数
我怎样才能做到这一点,我在下面添加了我的预期输出
var items = [
{
\"id\": 1,
\"status\": \"ORANGE\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 2,
\"status\": \"GREEN\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 3,
\"status\": \"ORANGE\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 4,
\"status\": \"YELLOW\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 5,
\"status\": \"RED\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\":6,
\"status\": \"GREEN\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 7,
\"status\": \"GREEN\",
\"Shop\":\"XYZ\",
\"ownerDetails\":[ {\"name\":\"test2\",\"address\":\"test2\"}]
},
{
\"id\": 8,
\"status\": \"ORANGE\",
\"Shop\":\"XYZ\",
\"ownerDetails\":[ {\"name\":\"test2\",\"address\":\"test2\"}]
},
{
\"id\": 9,
\"status\": \"YELLOW\",
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}]
},
{
\"id\": 10,
\"status\": \"GREEN\",
\"Shop\":\"EFG\",
\"ownerDetails\":[ {\"name\":\"test3\",\"address\":\"test3\"}]
},
{
\"id\": 11,
\"status\": \"GREEN\",
\"Shop\":\"EFG\",
\"ownerDetails\":[ ]
}
]
预期输出:因此,我们必须根据每个商店、状态和名称(ownerDetails)来计算对象
[
{
\"id\": 1,
\"status\": \"ORANGE\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 2,
\"status\": \"GREEN\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 3,
\"status\": \"ORANGE\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 4,
\"status\": \"YELLOW\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 5,
\"status\": \"RED\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 1
},
{
\"id\":6,
\"status\": \"GREEN\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 7,
\"status\": \"GREEN\"
\"Shop\":\"XYZ\",
\"ownerDetails\":[ {\"name\":\"test2\",\"address\":\"test2\"}],
\"Count\": 1
},
{
\"id\": 8,
\"status\": \"ORANGE\"
\"Shop\":\"XYZ\",
\"ownerDetails\":[ {\"name\":\"test2\",\"address\":\"test2\"}],
\"Count\": 1
},
{
\"id\": 9,
\"status\": \"YELLOW\"
\"Shop\":\"ABC\",
\"ownerDetails\":[ {\"name\":\"test1\",\"address\":\"test1\"}],
\"Count\": 2
},
{
\"id\": 10,
\"status\": \"GREEN\"
\"Shop\":\"EFG\"
\"ownerDetails\":[ {\"name\":\"test3\",\"address\":\"test3\"}],
\"Count\": 1
},
{
\"id\": 11,
\"status\": \"GREEN\",
\"Shop\":\"EFG\",
\"ownerDetails\":[ ],
\"Count\": 1
}
]
请看demo
感谢@Nico_ 和@Parth Ravalhelp
下面的代码在\"ownerDetails\":[ ] 时不起作用,我在控制台中收到以下错误Cannot read properties of undefined (reading \'name\')
代码 :
const itemsWithCount = items.map(item => ({
...item,
Count: items.filter(({ status, Shop ,ownerDetails: [{ name }]}) => item.status === status && item.Shop === Shop && item.ownerDetails[0].name === name).length
}));
console.log(itemsWithCount)
-
你有没有尝试过,如果是的话,你能分享一下吗?如果没有,请尝试使用
new Map()构造一个映射,其键将是status和Shop的组合,值将是一个计数。使用它,您可以转换当前数组,以便每个对象都有一个计数。 -
到目前为止,请显示您的代码。
标签: javascript arrays json angularjs