Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  133] [ 7]  / answers: 1 / hits: 6892  / 2 Years ago, mon, december 27, 2021, 12:00:00
    Array(96) [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]

0: Object { id: 1, name: "PrimShal01", period: 3, … }
1: Object { id: 61, name: "TertDeep01", period: 1, … }
2: Object { id: 37, name: "SecoDeep01", period: 2, … }
3: Object { id: 49, name: "TertShal01", period: 1, … } ​
4: Object { id: 13, name: "PrimDeep01", period: 3, … }
5: Object { id: 61, name: "TertDeep01", period: 1, … }

When I try the following code I only get the unique id, but I want the objects:


const uniques = [new Set(all_filter_ids.map(pos => pos.id))]

When I try the following code I get the the same as before:


const uniques = [new Set(all_filter_ids)]

More From » arrays

 Answers
9

Another one solution:




const arr = [{ id: 1, name: PrimShal01, period: 3},{ id: 61, name: TertDeep01, period: 1},{ id: 37, name: SecoDeep01, period: 2},{ id: 49, name: TertShal01, period: 1},{ id: 13, name: PrimDeep01, period: 3},{ id: 61, name: TertDeep01, period: 1}]

const result = Object.values(
arr.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {})
);

console.log(result);

.as-console-wrapper{min-height: 100%!important; top: 0}




[#556] Wednesday, December 15, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;