-1

Hi i have object of objects where i need to merge in to single object below is the object of objects :

{
  0: {
    CLIENTS_EMAIL: null
  }
  1: {
    CLIENTS_PASSWORD: null
  }
  2: {
    CLIENTS_CONFIRMPASSWORD: null
  }
  3: {
    CLIENTS_FIRSTNAME: null
  }
  4: {
    CLIENTS_LASTNAME: null
  }
  5: {
    CLIENTS_MOBILE: null
  }
  6: {
    CLIENTS_HOMEPHONE: null
  }
  7: {
    CLIENTS_BUSPHONE: null
  }
  8: {
    CLIENTS_RECEIVE_MARKETING_VIA: [
      0: {
        CLIENTS_ISMARKETINGVIAEMAIL: false
      }
      1: {
        CLIENTS_ISMARKETINGVIAMAIL: false
      }
      2: {
        CLIENTS_ISMARKETINGVIAPHONE: false
      }
      3: {
        CLIENTS_ISMARKETINGVIASMS: false
      }
    ]
  }
}

need this in to single object below is the example:

let data = {
  
CLIENTS_EMAIL: null,
CLIENTS_PASSWORD: null,
CLIENTS_CONFIRMPASSWORD: null,
CLIENTS_FIRSTNAME: null,
CLIENTS_LASTNAME: null,
CLIENTS_MOBILE: null,
CLIENTS_HOMEPHONE: null,
CLIENTS_BUSPHONE: null,
CLIENTS_ISMARKETINGVIAEMAIL: false,
CLIENTS_ISMARKETINGVIAMAIL: false,
CLIENTS_ISMARKETINGVIAPHONE: false,
CLIENTS_ISMARKETINGVIASMS: false,
  
}

Below is the logic i am trying :

const data = Object.entries(Object.entries(object).map((data: any) => {
  return data[1]
}))

Looked in to other other functions also but dint succeed

4
  • 1
    Does this answer your Question ? Commented Dec 7, 2020 at 11:01
  • Can you please provide a valid object? CLIENTS_RECEIVE_MARKETING_VIA: [ 0: { ... will throw a syntax error (and you are missing commas after your values) Commented Dec 7, 2020 at 11:02
  • @Codenewbie no tht is not what i need Commented Dec 7, 2020 at 11:22
  • @vinuta I was mainly talking about your input object Commented Dec 7, 2020 at 12:30

2 Answers 2

1

Here is Demo

Interesting recursion problem.

But the source data is a little weird. The array item is not a key-value object.

Should be like:

var a = {
    0: {
        CLIENTS_EMAIL: null
    },
    1: {
        CLIENTS_PASSWORD: null
    },
    2: {
        CLIENTS_CONFIRMPASSWORD: null
    },
    3: {
        CLIENTS_FIRSTNAME: null
    },
    4: {
        CLIENTS_LASTNAME: null
    },
    5: {
        CLIENTS_MOBILE: null
    },
    6: {
        CLIENTS_HOMEPHONE: null
    },
    7: {
        CLIENTS_BUSPHONE: null
    },
    8: {
        CLIENTS_RECEIVE_MARKETING_VIA: [
            {
                CLIENTS_ISMARKETINGVIAEMAIL: false
            },
            {
                CLIENTS_ISMARKETINGVIAMAIL: false
            },
            {
                CLIENTS_ISMARKETINGVIAPHONE: false
            },
            {
                CLIENTS_ISMARKETINGVIASMS: false
            }
        ]
    }
}

Then use recursion

let result = {};

let convert = (obj) => {
    for (var i in obj) {
        if (obj[i] != null && obj[i] !== false) {
            convert(obj[i]);
        } else {
            result[i] = obj[i];
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

codepen.io/jfw10973/pen/zYKBpaw?editors=0010 See if this demo could help you~
1

This function recursively merges an object of objects into a single object, but it's too expensive.
Time complexity is around O(n2log n)

(didn't test for other inputs tho, so let me know if it doesn't work with everything)

let result = {};
recMerge(ooo); /// --> 'ooo' is your (o)bject (o)f (o)bjects

function recMerge(obj) {
  Object.getOwnPropertyNames(obj).forEach((prop) => {
    const propName = obj[prop];

    Object.getOwnPropertyNames(propName).forEach((propValue) => {
      if (!!propName[propValue] && typeof propName[propValue] === "object") {
        recMerge(propName[propValue]);
      } else {
        result = { ...result, ...propName };
      }
    });
  });
}

Example

Output will always be the INNER MOST object in each iteration, spreaded into a single object.

CLIENTS_RECEIVE_MARKETING_VIA: {
  0: {
    CLIENTS_ISMARKETINGVIAEMAIL: false,
  },
  1: {
    CLIENTS_ISMARKETINGVIAMAIL: false,
  },
  2: {
    CLIENTS_ISMARKETINGVIAPHONE: false,
  },
  3: {
    CLIENTS_ISMARKETINGVIASMS: {
      0: {
        CLIENTS_ISMARKETINGVIATEXT: false,
      },
      1: {
        CLIENTS_ISMARKETINGVIAPIC: false,
      },
    },
  },
},

Will output

{
  CLIENTS_ISMARKETINGVIAEMAIL: false,
  CLIENTS_ISMARKETINGVIAMAIL: false,
  CLIENTS_ISMARKETINGVIAPHONE: false,
  CLIENTS_ISMARKETINGVIATEXT: false,
  CLIENTS_ISMARKETINGVIAPIC: false,
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.