gpt4 book ai didi

javascript - Jquery 不扩展外部 json 文件

转载 作者:行者123 更新时间:2023-11-30 16:14:18 25 4
gpt4 key购买 nike

<分区>

下面的函数看起来是正确的,但它运行不正确

$.when(
$.getJSON('compare/As_edit.json'), $.getJSON('compare/As_old.json'))
.then(function (a,b) {
//return $.extend(a, b);
console.log($.extend(a, b));
})

在控制台日志中我看到:

Object {text: "As", icon: "icons/tree.png", children: Array[1]}

虽然它应该是“children: Array 2

我的文件看起来像:

文件1

{
"text": "As",
"icon": "icons/tree.png",
"children": [
{
"text": "Class1",
"children": [
{
"text": "Intern1",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
},
{
"text": "Intern2",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
}
]
}
]
}

文件2

{
"text": "As",
"icon": "icons/tree.png",
"children": [
{
"text": "Class2",
"children": [
{
"text": "Intern3",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
},
{
"text": "Intern4",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
}
]
}
]
}

在输出中我想看到

{
"text": "As",
"icon": "icons/tree.png",
"children": [
{
"text": "Class1",
"children": [
{
"text": "Intern1",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
},
{
"text": "Intern2",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
}
]
},
{
"text": "Class2",
"children": [
{
"text": "Intern3",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
},
{
"text": "Intern4",
"-ORDER": "2",
"children": [
{
"name": "--TRT",
"text": "Name of Intern"
}
]
}
]
}
]
}

但我只从 file2 中的对象获取输出怎么了?

这是一个fiddle例子

在对 Merge 2 arrays of objects 进行检查后,我已经使用了下一个代码

 $.when(
$.getJSON('compare/Astella_edit.json'), $.getJSON('compare/Astella_old.json'))
.then(function (a,b) {
var arr3 = [];
for (var i in a) {
var shared = false;
for (var j in b)
if (b[j].children == a[i].children) {
console.log('['+ (b[j].children == a[i].children) +']');
shared = true;
break;
}
if (!shared) arr3.push(a[i])
}
arr3 = arr3.concat(b);
console.log(arr3);
//return arr3;
})

它合并到我几乎是正确的

[Object, Object, "success", Object]

所以第一个对象是file1,第二个是file2

生成的 JSON 中的“成功”是什么意思,第三个对象来自哪里?

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