Javascript reduce function error - javascript

I am trying to play with the Reduce function in JavaScript. What I am trying to achieve can be easily done via the filter function but I am trying to do it via Reduce.
I am trying to retrieve all the people where the age is greater or equal to 18 years and store the result in an array
var people = [
{ name: "John", age: 16 },
{ name: "Thomas", age: 20 },
{ name: "Smith", age: 18 },
{ name: "Jessy", age: 17 },
];
var arr = [];
var output = people.reduce(function(arr,ppl){
if(ppl.age >= 18)
return(arr.push(ppl));
},arr);
console.log(output);
However when I run this snippet, I get an error that says "TypeError: Cannot read property 'push' of undefined". I am not sure where do I need to define the arr (array where I need to store the output)

try this:
you should return arr,not return arr.push(ppl) because [].push(3) return 1(new length of [] ) not [3].And reduce works with accumulator which in this case the accumulator is arr.So,you should return arr not return (arr.push(ppl));
var people = [{
name: "John",
age: 16
},
{
name: "Thomas",
age: 20
},
{
name: "Smith",
age: 18
},
{
name: "Jessy",
age: 17
},
];
var arr = [];
var output = people.reduce(function(arr, ppl) {
if (ppl.age >= 18)
(arr.push(ppl));
return arr;
}, arr);
console.log(output);

return(arr.push(ppl));
As arr.push returns the new length of the array, it will return a number. So at the next iteration arr will be a number, and you cant push to that. So you need to pass on the array:
const output = people.reduce((arr, person) => person.age > 17 ? arr.concat(person): arr, []);
That works as arr.concat returns an array.

You can use array.concat to push to an array and return it, and put that in a ternary operator:
let people=[{name:"John",age:16},{name:"Thomas",age:20},{name:"Smith",age:18},{name:"Jessy",age:17}]
let output = people.reduce((arr,ppl) => {
return ppl.age >= 18 ? arr.concat(ppl) : arr
},[])
console.log(output)

Related

Trying to apply filter and reduce on an array to sum only numbers

