如何在JSON中找到特定值并在JavaScript中显示这些值的最小值?

发布于 2025-02-04 18:23:58 字数 2830 浏览 2 评论 0原文

我是JS的新手,并尝试从事任务,但找不到同一任务的精确解决方案。 这是我第一次发表问题,所以请纠正我走在哪里。

这是我的问题,

这是我的JSON文件的一小部分,它超过3000行,我给出了同一JSON文件的确切格式,

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "99.919094"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "98.406375"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      }
}

我的问题是如何检查完整的JSON文件并搜索云键和其值并显示值的最小值。

例如 我必须在整个JSON中以M1的名称获取云,这可能发生在12次。

“云”:“ M2”, “ cloud_kpi”:“ m1.e1”,

在上行中,我所需的键将是云,值将为m2。 之后,我必须获取“值”的云的值

:[ 1653633595.484, “ 100” ]

我需要的值是100。

以同样的方式,我必须在完整的JSON文件中检查这些值并获得12个值

m1-100 M1-100 M2-33 M1-44 .......因此,

在获得这些值后,我需要找到最小的这些值并显示它们。

以同样的方式,我必须获取所有云的及其最小值 M1-min(100,98,94 ...) M2-min(100,38,99,...) M3-min(100,39,20 ..) 等等。...

我尝试了地图方法,这使我感到困惑,也尝试了每种方法,我只能获取所有云名称,但不要进一步移动,

response.json().then(function (data) {
                        var jsonarray = data["data"]["result"]                    
                        for (var k in jsonarray) {
                            if (jsonarray[k] instanceof Object) {
                                console.log(jsonarray[k].metric.cloud)                                                
                            } 
                        }
                    });

请让我知道我必须尝试的下一步是什么。

I am new to JS and trying to work on a task and not find an exact solution for the same.
This is the first time I am posting questions so please correct me wherever I am going wrong.

Here is my question

This is a small part of my JSON file which is more than 3000 lines for which I have given the exact format of the same JSON file

{
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "99.919094"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "98.406375"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      }
}

My question is how can I check the complete JSON file and search for the cloud key and its values and display the minimum of the value.

For example
I have to fetch the cloud with the name M1 in the whole JSON which may occur around 12 times.

"cloud": "m2",
"cloud_kpi": "m1.e1"

in the above line, my required key will be cloud and the value will be m2.
after this i have to fetch the value of this cloud that is,

"value": [
1653633595.484,
"100"
]

here the value I need is 100.

in the same way, I have to check for these values in a complete JSON file and get around 12 values

m1-100
m1-100
m2-33
m1-44 .......so on

after getting these values I need to find minimum of these and display them.

In the same way, I have to fetch all the cloud's and its minimum value that is
M1-min(100,98,94...)
M2-min(100,38,99,...)
M3-min(100,39,20..)
and so on....

I tried the map method which was confusing for me and also for each method at last I was just able to fetch all cloud names but not move further

response.json().then(function (data) {
                        var jsonarray = data["data"]["result"]                    
                        for (var k in jsonarray) {
                            if (jsonarray[k] instanceof Object) {
                                console.log(jsonarray[k].metric.cloud)                                                
                            } 
                        }
                    });

Please let me know what is the next step I have to try.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

時窥 2025-02-11 18:23:58
  1. > 将云ID匹配查询的对象。

  2. 在该数组上仅返回您需要的值。

  3. 来自 阵列使用 math.min

const data={status:"success",data:{resultType:"vector",result:[{metric:{ABC:"yes",KPI:"M1",__name__:"m1_status",cloud:"m1",cloud_kpi:"m1 status",instance:"m1.e1.s1",job:"m1",src:"m1.m1.m1",src_vm:"m1",timespan:"2h"},value:[1653633595.484,"100"]},{metric:{ABC:"yes",KPI:"M2",__name__:"m1_status",cloud:"m2",cloud_kpi:"m1.e1",instance:"m2",job:"m2 services",src:"m2.m3",src_vm:"m2",timespan:"2h"},value:[1653633595.484,"99.919094"]},{metric:{ABC:"yes",KPI:"M2",__name__:"m1_status",cloud:"m2",cloud_kpi:"m1.e1",instance:"m2",job:"m2 services",src:"m2.m3",src_vm:"m2",timespan:"2h"},value:[1653633595.484,"98.406375"]},{metric:{ABC:"yes",KPI:"M1",__name__:"m1_status",cloud:"m1",cloud_kpi:"m1 status",instance:"m1.e1.s1",job:"m1",src:"m1.m1.m1",src_vm:"m1",timespan:"2h"},value:[1653633595.484,"999"]}]}};

function findMinimum(data, key) {

  // `filter` out the objects where the value
  // of `cloud` matches your query
  const result = data.data.result.filter(obj => {
    return obj.metric.cloud?.toLowerCase() === key.toLowerCase();

  // For each object return the second element
  // of the value array
  }).map(obj => {
    return obj.value[1];
  });

  // `spread` out the array and use `Math.min` to
  // find the minimum value
  return Math.min(...result);

}

console.log(`Minimum value: ${findMinimum(data, 'M1')}`);

附加文档


  • “ nofollow noreferrer”> =“ https://developer.mozilla.org/en-us/docs/web/javascript/reference/referent/operators/spread_syntax” rel =“ nofollow noreferrer”> spread Syntax

  1. filter out the objects where the cloud id matches the query.

  2. map over that array to return only the value you need.

  3. From that array find the minimum value using Math.min.

