1

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.

1
  • 1
    console.log(Object.values(array)) Commented Jan 4, 2021 at 5:42

2 Answers 2

8

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));

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

Comments

1

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

Comments

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.