Home > Blockchain >  remove duplicate string include special char and space from json using java scripts..?
remove duplicate string include special char and space from json using java scripts..?

Time:02-07

I want get all state from Json with unique value please click on this link to go through my question

desire output

Maharashtra
Andaman & Nicobar Islands
Andhra Pradesh
Andhra pradesh
Arunachal Pradesh
Assam
Bihar

CodePudding user response:

Start by assigning your array of objects to a variable:

let places = [
  {"city":"Kolhapur", "state":"Maharashtra"},
  {"city":"Port Blair", "state":"Andaman & Nicobar Islands"},
  {"city":"Adilabad", "state":"Andhra Pradesh"},
  {"city":"Adoni", "state":"Andhra Pradesh"},
  // etc. etc.
];

Then extract the states into an array:

let states = places.map(obj => obj.state);

Then remove the duplicates from the array:

let uniques = [...new Set(states)];

And there you have it:

console.log(uniques);

// [
//   'Maharashtra',
//   'Andaman & Nicobar Islands',
//   'Andhra Pradesh',
//   'Andhra pradesh',
//   'Arunachal Pradesh',
//   'Assam',
//   'Bihar',
//   etc. etc.
// ]

You can combine these steps as you see fit:

console.log([...new Set(places.map(obj => obj.state))]);
  •  Tags:  
  • Related