0

I have an array of objects:

let array1 = [
  { age: 20, name: "bob" }, 
  { age: 24, name: "Mike" },
  { age: 20, name: "Penny" },
  { age: 24, name: "Jeff" },
  { age: 25, name: "Mary" }
];

Trying to find an example of a forLoop or forEach that will loop over the array of objects and return only the unique age keys([20, 24, 25]) that I will push to another array.

The expect results values would be:

[20, 24, 25]
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Gabe Perry
  • 79
  • 9
  • Possible duplicate of [Filter unique values from an array of objects](https://stackoverflow.com/questions/43374112/filter-unique-values-from-an-array-of-objects) – vahdet Mar 23 '18 at 12:28
  • May you provide the expected result? it's quite unclear what you mean by `and return only the unique age keys that I will push to another array.` – briosheje Mar 23 '18 at 12:34

6 Answers6

4

Using es6 sets this can be accomplished in a single line.

let res = [...new Set(array1.map(e => e.age))];

Explained:

array1.map(e => e.age) will return a copy of the original array by returning the .age values only, hence the result will be: [20,24,20,24,25].

new Set(array1.map(e => e.age)) will make a unique set from the above array, hence it will be a Set instance, which will automatically remove duplicates.

...new Set <-- the spread operator (...) will convert the above set to an array. The corresponding result will be, so, [20,24,25].

https://jsfiddle.net/6ozdqsuw/2/

briosheje
  • 7,356
  • 2
  • 32
  • 54
1

If you'd like to end up with a standard array I'd recommend getting all instances of age, then convert to a Set and back again.

First get just the entries at the age key to get an array of ages.

const allAges = originalArray.map(({ age }) => age)

Then create a set from the age:

const ageSet = new Set(allAges)

Then convert back to an array of you want:

const uniqueAges = [...ageSet]

If you want it in one line:

const uniqueAges = [...new Set(originalArray.map(({ age }) => age))]
Mackie Drew
  • 43
  • 1
  • 8
0

Something like this?

let result = [];
array1.forEach(e=>{
    if (result.indexOf(e.age) === -1) {
        result.push(e.age)
    }
})
TLP
  • 1,262
  • 1
  • 8
  • 20
  • Thank you so much. It works. I have an understanding of what you are doing by pushing to two arrays. Can you explain the function or actually write it out? – Gabe Perry Mar 23 '18 at 12:53
  • It loops through the original array and checks if the value for the `age` property exists in the `result` array (its `indexOf()` will return `-1` if it doesn't). If not, it will push this value to `result`. – TLP Mar 23 '18 at 12:56
  • Thanks I'm not good with arrow functions. Thanks why I was like can we write this out lol – Gabe Perry Mar 23 '18 at 13:18
0

I'm not sure what do you mean by "unique" but, if you use filter, you can get a new array with only age keys:

let filtered = array1.map(elem => elem.age)

More info here:

Damian Peralta
  • 1,846
  • 7
  • 11
0

Your problem is exactly the thing the ES6 Set was made for!

Using a Set, you can add any elements you like, but they will only add to the set if they are not already in it.

let array1 = [
    {
        name: "bob",
        age: 20,
    }, 
    {
        name: "Mike",
        age: 24,
    },
    {
        name: "Penny",
        age: 20,
    },
    {
        name: "Jeff",
        age: 24,
    },
    {
        name: "Mary",
        age: 25,
    },
]

let ages = new Set()
array1.forEach(user => {
  ages.add(user.age)
}) 

//ages = Set [ 20, 24, 25 ]
Soham Kamani
  • 552
  • 2
  • 7
0

Based off of my understanding of the question, this is a functional solution using built in Array iterator methods. map returns an array of just the ages (ie: [20, 24, 20, 24, 25]) and filter removes duplicates.

let array1 = [
    {
        name: "bob",
        age: 20,
    }, 
    {
        name: "Mike",
        age: 24,
    },
    {
        name: "Penny",
        age: 20,
    },
    {
        name: "Jeff",
        age: 24,
    },
    {
        name: "Mary",
        age: 25,
    },
]

console.log(
  array1
    .map(({age}) => age)
    .filter((age, i, arr) => arr.lastIndexOf(age) === i)
)
vapurrmaid
  • 2,287
  • 2
  • 14
  • 30