4

I have an json data like this

{
  "name":"John",
  "age":30,
  "cars":"BMW"
},
{
  "name":"Micheal",
  "age":30,
  "cars":"Ford"
},
{
  "name":"Andy",
  "age":29,
  "cars":"Ford"
}

I just want to get the unique value of cars in an array like this

cars = ["BMW, Ford"];
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Arun Karthick
  • 326
  • 2
  • 4
  • 16
  • The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Dec 11 '18 at 08:36
  • 3
    **Hint**: First make an array from all the `cars` and then use `Set` class to get unique values! – vrintle Dec 11 '18 at 08:37
  • Thanks.. I've got the answer :) – Arun Karthick Dec 11 '18 at 08:52

4 Answers4

8

You could take a Set for unique values.

var array = [{ name: "John", age: 30, car: "BMW" }, { name: "Micheal", age: 30, car: "Ford" }, { name: "Andy", age: 29, car: "Ford" }],
    cars = Array.from(new Set(array.map(({ car }) => car)));

console.log(cars);

By using a helper function, you could hand over the data and wanted key.

const
    getUniqueValues = (data, key) => Array.from(new Set(array.map(({ [key]: value }) => value))),
    array = [{ name: "John", age: 30, car: "BMW" }, { name: "Micheal", age: 30, car: "Ford" }, { name: "Andy", age: 29, car: "Ford" }];

console.log(...getUniqueValues(array, 'name'));
console.log(...getUniqueValues(array, 'car'));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Hey Nina is there a way to make this into a helper function? ```export const filterByUniqueValues = (key, data) => { return Array.from(new Set(data.map(({key}) => key))) };``` I get the key parameter is declared but value is never read. – HelpANoobOut Jan 02 '22 at 07:26
  • 1
    @HelpANoobOut, you need to destructure a computed property and rename this variable to have a handle of it. please see the added code above. – Nina Scholz Jan 02 '22 at 08:42
3

use Array.map for get all car values and use Array.filter for filter duplicate car values, you can do it by chaining Array.map().filter()

var arr = [{
    "name": "John",
    "age": 30,
    "cars": "BMW"
  },
  {
    "name": "Micheal",
    "age": 30,
    "cars": "Ford"
  },
  {
    "name": "Andy",
    "age": 29,
    "cars": "Ford"
  }
];

var carValues = arr.map( (value) => value.cars).filter( (value, index, _arr) => _arr.indexOf(value) == index);
console.log(carValues);
Azad
  • 5,144
  • 4
  • 28
  • 56
2

You need to use Array.map() to map every object to key cars and use Array.filter() to remove duplicate items

var cars = arr.map(ele => ele.cars).filter((ele, i, arr) => arr.indexOf(ele) == i);

var arr = [{
  "name":"John",
  "age":30,
  "cars":"BMW"
},
{
  "name":"Micheal",
  "age":30,
  "cars":"Ford"
},
{
  "name":"Andy",
  "age":29,
  "cars":"Ford"
}];

var cars = arr.map(ele => ele.cars).filter((ele, i, arr) => arr.indexOf(ele) == i);
console.log(cars);

You can use also ES5

var res = arr.map(function(ele){
  return ele.cars
}).filter(function(ele, i, arr){
  return arr.indexOf(ele) == i
});
Mohammad
  • 21,175
  • 15
  • 55
  • 84
0

Assuming that data is an array, just iterate through the array like so:

var arr = [{
  "name":"John",
  "age":30,
  "cars":"BMW"
},
{
  "name":"Micheal",
  "age":30,
  "cars":"Ford"
},
 {
  "name":"Andy",
  "age":29,
  "cars":"Ford"
}];
 var cars = [];
arr.forEach(function(car) {
    if (!cars.includes(car["cars"]) {
        cars.push(car["cars"]);
    }
})
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79