在另一个数组中的对象内的一个对象数组中添加对象内容

时间:2018-04-23 23:27:36

标签: javascript object merge

我有两个包含对象数组的大文件,第一个包含这样的数据:

[{
    "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()?

5 个答案:

答案 0 :(得分:2)

如果每个数组中的每个元素对应于另一个数组中具有相同索引的另一个元素,那么它是一个简单的.map,它比for循环更合适:< / p>

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:2)

循环遍历metas数组并使用Custom Nav in Wordpress not Loading Pages将元数据添加到第一个数组中的相应对象:

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)

答案 2 :(得分:1)

如果阵列没有对齐,您可以使用.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);

答案 3 :(得分:0)

只需从第一个数组中设置对象的新属性即可。

 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]} );
 }

答案 4 :(得分:0)

另一种方法是使用函数reduce +函数map

函数reduce将第二个数组input2转换为一个对象,其中键来自属性meta.term,这样函数map使用该对象来查找非常通过密钥而不是重复的find执行来加快相应的元值。

此方法与订单无关,因为它会匹配属性word和属性meta.term

&#13;
&#13;
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);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;