const data={status:"success",data:{resultType:"vector",result:[{metric:{ABC:"yes",KPI:"M1",__name__:"m1_status",cloud:"m1",cloud_kpi:"m1 status",instance:"m1.e1.s1",job:"m1",src:"m1.m1.m1",src_vm:"m1",timespan:"2h"},value:[1653633595.484,"100"]},{metric:{ABC:"yes",KPI:"M2",__name__:"m1_status",cloud:"m2",cloud_kpi:"m1.e1",instance:"m2",job:"m2 services",src:"m2.m3",src_vm:"m2",timespan:"2h"},value:[1653633595.484,"99.919094"]},{metric:{ABC:"yes",KPI:"M2",__name__:"m1_status",cloud:"m2",cloud_kpi:"m1.e1",instance:"m2",job:"m2 services",src:"m2.m3",src_vm:"m2",timespan:"2h"},value:[1653633595.484,"98.406375"]},{metric:{ABC:"yes",KPI:"M1",__name__:"m1_status",cloud:"m1",cloud_kpi:"m1 status",instance:"m1.e1.s1",job:"m1",src:"m1.m1.m1",src_vm:"m1",timespan:"2h"},value:[1653633595.484,"999"]}]}};

function findMinimum(data, key) {

  // `filter` out the objects where the value
  // of `cloud` matches your query
  const result = data.data.result.filter(obj => {
    return obj.metric.cloud?.toLowerCase() === key.toLowerCase();

  // For each object return the second element
  // of the value array
  }).map(obj => {
    return obj.value[1];
  });

  // `spread` out the array and use `Math.min` to
  // find the minimum value
  return Math.min(...result);

}

console.log(`Minimum value: ${findMinimum(data, 'M1')}`);

Additional documentation

与风相奔跑 2025-02-11 18:23:58

您可以使用array.filter()查找数组的值和math.min()来查找其参数的最小值

response.json().then(function(data){
    var jsonarray = data.data.result.filter(item => item.metric.cloud == 'm2') // here you will have all of the objects with the cloud m2
    const min = Math.min(...jsonarray.map(item => item.value[1]))
    console.log(min) // the minimum value
})

You can use Array.filter() to find value of an array and Math.min() to find minimum value of its parameters

response.json().then(function(data){
    var jsonarray = data.data.result.filter(item => item.metric.cloud == 'm2') // here you will have all of the objects with the cloud m2
    const min = Math.min(...jsonarray.map(item => item.value[1]))
    console.log(min) // the minimum value
})
月寒剑心 2025-02-11 18:23:58

请检查以下代码:

const data = response.json();
const result = data.data.result;

// mapping
const mappedArray = result.map(x => ({
    cloud: x.metric.cloud,
    value: Number(x.value[1] ?? 0)
}))
const maxSorted = mappedArray.sort((x, y) => y.value - x.value) // sorting
const minSorted = mappedArray.sort((x, y) => x.value - y.value) 
const maxValue = maxSorted[0] ?? null;
const minValue = minSorted[0] ?? null;
console.log(maxValue); // max value
console.log(minValue); // min value

简短样式:

response.json().then((data) => {
    const minValue = data.data.result.map(x => ({
        cloud: x.metric.cloud,
        value: Number(x.value[1] ?? 0)
    })).sort((x, y) => x.value - y.value)
    console.log(minValue)
})

Please, check following code:

const data = response.json();
const result = data.data.result;

// mapping
const mappedArray = result.map(x => ({
    cloud: x.metric.cloud,
    value: Number(x.value[1] ?? 0)
}))
const maxSorted = mappedArray.sort((x, y) => y.value - x.value) // sorting
const minSorted = mappedArray.sort((x, y) => x.value - y.value) 
const maxValue = maxSorted[0] ?? null;
const minValue = minSorted[0] ?? null;
console.log(maxValue); // max value
console.log(minValue); // min value

Short styled:

response.json().then((data) => {
    const minValue = data.data.result.map(x => ({
        cloud: x.metric.cloud,
        value: Number(x.value[1] ?? 0)
    })).sort((x, y) => x.value - y.value)
    console.log(minValue)
})
双手揣兜 2025-02-11 18:23:58

因为您想在数组中所有对象中打印最小值。您只需在单独的数组中获取所有值,然后使用Math.min.min(... array)您可以获取Miminum值。

演示

const obj = {
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "99.919094"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "98.406375"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      }]
  }
};

const values = [];

obj.data.result.forEach(obj => {
    obj.value.forEach(value => {
    values.push(Math.round(value))
  });
});

console.log(Math.min(...values));

As you want to print minimum value among all the objects that you have in the array. You can simply get all the values in a seperate array and then using Math.min(...array) you can fetch the miminum value.

Demo :

const obj = {
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "99.919094"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M2",
          "__name__": "m1_status",
          "cloud": "m2",
          "cloud_kpi": "m1.e1",
          "instance": "m2",
          "job": "m2 services",
          "src": "m2.m3",
          "src_vm": "m2",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "98.406375"
        ]
      },
      {
        "metric": {
          "ABC": "yes",
          "KPI": "M1",
          "__name__": "m1_status",
          "cloud": "m1",
          "cloud_kpi": "m1 status",
          "instance": "m1.e1.s1",
          "job": "m1",
          "src": "m1.m1.m1",
          "src_vm": "m1",
          "timespan": "2h"
        },
        "value": [
          1653633595.484,
          "100"
        ]
      }]
  }
};

const values = [];

obj.data.result.forEach(obj => {
    obj.value.forEach(value => {
    values.push(Math.round(value))
  });
});

console.log(Math.min(...values));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文