I'm trying to write a function that will be called with an array that has information on a person such as their name and then age. I need this function to grab all of the numbers only and then return them then add them all together but I'm having trouble figuring out how to combine filter and reduce (if that's what I even need to use to do this in the easiest way?) so if you could help with that I would be thankful.
Maybe it would be easier for me not to use an arrow function but I just learned about them an hour ago and wanted to try them out.
Apologies for any typos/wrong jargon as my dyslexia gets the better of me sometimes.
What I've got so far;
const totalNums = arr => arr.reduce((a,b) => a + b, 0)
An example of what I want it to return when the function is supplied with the array is
{ name: 'Clarie', age: 22 },
{ name: 'Bobby', age: 30 },
{ name: 'Antonio', age: 40 },
// returns 92
EDIT
Why isn't the array I'm calling this function with working? Can you provide me a working example without the array being hardcoded like the other answers? - I'm passing in an array to the function. The main objective is to grab any number from the passed in array and add them together with an empty array returning 0.
function totalNums(person) {
person.reduce((a,b) => a + b, 0)
return person.age;
}
console.log(totalNums([]))
The first argument in a reduce callback is the "accumulator" - something that takes the initial value and is then passed into each iteration. The second value is the item that's next in the iteration whether that's a number, a string, an object etc.
In your example you're iterating over an array of objects, so you must perform the sum operation using the age value of each of those objects.
const arr = [
{ name: 'Clarie', age: 22 },
{ name: 'Bobby', age: 30 },
{ name: 'Antonio', age: 40 }
];
const total = arr.reduce((acc, obj) => {
return acc + obj.age;
}, 0);
console.log(total);
You can reduce the object array number as below
const objArr = [
{ name: "Clarie", age: 22 },
{ name: "Bobby", age: 30 },
{ name: "Antonio", age: 40 },
];
console.log(objArr.reduce((prev, curr) => prev + curr.age, 0));
If you want to convert the array of object into array containing only ages:
const objArr = [
{ name: 'Clarie', age: 22 },
{ name: 'Bobby', age: 30 },
{ name: 'Antonio', age: 40 },
];
console.log(objArr.map(obj => obj.age));

How to return specific values from an array with objects inside in a new array which must pass a test?

Hello I am try to get the solution but dont find the right answer on my own research and hope somebody can help me with my problem. The task required that I must write a function which returns every persons name which is 16 or older in a new array. The goal is to write a function which returns in my example: ['Jane', 'Jack']
function onlyAdult(obj) {
}
const examplePeopleArray = [
{ name: 'John', age: 15 },
{ name: 'Jane', age: 16 },
{ name: 'Jack', age: 17 }
];
console.log(onlyAdult(examplePeopleArray));
I tried to manage the task with a for loop which loop through the array and connected a if statement but this way it doesnt worked. After this i tried to find the right methods for my task with every(),filter(), forEach(), map(), some() but none of these actually worked for my task .
function onlyAdult(obj) {
for (i = 0; i < obj.length; i++) {
if (obj[0].age >= 16) {
return obj[0].age;
} else if (obj[1].age >= 16) {
return obj[1].age;
} else if (obj[2].age >= 16) {
return obj[2].age;
}
}
}
I know my code is wrong and also the way I tried to solved it, I would be very grateful when someone could help me .
You can filter the array first using .filter() and then use .map() to get the desired property values only.
const data = [
{ name: 'John', age: 15 },
{ name: 'Jane', age: 16 },
{ name: 'Jack', age: 17 }
];
const result = data.filter(({ age }) => age >= 16).map(({ name }) => name);
console.log(result);
References:
Array.prototype.filter()
Array.prototype.map()
Object Destructuring
const examplePeopleArray = [
{ name: 'John', age: 15 },
{ name: 'Jane', age: 16 },
{ name: 'Jack', age: 17 }
];
const result = examplePeopleArray.reduce((arr, el) => {
if (el.age >= 16) arr.push(el.name)
return arr
}, [])
For this given task reduce will be enough. Here we reduce input array to an array of filtered string values based on age comparison. If age is under 16 we just do not push anything and skip to the next element.

Find (and remove) duplicates in array of objects depending on object attributes

I want to remove duplicates in an array of objects, depending on object attributes.
Simplified example:
Assuming you have an array like:
[
{
name: 'alice',
something: 123
},
{
name: 'alice',
something: 321
},
{
name: 'bob',
something: 213
}
]
I want to remove objects, wich have the same value for name, but I want to decide which object to remove with some custom calculation (e.g. keep the object with bigger value for something).
I was able to adapt the accepted answer in find duplicate values in a JavaScript array, but that does not work so well with more than 2 duplicates.
You can try with reduce and object, set properties base on your condition.
Then convert it to array by Object.values.
var arr = [
{
name: 'alice',
something: 123
},
{
name: 'alice',
something: 321
},
{
name: 'bob',
something: 213
}
];
var res = arr.reduce( (acc,b) => {
if ((acc[b.name] && acc[b.name].something < b.something) || !acc[b.name]) {
acc[b.name] = b;
}
return acc;
}, {});
var newArr = Object.values(res);
console.log(newArr);
You could use a hash table as reference to the same name objects.
var array = [{ name: 'alice',something: 123 }, { name: 'alice', something: 321 }, { name: 'bob', something: 213 }],
result = array.reduce(function (hash) {
return function (r, a) {
if (!(a.name in hash)) {
hash[a.name] = r.push(a) - 1;
return r;
}
if (r[hash[a.name]].something < a.something) {
r[hash[a.name]] = a;
}
return r;
};
}(Object.create(null)), []);
console.log(result)

How to find duplicate values in a JavaScript array of objects, and output only unique values?

I'm learning JS. Supposing I have the below array of objects:
var family = [
{
name: "Mike",
age: 10
},
{
name: "Matt"
age: 13
},
{
name: "Nancy",
age: 15
},
{
name: "Adam",
age: 22
},
{
name: "Jenny",
age: 85
},
{
name: "Nancy",
age: 2
},
{
name: "Carl",
age: 40
}
];
Notice that Nancy is showing up twice (changing only the age). Supposing I want to output only unique names. How do I output the above array of objects, without duplicates? ES6 answers more than welcome.
Related (couldn't find a good way for usage on objects):
Remove Duplicates from JavaScript Array
Easiest way to find duplicate values in a JavaScript array
EDIT Here's what I tried. It works well with strings but I can't figure how to make it work with objects:
family.reduce((a, b) => {
if (a.indexOf(b) < 0 ) {
a.push(b);
}
return a;
},[]);
You could use a Set in combination with Array#map and a spread operator ... in a single line.
Map returns an array with all names, which are going into the set initializer and then all values of the set are returned in an array.
var family = [{ name: "Mike", age: 10 }, { name: "Matt", age: 13 }, { name: "Nancy", age: 15 }, { name: "Adam", age: 22 }, { name: "Jenny", age: 85 }, { name: "Nancy", age: 2 }, { name: "Carl", age: 40 }],
unique = [...new Set(family.map(a => a.name))];
console.log(unique);
For filtering and return only unique names, you can use Array#filter with Set.
var family = [{ name: "Mike", age: 10 }, { name: "Matt", age: 13 }, { name: "Nancy", age: 15 }, { name: "Adam", age: 22 }, { name: "Jenny", age: 85 }, { name: "Nancy", age: 2 }, { name: "Carl", age: 40 }],
unique = family.filter((set => f => !set.has(f.name) && set.add(f.name))(new Set));
console.log(unique);
The Solution
Store occurrences of name external to the loop in an object, and filter if there's been a previous occurrence.
https://jsfiddle.net/nputptbb/2/
var occurrences = {}
var filteredFamily = family.filter(function(x) {
if (occurrences[x.name]) {
return false;
}
occurrences[x.name] = true;
return true;
})
you can also generalize this solution to a function
function filterByProperty(array, propertyName) {
var occurrences = {}
return array.filter(function(x) {
var property = x[propertyName]
if (occurrences[property]) {
return false;
}
occurrences[property]] = true;
return true;
})
}
and use it like
var filteredFamily = filterByProperty(family, 'name')
Explanation
Don't compare objects using indexOf, which only uses the === operator between objects. The reason why your current answer doesn't work is because === in JS does not compare the objects deeply, but instead compares the references. What I mean by that you can see in the following code:
var a = { x: 1 }
var b = { x: 1 }
console.log(a === b) // false
console.log(a === a) // true
Equality will tell you if you found the same exact object, but not if you found an object with the same contents.
In this case, you can compare your object on name since it should be a unique key. So obj.name === obj.name instead of obj === obj. Moreover another problem with your code that affects its runtime and not its function is that you use an indexOf inside of your reduce. indexOf is O(n), which makes the complexity of your algorithm O(n^2). Thus, it's better to use an object, which has O(1) lookup.
This will work fine.
const result = [1, 2, 2, 3, 3, 3, 3].reduce((x, y) => x.includes(y) ? x : [...x, y], []);
console.log(result);
With the code you mentioned, you can try:
family.filter((item, index, array) => {
return array.map((mapItem) => mapItem['name']).indexOf(item['name']) === index
})
Or you can have a generic function to make it work for other array of objects as well:
function printUniqueResults (arrayOfObj, key) {
return arrayOfObj.filter((item, index, array) => {
return array.map((mapItem) => mapItem[key]).indexOf(item[key]) === index
})
}
and then just use printUniqueResults(family, 'name')
(FIDDLE)
I just thought of 2 simple ways for Lodash users
Given this array:
let family = [
{
name: "Mike",
age: 10
},
{
name: "Matt",
age: 13
},
{
name: "Nancy",
age: 15
},
{
name: "Adam",
age: 22
},
{
name: "Jenny",
age: 85
},
{
name: "Nancy",
age: 2
},
{
name: "Carl",
age: 40
}
]
1. Find duplicates:
let duplicatesArr = _.difference(family, _.uniqBy(family, 'name'), 'name')
// duplicatesArr:
// [{
// name: "Nancy",
// age: 2
// }]
2 Find if there are duplicates, for validation purpose:
let uniqArr = _.uniqBy(family, 'name')
if (uniqArr.length === family.length) {
// No duplicates
}
if (uniqArr.length !== family.length) {
// Has duplicates
}
Since most of the answers won't have a good performance, i thought i share my take on this:
const arrayWithDuplicateData = [{ id: 5, name: 'Facebook'}, { id: 3, name: 'Twitter' }, { id: 5, name: 'Facebook' }];
const uniqueObj = {};
arrayWithDuplicateData.forEach(i => {
uniqueObj[i.id] = i;
});
const arrayWithoutDuplicates = Object.values(uniqueObj);
We're leveraging the fact that keys are unique within objects. That means the last duplication item inside the first array, will win over its predecessors. If we'd want to change that, we could flip the array before iterating over it.
Also we're not bound to use only one property of our object for identifying duplications.
const arrayWithDuplicateData = [{ id: 5, name: 'Facebook'}, { id: 3, name: 'Twitter' }, { id: 5, name: 'Facebook' }];
const uniqueObj = {};
arrayWithDuplicateData.forEach(item => {
uniqueObj[`${item.id}_${item.name}`] = item;
});
const arrayWithoutDuplicates = Object.values(uniqueObj);
Or we could simply add a check, if the uniqueObj already holds a key and if yes, not overwrite it.
Overall this way is not very costly in terms of performance and served me well so far.
I would probably set up some kind of object. Since you've said ECMAScript 6, you have access to Set, but since you want to compare values on your objects, it will take a little more work than that.
An example might look something like this (removed namespace pattern for clarity):
var setOfValues = new Set();
var items = [];
function add(item, valueGetter) {
var value = valueGetter(item);
if (setOfValues.has(value))
return;
setOfValues.add(value);
items.push(item);
}
function addMany(items, valueGetter) {
items.forEach(item => add(item, valueGetter));
}
Use it like this:
var family = [
...
];
addMany(family, item => item.name);
// items will now contain the unique items
Explanation: you need to pull a value from each object as it's added and decide if it has already been added yet, based on the value you get. It requires a value getter, which is a function that given an item, returns a value (item => item.name). Then, you only add items whose values haven't already been seen.
A class implementation:
// Prevents duplicate objects from being added
class ObjectSet {
constructor(key) {
this.key = key;
this.items = [];
this.set = new Set();
}
add(item) {
if (this.set.has(item[this.key])) return;
this.set.add(item[this.key]);
this.items.push(item);
}
addMany(items) {
items.forEach(item => this.add(item));
}
}
var mySet = new ObjectSet('name');
mySet.addMany(family);
console.log(mySet.items);

How to skip same values and get array length

I have my example array:
var person = [{
firstName:"John",
lastName:"Doe",
age:46
},
{
firstName:"Alexander",
lastName:"Bru",
age:46
},
{
firstName:"Alex",
lastName:"Bruce",
age:26
}];
Simple person.length gives me the length of my array, but I need to merge values when the age is the same. So if two people have same age return 1 no 2. Sorry for my bad English, I can made a mistakes.
Use Array#forEach method with an object reference for age.
var person = [{
firstName: "John",
lastName: "Doe",
age: 46
}, {
firstName: "Alexander",
lastName: "Bru",
age: 46
}, {
firstName: "Alex",
lastName: "Bruce",
age: 26
}];
// object for storing reference to age
var obj = {},
res = 0;
// iterate and count
person.forEach(function(v) {
// check age already not defined
if (!obj[v.age]) {
// define the property
obj[v.age] = true;
// increment count
res++;
}
});
console.log(res);
you can use underscore or similar library that supports groupBy:
_.size(_.groupBy(person, "age"))
Filter the array down to only those elements for which a find on the array for the first element with the same age yields the element itself, then take the length of the result:
array.filter(o1 => o1 === array.find(o2 => o2.age === o1.age)).length
Another idea involves using a little function called uniqueCount, which counts the number of unique values in a (sorted) array:
function uniqueCount(a) {
return a.reduce((cnt, elt, i) => a[i] === a[i-1] ? cnt : cnt + 1), 0);
}
Now you can create an array of all the ages, and do a count of its unique elements on it:
uniqueCount(array.map(e => e.age).sort(numeric))
If you are allowed to, you could add all the ages to a set and take its size.
const people = [{
firstName: "John",
lastName: "Doe",
age: 46
}, {
firstName: "Alexander",
lastName: "Bru",
age: 46
}, {
firstName: "Alex",
lastName: "Bruce",
age: 26
}];
const ages = new Set(people.map(person => person.age));
console.log(ages.size)

Categories

Resources