This website requires JavaScript.
原创

JS之将JSON数据结构里的所有值初始化

2020.07.06 21:14 

0 人喜欢
767 次阅读
0 条评论
var data = {
  str: "123123",
  num: 111,
  strAry: ["isis", "wddd", "23sdfsdfs", "wclgdj"],
  isOn: true,
  isNb: false,
  isC: true,
  daa: {
    num: 12312,
    ary: [
      {
        nb: 1,
        wc: ["nb", "xdnb", "兄弟牛逼"],
        nbb: {
          strr: "nbnbnb",
        },
      },
    ],
  },
};
function verifType(obj) {
  if (obj.constructor === Object) return "Object";
  if (obj.constructor === Array) return "Array";
  if (obj.constructor === Number) return "Number";
  if (obj.constructor === String) return "String";
  if (obj.constructor === Boolean) return "Boolean";
}

function k(j) {
  const keys = Object.keys(j);
  keys.forEach((e) => {
    let v = j[e];
    if (verifType(v) === "String") j[e] = "";
    if (verifType(v) === "Number") j[e] = 0;
    if (verifType(v) === "Boolean") j[e] = true;
    if (verifType(v) === "Array") {
      if (verifType(v[0]) === "Object") j[e] = [k(v[0])];
      else j[e] = [];
    }
    if (verifType(v) === "Object") j[e] = k(v);
  });
  return j;
}
k(data);
console.log(data);

  • 😃
  • 😂
  • 😅
  • 😉
  • 😌
  • 😔
  • 😓
  • 😘
  • 😡
  • 😭
  • 😱
  • 😳
  • 😵
  • 🌚
  • 👍
  • 👎
  • 💪
  • 🌹
  • 💊
  • 🇨🇳
  • 🇺🇸