1

I'm currently making a discord.js bot that helps with auto-moderation using perspective API. After making the API calls to perspective's API, I then sort through each "attribute". The problem is I'm left with this sort of thing:

enter image description here

However, I am not sure how I would sort it by the summaryScore.value, and then also get the top result from that. Any help is appreciated! Here is my current code:

try {
  google.discoverAPI(DISCOVERY_URL)
    .then(client => {
      const analyzeRequest = {
        comment: {
          text: message.content,
        },
        requestedAttributes: {
          SEVERE_TOXICITY: {
            scoreThreshold: 0.85,
          },
          IDENTITY_ATTACK: {
            scoreThreshold: 0.85,
          },
          THREAT: {
            scoreThreshold: 0.85,
          },
          INSULT: {
            scoreThreshold: 0.85,
          },
          PROFANITY: {
            scoreThreshold: 0.85,
          },
          FLIRTATION: {
            scoreThreshold: 0.85,
          },
          SEXUALLY_EXPLICIT: {
            scoreThreshold: 0.85,
          },
          INCOHERENT: {
            scoreThreshold: 0.85,
          },
          INFLAMMATORY: {
            scoreThreshold: 0.85,
          },
          SPAM: {
            scoreThreshold: 0.85,
          },
        },
        languages: ['en'],
      };
      client.comments.analyze({
          key: API_KEY,
          resource: analyzeRequest,
        },
        (err, response) => {
          if (err) console.log(err);
          else {
            complete = false;
            if (response.data.attributeScores) {
              var array = [response.data.attributeScores]
              for (const attributeScore of array) {
               console.log(attributeScore)
                if (attributeScore.name.summaryScore.value > 0.85) {
                  message.reply(`your message has been removed for: "${attributeScore.name}".`)
                  message.delete()
                }
              }
            }
          }
        });
    })
    .catch(err => {
      console.log(err);
    });

} catch (err) {
}

Thank you so much! Any help is appreciated! I am new-ish to javascript so I apologize if it's a simple answer.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
FxllenCode
  • 63
  • 1
  • 1
  • 14

1 Answers1

1

You can convert your object to an array and sort it by summaryScore.value

const obj = {
  SEVERE_TOXICITY: {
    spanScores: [],
    summaryScore: { value: 0.9197861, type: 'PROBABILITY' }
  },
  PROFANITY: {
    spanScores: [],
    summaryScore: { value: 0.9844065, type: 'PROBABILITY' }
  },
  INSULT: {
    spanScores: [],
    summaryScore: { value: 0.9806645, type: 'PROBABILITY' }
  },
}

const array = Object.entries(obj)
  .map(([key, value]) => ({id: key, ...value}))

array.sort(
  (a, b) => b.summaryScore.value - a.summaryScore.value
)
console.log(`The top one is ${array[0].id} with a value of ${array[0].summaryScore.value}`)

Or, if you only need the object with the highest value, you can iterate over the object entries and check for the highest one:

const obj = {
  SEVERE_TOXICITY: {
    spanScores: [],
    summaryScore: { value: 0.9197861, type: 'PROBABILITY' }
  },
  PROFANITY: {
    spanScores: [],
    summaryScore: { value: 0.9844065, type: 'PROBABILITY' }
  },
  INSULT: {
    spanScores: [],
    summaryScore: { value: 0.9806645, type: 'PROBABILITY' }
  },
}

let highest = null
for (let [id, value] of Object.entries(obj)) {
  if (!highest || value.summaryScore.value > highest.summaryScore.value)
    highest = { id, ...value }
}

console.log(highest)
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57