Home > Enterprise >  Merge same keys in one list values in dict
Merge same keys in one list values in dict

Time:11-07

I am building a blog app in react and I am filtering some dict values and appending in list dict. But It is appending duplicate keys with unique values in list dict. And I am know trying to merge the two same keys into one But I have tried many times but it is not working.

App.js

function App(response) {
  var filteredResponse = []

  response.forEach(function (element) {
    for (let i = 0; i < blogTypes.length; i  ) {
      if (element['type'] === blogTypes[i]) {
        filteredResponse.push({type: blogTypes[i], data : element})
      }
    }
  })

  console.log(filteredResponse);

  return null;
}

It is showing

[
  {
      "type": "Wellness",
      "blog_title": "First Blog",
  },
  {
      "type": "Writing",
      "blog_title": "Second Blog",

  },
  {
      "type": "Wellness",
      "blog_title": "Third Blog",

  },
  {
      "type": "Health",
      "blog_title": "Fourth Blog",
  }
]

And I am trying to get like


[
  {
    "type": "Wellness",
    "blogs": [
            "First Blog", 
            "Third Blog"
      ]
  },
  {
    "type": "Writing",
    "blogs": [
            "Second Blog", 
      ]
  },
  {
    "type": "Health",
    "blogs": [
            "Fourth Blog", 
      ]
  },
]

I have tried using :-

const map = new Map(filteredResponse.map(({ blog_title, type }) => [blog_title, { blog_title, type: [] }]));
for (let { blog_title, type } of multipleFilteredResponse) map.get(blog_title).type.push(...[type].flat());

console.log([...map.values()]);

But it reterned

[
  {
    "data": [
      {
        "type": "Wellness",
        "blog_title": "First Blog",
      },
      {
        "type": "Writing",
        "blog_title": "Second Blog",

      },
      {
        "type": "Wellness",
        "blog_title": "Third Blog",

      },
      {
        "type": "Health",
        "blog_title": "Fourth Blog",
      }
    ]
  },
  department : undefined
]

I have tried many times but it is still not working. Any help would be much Appreciated.

CodePudding user response:

I am not sure what is the original input from your response, but here is how you can turn data in your first code block into the second one you wanted.

// Online Javascript Editor for free
// Write, Edit and Run your Javascript code using JS Online Compiler

filteredResponse = [
  {
      "type": "Wellness",
      "blog_title": "First Blog",
  },
  {
      "type": "Writing",
      "blog_title": "Second Blog",

  },
  {
      "type": "Wellness",
      "blog_title": "Third Blog",

  },
  {
      "type": "Health",
      "blog_title": "Fourth Blog",
  }
]

const map = new Map(filteredResponse.map(({ blog_title, type }) => [type, { type, blogs: [] }]));

for (let { type, blog_title } of filteredResponse) map.get(type).blogs.push(blog_title);

console.log([...map.values()]);

CodePudding user response:

I would start with an object instead of an array:

const blogByTypes = {};


response.forEach(response => {
    if (blogByTypes[response.type]) {
        blogByTypes[response.type].blogs.push(response.blog_title);
    } else {
        blogByTypes[response.type] = {blogs: [response.blog_title]};
    }
});

You can then extract the entries:

const result = Object.entries(blogByTypes);
  • Related