如何拆分 javascript 中的字典?

How to split a dictionary in javascript?

我想知道是否有一个简单的方法,一个内置函数来拆分 javascript 中的字典。

例如:

我的词典是:

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]}

我希望它是:

 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]}

我可以通过循环来完成,但我想知道是否有更快的方法或内置功能,我进行了搜索,但没有找到任何可以做到这一点的东西。

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

使用Object.entries将其转换为数组。然后用splice来分。然后使用 Object.fromEntries() 将数组转换回对象。

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]
}

const end = Object.entries(data);
const start = end.splice(0, 2); // Take two entries off the front of the array
const results = [start, end].map(Object.fromEntries);

console.log(results);