Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于创建JSON对象而不复制键的JavaScript循环_Javascript - Fatal编程技术网

用于创建JSON对象而不复制键的JavaScript循环

用于创建JSON对象而不复制键的JavaScript循环,javascript,Javascript,需要一些关于如何通过循环数组而不复制某些键来创建JSON对象的建议吗 我得到了一个数组,需要将其拆分为一个对象,但不复制其中一个键 例如: var myArray = [ "name/ServiceV1/20190201/1/index.html", "name/ServiceV2/20190201/1/index.html", "name/ServiceV2/20190201/2/index.html", "name/ServiceV2/20190201/3/i

需要一些关于如何通过循环数组而不复制某些键来创建JSON对象的建议吗

我得到了一个数组,需要将其拆分为一个对象,但不复制其中一个键

例如:

var myArray = [
    "name/ServiceV1/20190201/1/index.html",
    "name/ServiceV2/20190201/1/index.html",
    "name/ServiceV2/20190201/2/index.html",
    "name/ServiceV2/20190201/3/index.html",
    "name/ServiceV2/20190203/3/index.html",
    "name/ServiceV3/20190213/1/index.html"
];
返回

[
    {
        "name": {
            "ServiceV1": {
                "20190201": {
                    "1": "index.html"
                }
            },
            "ServiceV2": {
                "20190201": {
                    "1": "index.html",
                    "2": "index.html",
                    "3": "index.html"
                },
                "20190203": {
                    "1": "index.html"
                },
            },
            "ServiceV3": {
                "20190213": {
                    "1": "index.html"
                },
            }
        }
    }
]

我怎样才能让它工作呢?下面的代码是我已经知道的

var jsonify = function() {
  var myArray = [
    "name/ServiceV1/20190201/1/index.html",
    "name/ServiceV2/20190201/1/index.html",
    "name/ServiceV2/20190201/2/index.html",
    "name/ServiceV2/20190201/3/index.html",
    "name/ServiceV2/20190203/3/index.html",
    "name/ServiceV3/20190213/1/index.html"
  ];
  let end = [];

  // Loop through all the myArray items
  for (let i = 0; i < myArray.length; i++) {
    var itemparts = myArray[i].split("/");

    var newObject = {};
    var value = itemparts.pop();
    while (itemparts.length) {
      var obj = {};
      if (newObject.hasOwnProperty(itemparts.pop())) {
        return;
      } else {
        newObject[itemparts.pop()] = value;
      }
      obj[itemparts.pop()] = value;
      value = obj;
    }
    end.push(value);
  }

  // return the results
  return end;
};

所以我有点不知道下一步要去哪里,Stephen,你正在创建新的对象并将它们推到数组的末尾,这总是会导致列表越来越长

您最初的措辞已经暗示了问题所在:“如何创建JSON对象”

不要创建要添加到列表中的新对象,而是只使用一个修改/更新的对象。请记住,对象是JavaScript中的引用

我在这个例子中使用递归,因为它非常适合

//警告:此代码采用非常特定的文本结构。
//它针对特定的用例,而不是通用的解决方案。详情见下文评论。
常量结果={};//引用在JS的常量中是不可变的,而不是值。
常量文本=[
“a/b/c/file1.html”,
“b/c/d/file2.html”,
“a/b/e/file3.html”
];
功能部件对象(对象、部件){
//这条线的尽头。
如果(parts.length==1)返回parts.shift();
//我们还有一些路要走。
const part=parts.shift();
if(对象拥有自己的财产(部分)){
//重复使用对象引用。
obj[零件]=胶接零件对象(obj[零件],零件);
}否则{
//如果还没有要引用的对象,请创建一个。
obj[part]=GluePartsObject({},parts);
}
返回obj;
}
//ES2015“of”。可替换为常规循环以实现兼容性。
用于(文本的文本){
让parts=text.split('/');
粘合零件对象(结果,零件);
}

控制台日志(结果)
这个答案的可能副本是使用lodash,另一个答案是使用硬编码密钥,而我从数组中分割的密钥太棒了!它的工作方式正是我需要它太谢谢你,谢谢你的评论代码:)
[
  {
    "name": {
      "ServiceV1": {
        "20190201": {
          "1": "index.html"
        }
      }
    }
  },
  {
    "name": {
      "ServiceV2": {
        "20190201": {
          "8": "index.html"
        }
      }
    }
  },
  {
    "name": {
      "ServiceV2": {
        "20190201": {
          "9": "index.html"
        }
      }
    }
  },
  {
    "name": {
      "ServiceV2": {
        "20190201": {
          "17": "index.html"
        }
      }
    }
  }
]