0

I've tried using a foreach loop, using Object properties. using bracket notation, none of these give me what I need. I want to store TT_pistol_(gold) as a variable, and have all the data related to it in another variable. I can't use data['TT_pistol_(gold)'] because it varies in name.

ex. input

{  
"TT_pistol_(gold)":{  
    "weight":0,
    "recoil":750,
    "ergonomics":75,
    "RPM":30,
    "Caliber":"7.62x25mm_Tokarev"
  }
}

ex. output
name = 'TT_pistol_(gold)';
data = '{ "weight":0, "recoil":750, "ergonomics":75, "RPM":30, "Caliber":"7.62x25mm_Tokarev" }'

1 Answer 1

1

Is this what you need?

'use strict';

const obj = {
  "TT_pistol_(gold)":{
    "weight":0,
    "recoil":750,
    "ergonomics":75,
    "RPM":30,
    "Caliber":"7.62x25mm_Tokarev"
  }
};

for (const [name, data] of Object.entries(obj)) {
  console.log(name);
  console.log(data);
}

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I had to wrap it in an array,forEach so that it would work with multiple in an array. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.