【发布时间】:2018-10-04 02:26:22
【问题描述】:
我有两个包含对象数组的大文件,第一个包含这样的数据:
[{
"id": "001",
"word": "abbess",
"def": "(noun) The lady superior of a nunnery",
}, {
"id": "002"
"word": "abbey",
"def": "(noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns.",
}, (etc...)
第二个,数据如下:
[{
"meta": {
"term": "abbess",
"part_of_speech": "noun",
"definition": "The lady superior of a nunnery"
}
}, {
"meta": {
"term": "abbey",
"part_of_speech": "noun",
"definition": "The group of buildings which collectively form the dwelling-place of a society of monks or nuns"
}
}, (etc...)
我想将这两个文件合并,以便将第二个文件中的“元”信息添加到第一个文件中的相应信息中,所以:
[{
"id": "001",
"word": "abbess",
"def": "(noun) The lady superior of a nunnery",
"meta": {
"term": "abbess",
"part_of_speech": "noun",
"definition": "The lady superior of a nunnery"
}
}, {
"id": "002"
"word": "abbey - (noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns.",
"def": "(noun) The group of buildings which collectively form the dwelling-place of a society of monks or nuns.",
"meta": {
"term": "abbey",
"part_of_speech": "noun",
"definition": "The group of buildings which collectively form the dwelling-place of a society of monks or nuns"
}
}, (etc...)
现在,我有这个代码
var newArr = [];
for(var i = 0; i < meta.length; i++) {
newArr.push(words[i]);
newArr.push(meta[i]);
}
在单词对象之后添加元对象,而不是在里面。我是否需要向下循环另一层以在单词对象中添加元对象,或者是否有其他方法可以在这里更好地工作,例如 .concat()?
【问题讨论】:
-
两个数组排成一行吗?换句话说,
meta[0]是否总是与data[0]、meta[1]与data[1]等一起使用? -
words.forEach((w, ix) => w.info = meta[ix].info) -
@Keith 会改变原始对象。
-
@CertainPerformance 是的,我知道。 ->
the second file is added to the corresponding information from the first file -
两个数组的顺序是相同的索引
0from A 相同索引0inB?
标签: javascript object merge