AnswerBun.com

Typescript get distinct values from an object

Stack Overflow Asked by Nikhila Reddy C on December 9, 2020

How do I write select distinct country, state from myObject in javascript.

Array.from(new Set(myObject.map(item => item.country))) would return distinct countries.. How do I add countries, states and other columns in map???

Input data:

const myObject = [
  {
    country: 'USA',
    state: 'Missouri',
    county: 'County1',
  },
  {
    country: 'USA',
    state: 'Missouri',
    county: 'County2',
  },
  {
    country: 'Canada',
    state: 'Alberta',
    county: 'County3',
  },
];

To get unique countries, I would write
Array.from(new Set(myObject.map(item => item.country)))
The result would be [‘USA’, ‘Canada’]

What should I write if want the result to be

[
  {
    country: 'USA',
    state: 'Missouri',
  },
  {
    country: 'Canada',
    state: 'Alberta',
  },
]

This contains unique country and state combinations

3 Answers

Below is a solution using reduce;

const myObject = [
  { country: 'USA', state: 'Missouri', county: 'County1' },
  { country: 'USA', state: 'Missouri', county: 'County2' },
  { country: 'Canada', state: 'Alberta', county: 'County3' },
];

const unique = myObject.reduce((prev, {country, state}) => 
  prev.some(x => x.country === country && x.state === state )? prev: [...prev, {country, state} ], [])

   console.log(unique )

Answered by Owen Kelvin on December 9, 2020

Base on referenced answer you can use Map and spread operator as below

https://stackoverflow.com/a/63274702/4964569

const myObject = [
  { country: 'USA', state: 'Missouri', county: 'County1' },
  { country: 'USA', state: 'Missouri', county: 'County2' },
  { country: 'Canada', state: 'Alberta', county: 'County3' },
];

let data = new Map();

for (let obj of myObject) {
  delete obj.county;
  data.set(obj.country, obj);
}

let unique = [...data.values()];

console.log(unique);

Answered by Hien Nguyen on December 9, 2020

The following code should take an array of objects and a list of keys of those objects, and return an array of objects representing the distinct values for that set of keys. I assume that the type of properties at those keys will only be string, number, or boolean.

function distinct<T extends Record<K, string | number | boolean>,
  K extends keyof T>(arr: T[], ...keys: K[]): Pick<T, K>[] {
  const key = (obj: T) => JSON.stringify(keys.map(k => obj[k]));
  const val = (obj: T) => keys.reduce((a, k) => (a[k] = obj[k], a), {} as Pick<T, K>);
  const dict = arr.reduce((a, t) => (a[key(t)] = val(t), a), {} as { [k: string]: Pick<T, K> })
  return Object.values(dict);
}

The idea is to take each object in the array, serialize the tuple of its properties at the keys in question with JSON.stringify(), and use this serialized string as a dictionary key. The value we put at that key is an object consisting of just the values at those properties. By using a dictionary, we guarantee that only one object will appear for each distinct set of properties at the keys we care about. Then we turn the values of this dictionary into an array.

If I test it on your myObject example, this is what comes out:

const result = distinct(myObject, "country", "state");
console.log(result);
/*  [{ "country": "USA", "state": "Missouri" }, { "country": "Canada", "state": "Alberta" }] */

which is what you wanted. Let's also test it on some type and array that I made up now:

interface MyObject {
  country: string,
  state: string,
  age: number,
  name: string
}
const arr: MyObject[] = [
  { name: "Alice", age: 35, country: "USA", state: "MA" },
  { name: "Bob", age: 40, country: "USA", state: "MI" },
  { name: "Carmen", age: 35, country: "Mexico", state: "BC" },
  { name: "Danilo", age: 35, country: "Mexico", state: "MI" }
]

So we have an array of MyObject. Let's get the distinct country values:

const distinctCountries = distinct(arr, "country"); // Array<{country: string}>
console.log(distinctCountries); // [{ "country": "USA" }, { "country": "Mexico" }] 
console.log(distinctCountries.map(x => x.country)); // USA, Mexico

Notice how what comes out is an array of {country: string} values. You can use map() to turn that into just an array of strings. Let's get the distinct country and state values:

const distinctCountriesAndStates = distinct(arr, "country", "state");
console.log(distinctCountriesAndStates);
/* [{ "country": "USA", "state": "MA" }, { "country": "USA", "state": "MI" }, 
{ "country": "Mexico", "state": "BC" }, { "country": "Mexico", "state": "MI" }] */

Here it's an array of {country: string, state: string} objects. Not sure how you want to represent those, but you can use map() to massage them as you see fit. Finally let's get the distinct country and age values:

const distinctAgesAndCountries = distinct(arr, "country", "age");
console.log(distinctAgesAndCountries);
/* [{ "country": "USA", "age": 35 }, { "country": "USA", "age": 40 },
 { "country": "Mexico", "age": 35 }] */

That's an array of {country: string, age: number} objects.

Anyway, hope that gives you some direction.

Playground link to code

Answered by jcalz on December 9, 2020

Add your own answers!

Related Questions

Can i show all variable results in a div in jQuery?

0  Asked on December 5, 2021 by nippledisaster

   

Comparing two generics in compareTo()

2  Asked on December 5, 2021 by chalupabatmac

       

arranging the json file base on key value

1  Asked on December 5, 2021 by prateek-singh

     

Vue watch method not filtering object

3  Asked on December 5, 2021

   

Passing a variable between HTML pages using JavaScript

2  Asked on December 5, 2021 by robert-little

   

How to avoid code redundancy with JAVA generics

1  Asked on December 5, 2021 by cosimoth

   

Why is my project no longer updating when I write new code?

1  Asked on December 5, 2021 by stephen-horton

       

node https ssl your connection is not private error

2  Asked on December 5, 2021 by andrea-dattero

       

Nested for loop using dict

9  Asked on December 5, 2021 by purwu

     

React Hooks – Input loses focus when 1 character is typed in

4  Asked on December 5, 2021 by dragos-strugar

   

How do I build a C Python Extension for Windows using Cygwin64?

2  Asked on December 5, 2021 by zenith2198

       

Wificonfiguration is deprecated Android 10

1  Asked on December 5, 2021 by ahmad-idrees

         

Wrong tootip in chartjs

1  Asked on December 5, 2021 by thiago-scher

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP