Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split a dictionary in javascript?

I was wondering if there is a simple way, a built-in function to split a dictionary in javascript.

For example:

my dictionary is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

let data = {    "Is your salary above 1000$?":["yes","no",false,0],
              "Is your salary above 2000?":["yes","no",false,1],
              "Is your salary above 3000?":["yes","no",false,2],
              "Is your salary above 4000?":["yes","no",false,3],
              "Is your salary above 5000?":["yes","no",false,4]}

I want it to be:

 data1 = {    "Is your salary above 1000$?":["yes","no",false,0],
              "Is your salary above 2000?":["yes","no",false,1]}

 data2 = {     "Is your salary above 3000?":["yes","no",false,2],
              "Is your salary above 4000?":["yes","no",false,3],
              "Is your salary above 5000?":["yes","no",false,4]}

I can do it through looping but i was wondering if there is a faster way or a built in fuction, i did search but i didn’t find anything that does that.

>Solution :

const data = {
  "Is your salary above 1000$?": ["yes", "no", false, 0],
  "Is your salary above 2000$?": ["yes", "no", false, 1],
  "Is your salary above 3000$?": ["yes", "no", false, 2],
  "Is your salary above 4000$?": ["yes", "no", false, 3],
  "Is your salary above 5000$?": ["yes", "no", false, 4],
};

const sliceAt = 2;
const dataArr = Object.entries(data);
const obA = Object.fromEntries(dataArr.slice(0, sliceAt));
const obB = Object.fromEntries(dataArr.slice(sliceAt));

console.log(obA);
console.log(obB);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading