如何将特定键合并到另一个对象关于公共键?

我有一个有很多记录的数组。需要根据条件合并对象(不适用于数组内所有可用的对象),这种情况下首先需要将数据与名称进行比较。

第 1 步:需要在数组中查找具有相似名称的对象。

步骤2:如果我们有相同的数据,接下来比较匹配数据中的状态键。

步骤3:

case1:如果匹配数据中的任何一个对象的状态为true(设置状态:true),则需要合并该对象的所有喜好键。 case2:如果匹配数据中的所有对象的状态都为false,则(设置状态:false)需要将所有喜欢的键合并到任何对象。

如何做到这一点?


大批


[

  {

    name: 'jane',

    age: 10,

    status: true,

    likings: [{ sports: 'football', books: 'harrypotter' }],

  },

  {

    name: 'sam',

    age: 20,

    status: false,

    likings: [{ sports: 'basketball', books: 'book1' }],

  },

  {

    name: 'jane',

    age: 10,

    status: false,

    likings: [{ sports: 'chess', books: 'book2' }],

  },

  {

    name: 'robert',

    age: 40,

    status: false,

    likings: [{ sports: 'carrom', books: 'book3' }],

  },

  {

    name: 'jane',

    age: 10,

    status: false,

    likings: [{ sports: 'gaming', books: 'book4' }],

  },

];



预期 o/p


[

  {

    name: 'jane',

    age: 10,

    status: true,

    likings: [

      { sports: 'football', books: 'harrypotter' },

      { sports: 'gaming', books: 'book4' },

      { sports: 'chess', books: 'book2' },

    ],

  },

  {

    name: 'sam',

    age: 20,

    status: false,

    likings: [{ sports: 'basketball', books: 'book1' }],

  },

  {

    name: 'robert',

    age: 40,

    status: false,

    likings: [{ sports: 'carrom', books: 'book3' }],

  },

];


慕神8447489
浏览 81回答 2
2回答

蓝山帝景

您可以使用.reduce()将所有具有相同名称的对象合并到一个Map. 这可以通过保留name具有该名称的对象的键和值的映射。每当您在数组中遇到新对象时,您都可以检查它是否存在于地图中。如果是这样,您可以添加到likings存储在关联对象中的数组中。true如果当前对象为,您还可以将状态更新为true。如果映射中不存在对象的名称作为键,您可以将对象添加为值,进一步的后续迭代reduce可以合并到该值中。请参见下面的示例:const arr = [ { name: 'jane', age: 10, status: true, likings: [{ sports: 'football', books: 'harrypotter' }], }, { name: 'sam', age: 20, status: false, likings: [{ sports: 'basketball', books: 'book1' }], }, { name: 'jane', age: 10, status: false, likings: [{ sports: 'chess', books: 'book2' }], }, { name: 'robert', age: 40, status: false, likings: [{ sports: 'carrom', books: 'book3' }], }, { name: 'jane', age: 10, status: false, likings: [{ sports: 'gaming', books: 'book4' }], }, { name: 'sam', age: 10, status: false, likings: [{ sports: 'gaming', books: 'book5' }], }];const merged = [...arr.reduce((m, o) => {  const curr = m.get(o.name) || {};  return m.set(o.name, {...o, status: curr.status || o.status, likings: [...(curr && curr.likings || []), ...o.likings]});}, new Map).values()]console.log(merged);

烙印99

我认为我没有完全理解您的要求,尤其是关于“相似名称”的要求。因此,我假设您要根据“名称”对记录进行分组,并且所有具有相同“名称”的记录都将具有相同的“年龄”。下面的解决方案是使用一个对象对记录进行分组,hash并继续将它们连接likings到元素中。完成后,通过调用返回对象的所有元素,Object.values()应该保持名称出现的顺序。这是你想要的,或者至少给你一些想法?希望能帮助到你。function merge(records) {&nbsp; const hash = {};&nbsp; for (let i = 0; i < records.length; i++) {&nbsp; &nbsp; const element = records[i];&nbsp; &nbsp; const key = element.name; // if you want to do something for "similarity", do something here.&nbsp; &nbsp; hash[key] = {&nbsp; &nbsp; &nbsp; ...element,&nbsp; &nbsp; &nbsp; status: (hash[key] && hash[key].status) || element.status,&nbsp; &nbsp; &nbsp; likings: element.likings.concat((hash[key] && hash[key].likings) || []),&nbsp; &nbsp; };&nbsp; }&nbsp; return Object.values(hash);}const data = [&nbsp; {&nbsp; &nbsp; name: "jane",&nbsp; &nbsp; age: 10,&nbsp; &nbsp; status: true,&nbsp; &nbsp; likings: [{ sports: "football", books: "harrypotter" }],&nbsp; },&nbsp; {&nbsp; &nbsp; name: "sam",&nbsp; &nbsp; age: 20,&nbsp; &nbsp; status: false,&nbsp; &nbsp; likings: [{ sports: "basketball", books: "book1" }],&nbsp; },&nbsp; {&nbsp; &nbsp; name: "jane",&nbsp; &nbsp; age: 10,&nbsp; &nbsp; status: false,&nbsp; &nbsp; likings: [{ sports: "chess", books: "book2" }],&nbsp; },&nbsp; {&nbsp; &nbsp; name: "robert",&nbsp; &nbsp; age: 40,&nbsp; &nbsp; status: false,&nbsp; &nbsp; likings: [{ sports: "carrom", books: "book3" }],&nbsp; },&nbsp; {&nbsp; &nbsp; name: "jane",&nbsp; &nbsp; age: 10,&nbsp; &nbsp; status: false,&nbsp; &nbsp; likings: [{ sports: "gaming", books: "book4" }],&nbsp; },];console.log(merge(data));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript