1
  1. Here is the task
  • @param {object} ???
  • @returns {object} an object with the given object's values as keys, and keys as values. If there are duplicate values in the input object, only the first key-value pair should be used and subsequent pairs with the same value discarded.

2.Here is what i did code

What's wrong with it? How should i rework?

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };

function swapPairs2(obj){                                    
    let newObject = {};
    for(const key in obj){
        let value = obj[key]
        newObject[value] = key;
    }
    return newObject;
} 

3.Here are the tests

test(swapPairs2(object5), { 1: "a", 2: "b", 3: "c" });
test(swapPairs2(object6), { 1: "a" });

4.Here is the error i got

I am supposed to get the first key-value pair, but a random return.

enter image description here

honknoodle
  • 108
  • 9
jessieWJ
  • 53
  • 6

3 Answers3

2

You can also reverse the entries.

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };
const swapPairs2 = obj => Object.fromEntries(
  Object.entries(obj)
    .reverse() // Keep the first, not the last
    .map(entry => [entry[1], entry[0]]) // Reverse the key and vlaue
);
console.log(swapPairs2(object5));
console.log(swapPairs2(object6));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

Only add the value/key pairs, if the value doesn't already exist on newObject by checking with the in operator:

function swapPairs2(obj) {
  const newObject = {};
  for (const key in obj) {
    const value = obj[key];
    if(!(value in newObject)) {
      newObject[value] = key;
    }
  }
  return newObject;
}

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };

console.log(swapPairs2(object5));
console.log(swapPairs2(object6));

Another option is to use Array.reduce():

const swapPairs2 = obj =>
  Object.entries(obj)
    .reduce((acc, [k, v]) => 
      v in acc 
      ? acc
      : { ...acc, [v]: k }
    , {})

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };

console.log(swapPairs2(object5));
console.log(swapPairs2(object6));
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
  • 1
    Hi, thank you so much for your tips. What I'm expecting are the ones showing in the tests, which are {a:1, b:2, c:3}, {a:1} should reflect in console. – jessieWJ Apr 18 '22 at 15:04
  • 2
    Look back at your question - it says `{ 1: "a", 2: "b", 3: "c" }` and `{ 1: "a" }`. – Ori Drori Apr 18 '22 at 15:07
  • 1
    Yes, sorry for my typo. Should be { 1: "a", 2: "b", 3: "c" } and { 1: "a" } – jessieWJ Apr 18 '22 at 15:24
1

You can do it by taking the object keys and using the Array.reduce() method on the keys.

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };

const swapPairs2 = (obj) => Object.keys(obj)
  .reduce((a, c) => (a[obj[c]] = a[obj[c]] ?? c, a), {});

console.log(swapPairs2(object5));
console.log(swapPairs2(object6));

Or using the Array.reduce() method on the Object.entries()

const object5 = { a: 1, b: 2, c: 3, d: 1 };
const object6 = { a: 1, b: 1, c: 1, d: 1 };

const swapPairs2 = obj => Object.entries(obj)
    .reduce((acc, [key, val]) => (acc[val] = acc[val] ?? key, acc), {})

console.log(swapPairs2(object5));
console.log(swapPairs2(object6));
Steve
  • 878
  • 1
  • 5
  • 9