javascript - Array.filter first of value -
i have json array based on structure:
[ {property1: "bmw", property2: value, property3: value, property4: value}, {property1: "mercedes", property2: value, property3: value, property4: value}, {property1: "bmw", property2: value, property3: value, property4: value}, {property1: "nissan", property2: value, property3: value, property4: value} ]
how can filter array find first time new value appears property1
, store in separate array?
i array like: ["bmw", "mercedes", "nissan"]
if want store unique values, should use javascript built in-object set()
the method .add(value)
append value
set if doesn't exist
let carnames = new set(); let cars= [ { property1: "bmw" }, { property1: "mercedes" }, { property1: "bmw" }, { property1: "nissan" } ]; cars.foreach(car => carnames.add(car.property1)); console.log(carnames.size); (let name of carnames) { console.log(name); }
Comments
Post a Comment