简体   繁体   中英

How to make Object value to Object property in JavaScript

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 aa=Object.values(category).map(i=>{ Object.keys(category).map(j=>{ i=j }) }) console.log(a)

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);
let a = {};
for (let key in category)
{
    a[category[key]] = key;
}

or

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

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 aa=Object.values(category).filter(i=>{ if(i=='General') { return Object.keys(i) } }) console.log(a)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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