0

I've been searching an answer for that but didn't found it.

I have an array like:

const data2 = [{
    "abc":{
            companyCity:"Cupertino",
            conpanyName:"Apple"
        }
    },
    {
    "def":{
            companyCity:"Mountain View",
            conpanyName:"Google"
        }
    }
]  

And I'd like to convert to and array like omiting the parent keys:

const data3 = [
    {
        companyCity:"Cupertino",
        companyName:"Apple",
    },
    {
        companyCity:"Mountain View",
        companyName:"Google"
    }
]

Perhaps, libraries like lodash have a method to achieve that, but didn't find it. Any help would be very appreciated :)

1
  • There is no JSON in your question, the corresponding tag probably should be removed. Commented Jan 23, 2020 at 22:48

1 Answer 1

2

Iterate the array with Array.flatMap() (or lodash's _.flatMap()), and get the an the inner object of each item using Object.values() (or _.values()):

const data = [{"abc":{"companyCity":"Cupertino","conpanyName":"Apple"}},{"def":{"companyCity":"Mountain View","conpanyName":"Google"}}]

const result = data.flatMap(Object.values)

console.log(result)

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

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.