JavaScript Removing duplicate key value pair in javascript array removing duplicate value in excel,remove duplicate value from array

I have a javascript array which get created dynamically in this format.

[{prdName: "Testing2"},
{prdName: "Testing2,Testing3"},
{markets: "Testing5"},
{markets: "Testing5,Testing6"}]

I want to remove the duplicate key in the above array map and convert it into this format.

 [ {prdName: "Testing2,Testing3"},
   {markets: "Testing5,Testing6"} ]

Could you let me know how to achieve the same. I am working on a reactjs application.

Answer:1

I'm assuming that you want to preserve all of the non-duplicate comma-delimited entries, not just throw away all but the last group as in the other answers. (So, for example, if the input were [{foo: "x"}, {foo:"x,y"}, {foo:"z"}] the output should be [{foo: "x,y,z"}], not [{foo:"z"}].)

var rearrange = function(input) {
  var tmp = {}; // track keys and values as we find them
  for (obj of input) {
    var key = Object.keys(obj)[0]; // each input has one key
    tmp[key] = tmp[key] || {}; // initialize an empty object at that key in tmp if necessary
    var vals = obj[key].split(",");
    for (v of vals) {
      tmp[key][v.trim()] = 1; // keep each of the (trimmed) comma-delimited values, implicitly dropping duplicates
    }
  }

  // now convert the tmp object into an array:
  var output = [];
  for (k of Object.keys(tmp)) {
    var x = {};
    x[k] = Object.keys(tmp[k]).join(","); // merge the values back into a comma-separated string
    output.push(x);
  }
  return output;
}
console.log(rearrange([
  {prdName: "Testing2"},
  {prdName: "Testing2,Testing3"},
  {markets: "Testing5"},
  {markets: "Testing5,Testing6"}
]));

console.log(rearrange([
  {foo: "x"},
  {foo: "x,y"},
  {foo: "z"},
  {bar: "x,y,z"}
]));
Answer:2

With ES6, you could use Map with Set for unique items.

var array = [{ prdName: "Testing2" }, { prdName: "Testing2,Testing3" }, { markets: "Testing5" }, { markets: "Testing5,Testing6" }],
    map = new Map,
    result;

array.forEach(o => Object.keys(o).forEach(k => {
    if (!map.has(k)) {
        map.set(k, new Set);
    }
    o[k].split(',').forEach(s => map.get(k).add(s));
}));

result = [...map].map(([k, s]) => ({ [k]: [...s].join() }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

Hi I want the form I'm making to submit but not before the users press ENTER key without accepting terms and conditions. I already made that but in Safari when the user press ENTER, the alert ...

  1. form submit alert javascript
  2. form submit alert in jquery
  3. form submit alert message
  4. submit form alert box
  5. html form submit alert
  6. php form submit alert
  7. form submit success alert
  8. form submit confirm alert
  9. javascript form submit alert box
  10. form submit after alert
  11. form submit sweet alert
  12. bootstrap form submit alert
  13. mvc form submit alert
  14. form submit onclick alert
  15. on submit alert form input
  16. submit form using sweet alert
  17. contact form 7 submit alert
  18. submit form alertify
  19. form submit 후 alert

I use jquery.flexslider plugin, which works fine. But for screens < 600px I do not want to load the plugin. I tried several scripts and ended with the following, which I thought works best. I ...

I use jquery.flexslider plugin, which works fine. But for screens < 600px I do not want to load the plugin. I tried several scripts and ended with the following, which I thought works best. I ...

I am creating a Line Chart in D3.js with two lines. The second line needs to start (and be rendered) and at a later date than the first. The data I am using for the graph looks like this: var data = [...

I am creating a Line Chart in D3.js with two lines. The second line needs to start (and be rendered) and at a later date than the first. The data I am using for the graph looks like this: var data = [...

  1. excel leader lines line chart
  2. multiple lines line chart power bi
  3. add vertical lines line chart
  4. 2 lines line chart
  5. hide lines line chart
  6. line chart with multiple lines
  7. line chart with multiple lines excel
  8. line chart not showing lines
  9. line chart (cubic lines) swift
  10. line chart with 2 lines excel
  11. line chart cubic lines android example
  12. line chart too many lines
  13. line chart with multiple lines tableau
  14. line chart with multiple lines in r
  15. line chart with many lines
  16. line chart with two lines
  17. line chart excel two lines
  18. line chart smooth lines
  19. line chart with multiple lines python
  20. line chart with multiple lines chart js

I am using the jQuery Globalize library which uses cldr for internationalization. Before you can use any of the Globalize functions, you have to load the correct cldr-data and pass it to Globalize. ...

I am using the jQuery Globalize library which uses cldr for internationalization. Before you can use any of the Globalize functions, you have to load the correct cldr-data and pass it to Globalize. ...