0

This is what I am working with:

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}

I have been trying to compute my output result to look like this

result = [ 
    {"a": {"1": "2", "3": "4"}},
    {"b": {"3": "4", "5": "6"}},
    {"c": {"7": "8", "9": "10"}}
    {"d": {"11": "12", "13": "14"}}
]

I have tried the following which is closer but not the same as what I am looking for: Object.entries(object).map(([key, value]) => { return value })

6
  • 2
    Shared object is not valid object Commented Feb 18, 2022 at 13:28
  • 1
    Where is the key for the first top-level object Commented Feb 18, 2022 at 13:29
  • 1
    Where are you getting the initial "object" from? Commented Feb 18, 2022 at 13:31
  • Sorry. You are right. The object had extra brackets that I mean to add. I corrected it now. Commented Feb 18, 2022 at 13:38
  • 1
    You cant use map, because you cant return an array with more entries than you originally had. I would advice to create a function using normal loop to create a new object. Do you really need a one liner response? if so i can show you another approach Commented Feb 18, 2022 at 13:45

3 Answers 3

1

Can be done using 2 forEach

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}



let res = []
Object.values(object).forEach((value) => {
    Object.entries(value).forEach((el) => {
    res.push({[el[0]]: el[1]})
  })
})


console.log(res)

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

5 Comments

This is pretty awesome of a solution.
@mothfly_dev i thought this is the most boring solution. its like running 2 loops
a variation on this let res = Object.values(object).flatMap(Object.entries).map(([k, v]) => ({ [k]: v }))
lol. What would an less boring solution use? I was trying to use recursion but I am too daft( also too daft to come up with a solution like you did with two forEach)
Thomas. Ingenious!
1

Try this

let object = {'A':{"a": {"1": "2", "3": "4"},"b": {"3": "4", "5": "6"}},'B':{"c": {"7": "8", "9": "10"},"d": {"11": "12", "13": "14"}}};
let arr = Object.entries(Object.assign({}, ...Object.values(object))).map(([k, v]) => ({[k]: v}));
console.log(arr)

Comments

0

Try this:

Object.values(object)
    .flatMap(a => Object.entries(a))
    .map(([k, v]) => ({ [k]: v }))

Edited to use the last map idea from @Thomas instead of using Object.fromEntries

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.