2

i'm coding a decentralized app and i'm stuck. Here is my problem : i'm calling with async function and return an array with object :

const result = [
{ trait_type: "Neck Gear", value: "1" },
{ trait_type: "Alpha Score", value: "6" },
{ trait_type: "Generation", value: "Gen 0" },];

I need to access the line "Alpha score". I have to check if alpha score exist in this object. So i did this :

async function checkAlpha() {
    try {
        const checkAlphaTxn = await traitsContract.compileAttributes([4]) 
                    .then((result) => {
                        // console.log(result.hasOwnProperty("Alpha Score")) //return false
                        console.log(result) //return array above
                    })
    } catch (error) {
        console.log(error)
    }
}

I wanted to use the .hasOwnProperty() method but it returns to me FALSE. But alpha score value exist.

Thank you in advance for those who can unblock me ! :)

Tristan
  • 51
  • 1
  • 4

2 Answers2

2

hasOwnProperty checks an object - you have an array of objects where Alpha Score is actually the value of one of the properties of one of the objects

console.log(result.find(r => r.trait_type == "Alpha Score"))
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 2
    And there's no duplicate? OP is the first asking about this type of problem? – Andreas Jan 28 '22 at 12:04
  • I have tried your solution, it return in the console : TypeError: results.find is not a function at checkAlpha Maybe because i'm coding with React ? – Tristan Jan 28 '22 at 12:55
  • @Tristan react does not change javascript! If you get that message then `results` is not an array. – Jamiec Jan 28 '22 at 13:03
  • Oh yes that's it ! I tried `console.log(typeof results)` and it return me STRING. But i don't understand why the answer look like an Array who contains objects... – Tristan Jan 28 '22 at 13:06
  • 1
    Try doing `JSON.parse` on the string before calling `find` – Jamiec Jan 28 '22 at 13:08
  • YES it works ! JSON.parse was the solution. My function returns a string array => so i use JSON.parse(results) and now i can use find so it works. Thank you a lot – Tristan Jan 28 '22 at 13:12
  • @Tristan glad I could help. – Jamiec Jan 28 '22 at 13:13
0

hasOwnProperty is used for checking keys like trait_type; not values like 'Alpha Score'.

Instead, you can just find an object for which the value for trait_type is equal to "Alpha Score", and then access the value property of the resulting trait object. You can do this using .find, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find?retiredLocale=nl

const getResults = async () => {
  return [
    { trait_type: "Neck Gear", value: "1" },
    { trait_type: "Alpha Score", value: "6" },
    { trait_type: "Generation", value: "Gen 0" }
  ];
};

(async () => {
  const results = await getResults();
  
  const alphaScoreTrait = results.find(t => t.trait_type === "Alpha Score");

  if (!alphaScoreTrait) {
    console.log("No trait found with type 'Alpha Score'");
    return;
  }
  
  console.log(alphaScoreTrait.value); // 6
})();
Luc
  • 544
  • 3
  • 9
  • I have tried this solutions too.. but it return : TypeError: results.find is not a function at checkAlpha I have tried with some() and filter(), they don't work too :( – Tristan Jan 28 '22 at 12:58