gpt4 book ai didi

javascript - underscore.js 如何合并 2 个集合

转载 作者:行者123 更新时间:2023-11-30 10:21:04 24 4
gpt4 key购买 nike

我有两个集合 A 和 B。(A、B 具有完全相同的结构,但节点值不同)

现在我想将 A 添加到 B 中,顺序完全相同:A B 并且不合并或更改任何它们里面的节点。 (就像联合行动一样){ A } + {B}

我已经阅读了下划线的文档,但找不到完成这项工作的合适函数。

有什么想法吗?

==========用例子更新========示例是从一个更大的结构简化而来的,如果有错误请告诉我=========

var collection1 = [{
"date": "29 January 2014",
"items": [{
"name": "Jack",
"score": 90,
"title": "programmer"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}, {
"date": "28 January 2014",
"items": [{
"name": "Jim",
"score": 30,
"title": "driver"
}, {
"name": "William",
"score": 52,
"title": "worker"
}]
}]



var collect2 = [{
"date": "26 January 2014",
"items": [{
"name": "Marry",
"score": 92,
"title": "teacher"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]

}]

========预期输出==============

[{
"date": "29 January 2014",
"items": [{
"name": "Jack",
"score": 90,
"title": "programmer"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]
}, {
"date": "28 January 2014",
"items": [{
"name": "Jim",
"score": 30,
"title": "driver"
}, {
"name": "William",
"score": 52,
"title": "worker"
}]

}, {

"date": "26 January 2014",
"items": [{
"name": "Marry",
"score": 92,
"title": "teacher"
}, {
"name": "TOM",
"score": 52,
"title": "designer"
}]


}]

最佳答案

我认为您正在寻找的只是 Array.concat

var foo = ['a','b','c'];
var bar = ['d','e','f'];

var all = foo.concat(bar); // ['a','b','c','d','e','f'];

关于javascript - underscore.js 如何合并 2 个集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21406589/

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