【问题标题】:Add object contents in one object array within objects in another array在另一个数组中的对象中添加一个对象数组中的对象内容
【发布时间】: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) =&gt; w.info = meta[ix].info)
  • @Keith 会改变原始对象。
  • @CertainPerformance 是的,我知道。 -> the second file is added to the corresponding information from the first file
  • 两个数组的顺序是相同的索引0 from A 相同索引0 in B?

标签: javascript object merge


【解决方案1】:

循环遍历 metas 数组并使用 Object.assign 将 meta 添加到第一个数组中的相应对象:

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】:

    如果每个数组中的每个元素都对应另一个数组中具有相同索引的另一个元素,那么它就是一个简单的.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);

    【讨论】:

    • 我喜欢你的回答,我仍然习惯于使用 Map(),你能否使用你的地图功能来比较术语与单词的属性并查找字符串相等性,这样你就不必依赖索引是否相同?
    • 易于调整,只需使用.find 来查找另一个对象
    • 感谢您的 sn-p。仍在尝试习惯 javascript 中的 llambda 语法,更多地在 C# 中使用它,所以我开始能够很好地阅读它。不知道你可以做一个像这样将对象连接在一起的返回语句,+1 先生。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        另一种方法是使用函数reduce + 函数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; }

        【讨论】:

          猜你喜欢
          • 2021-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-13
          • 2020-05-24
          • 1970-01-01
          • 1970-01-01
          • 2021-09-07
          相关资源
          最近更新 更多