0
let response = {
    data1: {
       names: ["John", "jim"]
    },
    data2: {
       ages: [34, 24]
    }
}

I want:

data: {
   names: ["John", "jim"],
   ages: [34,24]
}

Is there a way to merge these into 1 object without referencing response.data1 and response.data2? I know I can do const newObj = {...response.data1, ...response.data2} or Object.assign({}, response.data1, response.data1)... Can I do via a for in loop?

0

2 Answers 2

2

You could merge the values of the object using Object.assign() like this:

Object.assign({}, ...Object.values(response))

Explanation:

This is the syntax for Object.assign: Object.assign(target, source1, source2, ... etc). It basically takes all the properties from the source objects and updates the target object

The Object.values() bit returns the value of the response object. In this case, it's an array of objects like this:

[
  { names: ["John", "jim"] }, 
  { ages: [34, 24] }
]

You can spread the array and pass individual object as a parameter to Object.assign()

Here's a snippet:

let response = {
  data1: {
    names: ["John", "jim"]
  },
  data2: {
    ages: [34, 24]
  }
}

const output = Object.assign({}, ...Object.values(response))

console.log(output)


If it's too confusing you could use reduce:

Object.values(response)
      .reduce((a, v) => ({ ...a, ...v }), {})

Or a simple loop:

const output = {}

for(const key in response)
  Object.assign(output, response[key])
Sign up to request clarification or add additional context in comments.

4 Comments

I believe the question makes note that they are aware of the Object.assign solution, but that they specifically want to try to accomplish it with a loop.
@AlexanderNied that's not what they meant. They are saying they don't want to use the property name data1 explicitly as it is dynamic and there could be unknown number of properties.
Ah sure, that's fair; that's probably the correct interpretation.
@AlexanderNied they said they were aware of an Object.assign solution. There is no such thing as the Object.assign solution.
1

If you want to merge data1 and data2 using a for in loop, try this:

let response = {
    data1: {
       names: ["John", "jim"]
    },
    data2: {
       ages: [34, 24]
    }
}
var result = {}
for (data in response) {
  if (response.hasOwnProperty(data)) {
    [key] = Object.keys(response[data])
    result[key] = response[data][key]
  }
}
console.log(result)

The way it works is by getting the keys of data and data2, then using those keys, it gets the values and adds it to result

2 Comments

Probably worth noting that, because for...in loops will go over all properties of an object, including inherited ones, one generally will want to include a .hasOwnProperty() check before proceeding with any processing.
I fixed it, is that better?

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.