提问人:Parth Raval 提问时间:3/27/2023 更新时间:3/27/2023 访问量:32
找不到嵌套对象数组的最小日期
Not able to find minimum date for nested array of object
问:
我有嵌套的对象数组。我想从对象中找到最短日期。但哪一个是从市场对象的 FromDate 在市场数组中查找最小日期的最佳方法。
有时它可能是对象或对象数组。需要检查区域是数组还是对象。
发现它是否有价值可能是一个问题。
这是JS代码:-
{
"Products": {
"Product": {
"ProductId": "8",
"ProductName": "Woven Yarn",
"CreatedDate": "2017-04-20T17:04:07.4",
"Subproducts": {
"SubProduct": [{
"SubProductId": "3",
"SubProductName": "10/1 Carded",
"CreatedDate": "2017-04-20T17:04:14.36",
"Regions": {
"Region": [{
"RegionId": "1",
"RegionName": "Asia",
"CreatedDate": "2017-04-20T17:04:27.03",
"Markets": {
"Market": [{
"MarketId": "1~10",
"MarketName": "FOB China",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~44",
"MarketName": "FOB Pakistan",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~50",
"MarketName": "FOB West India",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "3~44",
"MarketName": "CNF Pakistan",
"FromDate": "2021-01-04T00:00:00",
"CreatedDate": "2021-01-04T00:00:00"
}
]
}
}]
}
}]
},
"Regions": {
"Region": [{
"RegionId": "1",
"RegionName": "Asia",
"CreatedDate": "2017-04-20T17:04:27.03",
"Markets": {
"Market": [{
"MarketId": "1~10",
"MarketName": "FOB China",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~44",
"MarketName": "FOB Pakistan",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~50",
"MarketName": "FOB West India",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "3~44",
"MarketName": "CNF Pakistan",
"FromDate": "2021-01-04T00:00:00",
"CreatedDate": "2021-01-04T00:00:00"
}
]
}
}]
}
}
}
}
我的代码:-
if (res && res.Data && res.Data.Products) {
if (res.Data.Products != null && res.Data.Products != undefined &&
res.Data.Products.Product != null && res.Data.Products.Product != undefined &&
res.Data.Products.Product.Subproducts != undefined) {
if (res.Data.Products.Product.Subproducts.SubProduct.length != undefined) {
if (res.Data.Products.Product.Subproducts.SubProduct.Regions.Region) {
} else {
}
console.log(res.Data.Products.Product.Subproducts.SubProduct);
}
}
}
答:
1赞
ProfDFrancis
3/27/2023
#1
使用递归
这使您可以处理任何组合对象和数组的结构。
在 JS 中,当您使用运算符时,对象和数组都会返回。由于在这两种情况下,对象或数组都会为您提供元素,因此您可以利用这一点来发挥自己的优势:您不需要知道嵌套的每个阶段是对象还是数组。object
typeof
Object.values()
下面是一个示例。
res = {
Data: {
"Products": {
"Product": {
"ProductId": "8",
"ProductName": "Woven Yarn",
"CreatedDate": "2017-04-20T17:04:07.4",
"Subproducts": {
"SubProduct": [{
"SubProductId": "3",
"SubProductName": "10/1 Carded",
"CreatedDate": "2017-04-20T17:04:14.36",
"Regions": {
"Region": [{
"RegionId": "1",
"RegionName": "Asia",
"CreatedDate": "2017-04-20T17:04:27.03",
"Markets": {
"Market": [{
"MarketId": "1~10",
"MarketName": "FOB China",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~44",
"MarketName": "FOB Pakistan",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~50",
"MarketName": "FOB West India",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "3~44",
"MarketName": "CNF Pakistan",
"FromDate": "2021-01-04T00:00:00",
"CreatedDate": "2021-01-04T00:00:00"
}
]
}
}]
}
}]
},
"Regions": {
"Region": [{
"RegionId": "1",
"RegionName": "Asia",
"CreatedDate": "2017-04-20T17:04:27.03",
"Markets": {
"Market": [{
"MarketId": "1~10",
"MarketName": "FOB China",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~44",
"MarketName": "FOB Pakistan",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "1~50",
"MarketName": "FOB West India",
"FromDate": "2015-01-01T00:00:00",
"CreatedDate": "2015-01-01T00:00:00"
},
{
"MarketId": "3~44",
"MarketName": "CNF Pakistan",
"FromDate": "2021-01-04T00:00:00",
"CreatedDate": "2021-01-04T00:00:00"
}
]
}
}]
}
}
}
}
}
function recursiveEarliestDate(obj, earliestDateSoFar = null) {
if ("FromDate" in obj && (obj.FromDate < earliestDateSoFar || !earliestDateSoFar)) {
earliestDateSoFar = obj.FromDate
}
Object.values(obj).forEach(value => {
if (typeof value === "object") {
const childEarliestDate = recursiveEarliestDate(value, earliestDateSoFar);
if (childEarliestDate < earliestDateSoFar || !earliestDateSoFar) {
earliestDateSoFar = childEarliestDate
}
}
})
return earliestDateSoFar
}
console.log(recursiveEarliestDate(res))
评论
0赞
Parth Raval
3/27/2023
这就是我想要的,谢谢伙计,实际上,我已经尝试过了,但没有工作。你的答案是准确的,我想要的......谢谢你节省我的时间,伙计
评论