Home > Blockchain >  How to make Object value to Object property in JavaScript
How to make Object value to Object property in JavaScript

Time:11-08

I have an Object named category. Here the property/keys are in short-form and the value is in fullform. I need to assign Object property/keys to Object value for a short time moment.
Like.

'Technology' have to make tech

const category = {
    tech: 'Technology',
    gnr: 'General',
    ent: 'Entertainment',
    sci: 'Science',
}
let a 
a=Object.values(category).map(i=>{
 Object.keys(category).map(j=>{
   i=j
 })
})
console.log(a)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

const categories = {
    tech: 'Technology',
    gnr: 'General',
    ent: 'Entertainment',
    sci: 'Science',
}

const predicate = 'General';

let match;

for (const category in categories) {
    if (categories[category] === predicate) {
        match = category;
    }
}

console.log(match);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

let a = {};
for (let key in category)
{
    a[category[key]] = key;
}

or

let a = {};
Object.entries(category).forEach(kv=>a[kv[1]]=kv[0]);

CodePudding user response:

Nope! Now after getting match with parameter that I will pass, I need that matching parameter Object Value's Property/Keys.
Like, If General Match then I need gnr
Here I am getting 'General' but I need gnr

const category = {
    tech: 'Technology',
    gnr: 'General',
    ent: 'Entertainment',
    sci: 'Science',
}
let a 
a=Object.values(category).filter(i=>{
  if(i=='General')  {
return Object.keys(i)
  }
 
})
console.log(a)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related