gpt4 book ai didi

javascript - 过滤 JSON 对象内容

转载 作者:行者123 更新时间:2023-12-02 23:06:22 27 4
gpt4 key购买 nike

我有一个 json 文件,它是来自检测图像中对象的 REST 端点的响应。在我的文件中,我需要获取边界框并为货架图构建货架数据。

目前我陷入了过滤我得到的输出的困境。这是我的代码。

let response = [
{
"boundingBox": {
"maxX": 660,
"maxY": 971,
"minX": 582,
"minY": 735
},
"label": "other",
"probability": 0.698079
},
{
"boundingBox": {
"maxX": 407,
"maxY": 969,
"minX": 326,
"minY": 723
},
"label": "other",
"probability": 0.6874929
},
{
"boundingBox": {
"maxX": 569,
"maxY": 963,
"minX": 489,
"minY": 728
},
"label": "other",
"probability": 0.6783268
},
{
"boundingBox": {
"maxX": 476,
"maxY": 960,
"minX": 395,
"minY": 725
},
"label": "other",
"probability": 0.6762803
},
{
"boundingBox": {
"maxX": 507,
"maxY": 643,
"minX": 442,
"minY": 405
},
"label": "soyaSauce",
"probability": 0.9600686
},
{
"boundingBox": {
"maxX": 574,
"maxY": 647,
"minX": 479,
"minY": 428
},
"label": "soyaSauce",
"probability": 0.9461142
},
{
"boundingBox": {
"maxX": 466,
"maxY": 662,
"minX": 363,
"minY": 421
},
"label": "soyaSauce",
"probability": 0.94173825
},
{
"boundingBox": {
"maxX": 679,
"maxY": 647,
"minX": 578,
"minY": 432
},
"label": "soyaSauce",
"probability": 0.9386765
},
{
"boundingBox": {
"maxX": 874,
"maxY": 656,
"minX": 810,
"minY": 401
},
"label": "soyaSauce",
"probability": 0.93829554
},
{
"boundingBox": {
"maxX": 743,
"maxY": 661,
"minX": 642,
"minY": 430
},
"label": "soyaSauce",
"probability": 0.9326571
},
{
"boundingBox": {
"maxX": 393,
"maxY": 664,
"minX": 292,
"minY": 427
},
"label": "soyaSauce",
"probability": 0.9322365
},
{
"boundingBox": {
"maxX": 805,
"maxY": 662,
"minX": 743,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.9173948
},
{
"boundingBox": {
"maxX": 332,
"maxY": 661,
"minX": 226,
"minY": 425
},
"label": "soyaSauce",
"probability": 0.9157952
},
{
"boundingBox": {
"maxX": 703,
"maxY": 298,
"minX": 603,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.90590423
},
{
"boundingBox": {
"maxX": 737,
"maxY": 306,
"minX": 669,
"minY": 44
},
"label": "soyaSauce",
"probability": 0.9048507
},
{
"boundingBox": {
"maxX": 251,
"maxY": 319,
"minX": 182,
"minY": 28
},
"label": "soyaSauce",
"probability": 0.90457577
},
{
"boundingBox": {
"maxX": 822,
"maxY": 299,
"minX": 719,
"minY": 35
},
"label": "soyaSauce",
"probability": 0.903023
},
{
"boundingBox": {
"maxX": 609,
"maxY": 665,
"minX": 539,
"minY": 411
},
"label": "soyaSauce",
"probability": 0.8928388
},
{
"boundingBox": {
"maxX": 943,
"maxY": 298,
"minX": 875,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.88029706
},
{
"boundingBox": {
"maxX": 636,
"maxY": 301,
"minX": 534,
"minY": 51
},
"label": "soyaSauce",
"probability": 0.8651045
},
{
"boundingBox": {
"maxX": 351,
"maxY": 296,
"minX": 252,
"minY": 24
},
"label": "soyaSauce",
"probability": 0.8629094
},
{
"boundingBox": {
"maxX": 874,
"maxY": 290,
"minX": 777,
"minY": 18
},
"label": "soyaSauce",
"probability": 0.8621941
},
{
"boundingBox": {
"maxX": 443,
"maxY": 291,
"minX": 343,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.8542643
},
{
"boundingBox": {
"maxX": 562,
"maxY": 302,
"minX": 463,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.85307074
},
{
"boundingBox": {
"maxX": 948,
"maxY": 653,
"minX": 852,
"minY": 391
},
"label": "soyaSauce",
"probability": 0.8488111
},
{
"boundingBox": {
"maxX": 772,
"maxY": 663,
"minX": 702,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.82573885
},
{
"boundingBox": {
"maxX": 270,
"maxY": 646,
"minX": 176,
"minY": 416
},
"label": "soyaSauce",
"probability": 0.77004087
},
{
"boundingBox": {
"maxX": 504,
"maxY": 288,
"minX": 409,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.76433444
},
{
"boundingBox": {
"maxX": 903,
"maxY": 274,
"minX": 847,
"minY": 30
},
"label": "soyaSauce",
"probability": 0.51238227
}
];



var data_filter = response.filter( element => element.label = 'soyaSauce')
console.log(data_filter);

目前,当我运行这个时,我可以获得所有包含 soyaSaucelabels (我写这个只是为了测试),但我的实际用例是找到 minX 的最小值并返回该节点作为响应。

例如,如果 minX 的最小值是 326。我想打印


{
“边界框”:{
“最大X”:407,
“最大Y”:969,
“最小X”:326,
“分钟”:723
},
“标签”:“其他”,
“概率”:0.6874929
}
在输出中。我可以使用下面的代码获得最小值。

let values = response.map(function (v) {
return v.boundingBox.minX;
});
var min = Math.min.apply(null, values);
console.log(min);

但我想在输出中获取相应的json对象。

我尝试将这两种逻辑结合起来,并编写了如下逻辑。

var data_filter = response.filter(element => element.boundingBox.minX = min)
console.log(data_filter);

不幸的是,我得到的输出如下。而不是返回单个 json 对象:-(

176
[ { boundingBox: { maxX: 660, maxY: 971, minX: 176, minY: 735 },
label: 'other',
probability: 0.698079 },
{ boundingBox: { maxX: 407, maxY: 969, minX: 176, minY: 723 },
label: 'other',
probability: 0.6874929 },
{ boundingBox: { maxX: 569, maxY: 963, minX: 176, minY: 728 },
label: 'other',
probability: 0.6783268 },
{ boundingBox: { maxX: 476, maxY: 960, minX: 176, minY: 725 },
label: 'other',
probability: 0.6762803 },
{ boundingBox: { maxX: 507, maxY: 643, minX: 176, minY: 405 },
label: 'soyaSauce',
probability: 0.9600686 },
{ boundingBox: { maxX: 574, maxY: 647, minX: 176, minY: 428 },
label: 'soyaSauce',
probability: 0.9461142 },
{ boundingBox: { maxX: 466, maxY: 662, minX: 176, minY: 421 },
label: 'soyaSauce',
probability: 0.94173825 },
{ boundingBox: { maxX: 679, maxY: 647, minX: 176, minY: 432 },
label: 'soyaSauce',
probability: 0.9386765 },
{ boundingBox: { maxX: 874, maxY: 656, minX: 176, minY: 401 },
label: 'soyaSauce',
probability: 0.93829554 },
{ boundingBox: { maxX: 743, maxY: 661, minX: 176, minY: 430 },
label: 'soyaSauce',
probability: 0.9326571 },
{ boundingBox: { maxX: 393, maxY: 664, minX: 176, minY: 427 },
label: 'soyaSauce',
probability: 0.9322365 },
{ boundingBox: { maxX: 805, maxY: 662, minX: 176, minY: 410 },
label: 'soyaSauce',
probability: 0.9173948 },
{ boundingBox: { maxX: 332, maxY: 661, minX: 176, minY: 425 },
label: 'soyaSauce',
probability: 0.9157952 },
{ boundingBox: { maxX: 703, maxY: 298, minX: 176, minY: 46 },
label: 'soyaSauce',
probability: 0.90590423 },
{ boundingBox: { maxX: 737, maxY: 306, minX: 176, minY: 44 },
label: 'soyaSauce',
probability: 0.9048507 },
{ boundingBox: { maxX: 251, maxY: 319, minX: 176, minY: 28 },
label: 'soyaSauce',
probability: 0.90457577 },
{ boundingBox: { maxX: 822, maxY: 299, minX: 176, minY: 35 },
label: 'soyaSauce',
probability: 0.903023 },
{ boundingBox: { maxX: 609, maxY: 665, minX: 176, minY: 411 },
label: 'soyaSauce',
probability: 0.8928388 },
{ boundingBox: { maxX: 943, maxY: 298, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.88029706 },
{ boundingBox: { maxX: 636, maxY: 301, minX: 176, minY: 51 },
label: 'soyaSauce',
probability: 0.8651045 },
{ boundingBox: { maxX: 351, maxY: 296, minX: 176, minY: 24 },
label: 'soyaSauce',
probability: 0.8629094 },
{ boundingBox: { maxX: 874, maxY: 290, minX: 176, minY: 18 },
label: 'soyaSauce',
probability: 0.8621941 },
{ boundingBox: { maxX: 443, maxY: 291, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.8542643 },
{ boundingBox: { maxX: 562, maxY: 302, minX: 176, minY: 46 },
label: 'soyaSauce',
probability: 0.85307074 },
{ boundingBox: { maxX: 948, maxY: 653, minX: 176, minY: 391 },
label: 'soyaSauce',
probability: 0.8488111 },
{ boundingBox: { maxX: 772, maxY: 663, minX: 176, minY: 410 },
label: 'soyaSauce',
probability: 0.82573885 },
{ boundingBox: { maxX: 270, maxY: 646, minX: 176, minY: 416 },
label: 'soyaSauce',
probability: 0.77004087 },
{ boundingBox: { maxX: 504, maxY: 288, minX: 176, minY: 29 },
label: 'soyaSauce',
probability: 0.76433444 },
{ boundingBox: { maxX: 903, maxY: 274, minX: 176, minY: 30 },
label: 'soyaSauce',
probability: 0.51238227 } ]

请让我知道我哪里出了问题以及如何解决这个问题。

谢谢

最佳答案

您可能想比较 filter 回调中的值而不是分配它

response.filter(element => element.boundingBox.minX === min)

就你而言,情况是这样的。注意过滤器回调

let response = [
{
"boundingBox": {
"maxX": 660,
"maxY": 971,
"minX": 582,
"minY": 735
},
"label": "other",
"probability": 0.698079
},
{
"boundingBox": {
"maxX": 407,
"maxY": 969,
"minX": 326,
"minY": 723
},
"label": "other",
"probability": 0.6874929
},
{
"boundingBox": {
"maxX": 569,
"maxY": 963,
"minX": 489,
"minY": 728
},
"label": "other",
"probability": 0.6783268
},
{
"boundingBox": {
"maxX": 476,
"maxY": 960,
"minX": 395,
"minY": 725
},
"label": "other",
"probability": 0.6762803
},
{
"boundingBox": {
"maxX": 507,
"maxY": 643,
"minX": 442,
"minY": 405
},
"label": "soyaSauce",
"probability": 0.9600686
},
{
"boundingBox": {
"maxX": 574,
"maxY": 647,
"minX": 479,
"minY": 428
},
"label": "soyaSauce",
"probability": 0.9461142
},
{
"boundingBox": {
"maxX": 466,
"maxY": 662,
"minX": 363,
"minY": 421
},
"label": "soyaSauce",
"probability": 0.94173825
},
{
"boundingBox": {
"maxX": 679,
"maxY": 647,
"minX": 578,
"minY": 432
},
"label": "soyaSauce",
"probability": 0.9386765
},
{
"boundingBox": {
"maxX": 874,
"maxY": 656,
"minX": 810,
"minY": 401
},
"label": "soyaSauce",
"probability": 0.93829554
},
{
"boundingBox": {
"maxX": 743,
"maxY": 661,
"minX": 642,
"minY": 430
},
"label": "soyaSauce",
"probability": 0.9326571
},
{
"boundingBox": {
"maxX": 393,
"maxY": 664,
"minX": 292,
"minY": 427
},
"label": "soyaSauce",
"probability": 0.9322365
},
{
"boundingBox": {
"maxX": 805,
"maxY": 662,
"minX": 743,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.9173948
},
{
"boundingBox": {
"maxX": 332,
"maxY": 661,
"minX": 226,
"minY": 425
},
"label": "soyaSauce",
"probability": 0.9157952
},
{
"boundingBox": {
"maxX": 703,
"maxY": 298,
"minX": 603,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.90590423
},
{
"boundingBox": {
"maxX": 737,
"maxY": 306,
"minX": 669,
"minY": 44
},
"label": "soyaSauce",
"probability": 0.9048507
},
{
"boundingBox": {
"maxX": 251,
"maxY": 319,
"minX": 182,
"minY": 28
},
"label": "soyaSauce",
"probability": 0.90457577
},
{
"boundingBox": {
"maxX": 822,
"maxY": 299,
"minX": 719,
"minY": 35
},
"label": "soyaSauce",
"probability": 0.903023
},
{
"boundingBox": {
"maxX": 609,
"maxY": 665,
"minX": 539,
"minY": 411
},
"label": "soyaSauce",
"probability": 0.8928388
},
{
"boundingBox": {
"maxX": 943,
"maxY": 298,
"minX": 875,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.88029706
},
{
"boundingBox": {
"maxX": 636,
"maxY": 301,
"minX": 534,
"minY": 51
},
"label": "soyaSauce",
"probability": 0.8651045
},
{
"boundingBox": {
"maxX": 351,
"maxY": 296,
"minX": 252,
"minY": 24
},
"label": "soyaSauce",
"probability": 0.8629094
},
{
"boundingBox": {
"maxX": 874,
"maxY": 290,
"minX": 777,
"minY": 18
},
"label": "soyaSauce",
"probability": 0.8621941
},
{
"boundingBox": {
"maxX": 443,
"maxY": 291,
"minX": 343,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.8542643
},
{
"boundingBox": {
"maxX": 562,
"maxY": 302,
"minX": 463,
"minY": 46
},
"label": "soyaSauce",
"probability": 0.85307074
},
{
"boundingBox": {
"maxX": 948,
"maxY": 653,
"minX": 852,
"minY": 391
},
"label": "soyaSauce",
"probability": 0.8488111
},
{
"boundingBox": {
"maxX": 772,
"maxY": 663,
"minX": 702,
"minY": 410
},
"label": "soyaSauce",
"probability": 0.82573885
},
{
"boundingBox": {
"maxX": 270,
"maxY": 646,
"minX": 176,
"minY": 416
},
"label": "soyaSauce",
"probability": 0.77004087
},
{
"boundingBox": {
"maxX": 504,
"maxY": 288,
"minX": 409,
"minY": 29
},
"label": "soyaSauce",
"probability": 0.76433444
},
{
"boundingBox": {
"maxX": 903,
"maxY": 274,
"minX": 847,
"minY": 30
},
"label": "soyaSauce",
"probability": 0.51238227
}
];



var data_filter = response.filter( element => element.label === 'soyaSauce')
console.log(data_filter);

关于javascript - 过滤 JSON 对象内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57578486/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com