0

I want swap key(s) in an object with value(s) and swap value(s) with key(s)

{name:"Mohammad",age:"29"} ==>> {Mohammad:"name",29:"age"}

Below code work:

function swap(oldObj) {
let newObj = {};
for (let i in oldObj) {
    newObj[oldObj[i]] = i;
     }
return newObj;
}

since below code log values(s) in an object:

Actually obj[i] is object's value(S)

function objValue (obj){
    for (let i in obj){
        console.log(obj[i]);
    }
}

I changed swap function (first block code) to:

function swap(oldObj) {
    let newObj = {};
    for (let i in oldObj) {
        newObj.oldObj[i] = i;
         }
    return newObj;
    }

Actually I try call oldObj's value(s) with oldObj[i] and add this as key to newObj with newObj.oldObj[i] instead of newObj[oldObj[i]] but error occur

Uncaught TypeError TypeError: Cannot set properties of undefined (setting 'name')
4
  • newObj.oldObj[i] = i <- this line won't work. Your first version newObj[oldObj[i]] = i is correct. I don't understand what are you asking about. Your first version code works as you want, Isn't your problem solved already? Commented Sep 9, 2022 at 17:18
  • my question is why we can't access oldObj's value whit newObj.oldObj[i] instead of newObj[oldObj[i]] Commented Sep 9, 2022 at 17:21
  • 1
    You got wrong understanding of the syntax. newObj.oldObj[i] means, first read the key named "oldObj" from newObj, let's name the value temp, then read the key that is stored in variable i from temp. Commented Sep 9, 2022 at 17:26
  • The equivalent bracket syntax would be: newObj["oldObj"][i] Commented Sep 9, 2022 at 17:27

2 Answers 2

1

You might want to reconsider your data structure. It should be an array of objects.

eg: [{name:"Mohammad",age:"29"}, {name:"John",age:"19"}]

However, the code would look like

const swap = Object.entries(obj).reduce((accum, [key, value]) => ({...accum, ...{ [value]: key } }), {})

Why your code didn't work?

javascript computed properties

you tried with, newObj.obj[i] = i

instead it should be newObj[obj[i]] = i

You need to resolve the value for [obj[i]]

in this case, newObj[obj[i]] becomes 29. but newObj.obj becomes newObj.29 which'll be undefined at this point.

rewriting your function would look like,

/* {name:"Mohammad",age:"29"} ==>>  {Mohammad:"name",29:"age"} */

const obj = {name:"Mohammad",age:"29"}

function objValue (obj){
        const newObj = {}
    
    for (const [key, value] of Object.entries(obj)) {
     newObj[value] = key
    }
    
    return newObj
}

console.log(objValue(obj))
Sign up to request clarification or add additional context in comments.

1 Comment

in reference we see for access object's key has two way: object["keyName"] or object.keyName. and if key name does not exist, create at this time. now, in my code why newObj[oldObj[i]] create key for newObj and newObj.oldObj[i] not create key and try ONLY for access, not create it.
0

You need to think about the logic first, If I needed to solve this I will make the object into two arrays and then convert them back to object by swapping there places like below:

const object = {
    name:"Mohammad",
    age:"29"
};

function swap(obj) {
    const key = Object.keys(obj);
    const value = Object.values(obj);
    let result = {};
    value.forEach((element, index) => {
        result[element] = key[index];
    });
    return result;
}

console.log(swap(object));

1 Comment

I got a one-liner equivalent for you: Object.fromEntries(Object.entries(obj).map(([a, b]) => [b, a])).

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.