简体   繁体   English

如何访问具有 5 个同名变量的 json 值

[英]How do i access to a json value with 5 variables with the same name

I'm making a NSFW filter and i need the results of the analysis, i need to verify if Pºrn and Hºntie are higher than 0.7, but i dont know how to access the percent value because its all the same我正在制作 NSFW 过滤器,我需要分析结果,我需要验证 Pºrn 和 Hºntie 是否高于 0.7,但我不知道如何访问百分比值,因为它们都是一样的

i first declare it with this:我首先用这个声明它:

let pred = await model.classify(image)

and returns something like this:并返回如下内容:

[
  { className: 'Drawing', probability: 0.8663265705108643 },
  { className: 'H*ntai', probability: 0.13287211954593658 },
  { className: 'Neutral', probability: 0.0006077145808376372 },
  { className: 'P*rn', probability: 0.0001762539177434519 },
  { className: 'S*xy', probability: 0.000017335718439426273 }
]

and because they're the same i dont know how to detect the values of probability of only pºrn and hºntie to check if one of them are higher than 0.7, i hope this doesn't get marked bad.并且因为它们是相同的,所以我不知道如何检测仅 pºrn 和 hºntie 的概率值以检查其中一个是否高于 0.7,我希望这不会被标记为坏。

What you could do is find using Array.prototype.find() each entry for 'Porn' and 'Hentai' .您可以做的是使用Array.prototype.find()查找'Porn''Hentai'每个条目。 Then you can compare the probability of each one:然后你可以比较每一个的概率:

const porn   = pred.find(r => r.className === 'Porn');
const hentai = pred.find(r => r.className === 'Hentai');

// The Math.max is another way of doing:
// if (porn.probability > 0.7 || hentai.probability > 0.7) { ... }

if (Math.max(porn.probability, hentai.probability) > 0.7) {
  // do something
}

Also if the function model.classify() is under your control, you could make it return an Object instead of an Array like this:此外,如果 function model.classify()在您的控制之下,您可以让它返回Object而不是这样的Array

{
  drawing: { probability: 0.8663265705108643 },
  hentai:  { probability: 0.13287211954593658 },
  neutral: { probability: 0.0006077145808376372 },
  porn:    { probability: 0.0001762539177434519 },
  sexy:    { probability: 0.000017335718439426273 }
}

and access each probability like so:并像这样访问每个概率:

if (pred.hentai.probability > 0.7 || pred.porn.probability > 0.7) {
  // do something
}

You can find the probability of the P*rn class name like this:您可以像这样找到P*rn class 名称的概率:

if (results.find(result => result.className == 'P*rn').probability > 0.7) {
  // some stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM