Reputation: 85
I have an array which looks like this:
const array = {FSES: {empId: '322344BD', address:'North'}, DSER:{empId: '322344BD', address:'West'}}
I want to be able to get rid of FSES and DSER. This is my desired array:
const desiredArray = [{empId: '322344BD', address:'North'},{empId: '322344BD', address:'West'}]
This is what I have tried but it is not working.
const newArray = [].concat(...array.map(o => o.address))
I hope you can help me. Thanks in advance.
Upvotes: 1
Views: 81
Reputation: 7905
Here you go:
const array = {FSES: {empId: '322344BD', address:'North'}, DSER:{empId: '322344BD', address:'West'}}
const new_array = Object.keys(array).map(item => array[item])
console.log(new_array)
Loop through the original object's keys and then get those keys' respective values using map
Upvotes: 1
Reputation: 5054
Your code is not working because you are trying to map over an object. We can use .map() only with array.
You can simply use Object.values for that,
const array = {FSES: {empId: '322344BD', address:'North'}, DSER:{empId: '322344BD', address:'West'}}
console.log(Object.values(array));
Upvotes: 8