0

My task is to write a function that swaps the value of an element with the value at the same location within the second object.

{placeOne:10,placeTwo:20},{ten:"firstPlace",twenty:"secondPlace"}   

{placeOne:"firstPlace",placeTwo:"secondPlace"},{ten:10,twenty:20} // should equal this

I wanted to try an approach that pushed the objects value into an array, then loop through the object and set each position to the position within the array.

But I had trouble looping through the object and the array at the same time so I could not solve it that way.

Heres what I have so far.

function swapObj(obj1,obj2){
  let obj1Arr = [];
  let obj2Arr = [];

  for(var i in obj1) {
    obj1Arr.push(obj1[i]);
  }

  for(var k in obj2) {
    obj2Arr.push(obj2[k])
  }

swapObj({placeOne:10,placeTwo:20,placeThree:30,}, 
        {ten:"firstPlace",twenty:"secondPlace",thirty:"thirdPlace"}
)
3
  • what is the problem? you don't start with an array so what do you mean by swap? your code just places those 2 objects into 2 arrays inside swapObj Commented Apr 9, 2018 at 21:27
  • 2
    the order of keys in an object is arbitrary, so you might create {ten:10,twenty:20} but may also create {ten:20,twenty:10} (see: stackoverflow.com/questions/5525795/…) Commented Apr 9, 2018 at 21:28
  • @user3080953 looks like the question is not asking about swapping keys inside an object, but the objects themselves inside an array, although it is ambiguous. Commented Apr 9, 2018 at 21:31

1 Answer 1

1

If I understood your problem correctly, this should do it (each step explained with a comment):

const swapValues = (a, b) => {
    // obtain arrays of entries ([key, value] pairs) of input objects
    // assuming the entries come in insertion order,
    // which is true in practice for all major JS engines
    const entriesA = Object.entries(a)
    const entriesB = Object.entries(b)

    // output is a pair of objects:
    // first with keys from a, but values from b
    //      at corresponding entry indices
    // second with keys from b, but values from a
    //      at corresponding entry indices
    // assuming both objects have the same number of entries
    //      (might want to check that)
    return entriesA.reduce(
        // for each entry from a with keyA, valueA and index
        (acc, [keyA, valueA], index) => {
            // get corresponding entry from b
            const entryB = entriesB[index]
            // with keyB and valueB
            const [keyB, valueB] = entryB
            // put valueB at keyA in the first output object
            acc[0][keyA] = valueB
            // put valueA at keyB in the second output object
            acc[1][keyB] = valueA

            return acc
        },
        // initially the output objects are empty:
        [{}, {}]
    )
}

console.log(swapValues(
    {placeOne: 10, placeTwo: 20},
    {ten: "a", twenty: "b"}
)) // -> [ { placeOne: 'a', placeTwo: 'b' }, { ten: 10, twenty: 20 } ]

You may want to adapt this to your version of JS. Note that no mutation of the input objects is taking place -- you get two brand new objects (may share some structure with your input objects if they have nested objects as values).

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

2 Comments

what do [ ] the brackets do around keyA and valueA in the reduce? Is that to put keyA and valueA into an array?
They destructure[0] the second argument of the reduce's callback. You could write: (acc, entry, index) => { const keyA = entry[0]; const valueA = entry[1]; /* ... */ } and you'd get the same thing. [0] developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

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.