[英]Add object contents in one object array within objects in another array
我有两个包含对象数组的大文件,第一个包含这样的数据:
[{
"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...)
我想组合这两个文件,以便将第二个文件中的“meta”信息添加到第一个文件中的相应信息中,这样:
[{
"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()?
如果每个数组中的每个元素对应于另一个数组中具有相同索引的另一个元素,则它是一个简单的.map
,它比for
循环更合适:
const input1 = [{ "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.", }]; const input2 = [{ "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" } }]; const combined = input1.map((item) => { const { word } = item ; const foundInput2 = input2.find(({ meta: { term }}) => term === word); const { meta } = foundInput2; return { ...item, meta }; }); console.log(combined);
循环遍历元数组并使用Object.assign将元添加到第一个数组中的相应对象:
var arr = [{ "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.", }] const arr2 = [{ "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" } }] arr2.forEach((e, i) => { Object.assign(arr[i], e); }); console.log(arr)
如果阵列没有.find
,您可以使用.map
和.find
来实现目标。
const input1 = [{ "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.", }]; const input2 = [{ "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" } }]; const output = input1.map(item => { return { ...item, ...input2.find(item2 => item2.meta.term === item.word) } }); console.log(output);
只需从第一个数组中设置对象的新属性即可。
var newArr = [];
for(var i = 0; i < meta.length; i++) {
var word = words[i];
word.meta = meta[i].meta;
newArr.push(word);
}
这假设两个数组总是以相同的顺序具有相同单词的信息。
奖金提示 - 如果您使用ECMAScript 6,您可以连接这样的对象:
const newArr = [];
for(let i = 0; i < meta.length; i++) {
newArr.push({ ...words[i], ...meta[i]} );
}
另一种方法是使用函数reduce
+ function map
函数reduce
将第二个数组input2
转换为一个对象,其中键来自属性meta.term
,这样函数map
使用该对象通过键非常快速地找到相应的元值而不是重复的find
执行。
此方法独立于订单工作,因为它将匹配属性word
和属性meta.term
。
const input1 = [{ "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.",}], input2 = [{ "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" }}], mapped = input2.reduce((a, o) => Object.assign(a, {[o.meta.term]: o.meta}), {}), result = input1.map((o) => Object.assign({}, o, {meta: mapped[o.word]})); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.