gpt4 book ai didi

用于在不复制键的情况下创建 JSON 对象的 JavaScript 循环

转载 作者:行者123 更新时间:2023-11-30 14:09:53 27 4
gpt4 key购买 nike

需要一些关于如何通过遍历数组而不复制某些键来创建 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;
};

但这会返回:

[
{
"name": {
"ServiceV1": {
"20190201": {
"1": "index.html"
}
}
}
},
{
"name": {
"ServiceV2": {
"20190201": {
"8": "index.html"
}
}
}
},
{
"name": {
"ServiceV2": {
"20190201": {
"9": "index.html"
}
}
}
},
{
"name": {
"ServiceV2": {
"20190201": {
"17": "index.html"
}
}
}
}
]

所以我有点迷失了下一步该去哪里

最佳答案

Stephen,你正在创建新对象并将它们推到数组的末尾,这总是会导致列表变得越来越长。

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

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

我在此示例中使用递归,因为它非常适合。

// WARNING: This code assumes a very specific text structure.
// It's for a specific use case, not a generic solution. Details in comments below.
const result = {}; // References are immutable in JS' const, not values.
const texts = [
'a/b/c/file1.html',
'b/c/d/file2.html',
'a/b/e/file3.html'
];

function gluePartsToObject(obj, parts) {
// End of the line.
if (parts.length === 1) return parts.shift();

// We've still got some ways to go.
const part = parts.shift();
if (obj.hasOwnProperty(part)) {
// Re-use object reference.
obj[part] = gluePartsToObject(obj[part], parts);
} else {
// Don't have an object yet to reference, create one.
obj[part] = gluePartsToObject({}, parts);
}

return obj;
}

// ES2015 "of". Can be replaced with a regular loop for compatibility.
for (text of texts) {
let parts = text.split('/');
gluePartsToObject(result, parts);
}

console.log(result);

关于用于在不复制键的情况下创建 JSON 对象的 JavaScript 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54674908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com