Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

先声明一个长度12,元素都是null 的数据,再循环obj 的key #565

Open
AwangXuan opened this issue Mar 9, 2022 · 1 comment

Comments

@AwangXuan
Copy link

let sele = { 1: 222, 2: 123, 5: 888 }
function tansformSele(obj) {
let res = [null, null, null, null, null, null, null, null, null, null, null, null]
for (const [key, value] of Object.entries(obj)) {
res[key - 1] = value
}
return res
}
sele = tansformSele(sele)
console.log("结果:", sele); // (12) [222, 123, null, null, 888, null, null, null, null, null, null, null]

@Stephen-Kai
Copy link

let sele = { 1: 222, 2: 123, 5: 888 };
function tansformSele(obj) {
  obj.length = 13;
  let res = Array.from(obj).map((d) => {
    if (!d) {
      return null;
    } else {
      return d;
    }
  });
  res.shift();
  return res;
}
sele = tansformSele(sele);
console.log("结果:", sele); // (12) [222, 123, null, null, 888, null, null, null, null, null, null, null]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants