How to skip same values and get array length - javascript

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)

Related

How to merge the key-value pairs of two json arrays? - Javascript

I have two JSON arrays like this. I want to merge their keyvalue pairs. They are some items which are common in both while they are some items which are uncommon.
var jsonData1 = [
{
firstName: "Sam",
age: "10"
},
{
firstName: "John",
age: "11"
},
{
firstName: "Jack",
age: "12"
},
{
firstName: "Pam",
age: "13"
},
{
firstName: "Tom",
age: "14"
},
{
firstName: "Mitch",
age: "15"
}
];
var jsonData2 = [
{
firstName: "Sam",
city: "London"
},
{
firstName: "John",
city: "New York"
},
{
firstName: "Jack",
city: "Paris"
},
{
firstName: "Pam",
city: "Moscow"
},
{
firstName: "Roger",
city: "Shanghai"
},
{
firstName: "Don",
city: "Jakarta"
}
];
As you can there are some firstName in 1st array which does not have city in 2nd array. Again there are some firstName in 2nd array which does not have city in 1st array.
I need to club these 2 array into one array, in case a firstName does not have age or city it will be assigned '' (blank string).
The new array will have 3 fields, there are some items which will be having values in 2 fields. They have a one field as blank string.
I want to do this using Vanilla JS, I do not want to use Jquery, Lodash and Underscore.
There are a number of ways to approach this, however one option would be to base this around the Array#reduce() method as follows:
const jsonData1=[{firstName:"Sam",age:"10"},{firstName:"John",age:"11"},{firstName:"Jack",age:"12"},{firstName:"Pam",age:"13"},{firstName:"Tom",age:"14"},{firstName:"Mitch",age:"15"}];
const jsonData2=[{firstName:"Sam",city:"London"},{firstName:"John",city:"New York"},{firstName:"Jack",city:"Paris"},{firstName:"Pam",city:"Moscow"},{firstName:"Roger",city:"Shanghai"},{firstName:"Don",city:"Jakarta"}];
/*
Use Array#concat() to combine both input arrays before performing
the merge. This will ensure that all items in both arrays are
processed and accounted for during the merge (which is important in
situations where the input arrays differ in length).
Object.values() is used to transform the dictionary resulting from
reduce() to an array
*/
var result = Object.values(
[].concat(jsonData1, jsonData2
).reduce((dict, item) => {
/*
We use Array#reduce is an intermediate step to construct a dictionary
which maps each unique "item.firstName" to a corresponding object
that contains the merged (or yet to be merged) data for this first
name
*/
var value = dict[item.firstName] || {}
/*
Use Object.assign() to merge existing data for "item.firstName" with
current item being processed. Also pass default values as first
argument to ensure all three key values are present in merge result
*/
value = Object.assign({ firstName : '', age : '', city : ''} , value, item)
/*
Update the dictionary with merged data
*/
dict[item.firstName] = value
return dict
}, {}))
console.log(result);
One possible approach:
const jsonData1=[{firstName:"Sam",age:"10"},{firstName:"John",age:"11"},{firstName:"Jack",age:"12"},{firstName:"Pam",age:"13"},{firstName:"Tom",age:"14"},{firstName:"Mitch",age:"15"}];
const jsonData2=[{firstName:"Sam",city:"London"},{firstName:"John",city:"New York"},{firstName:"Jack",city:"Paris"},{firstName:"Pam",city:"Moscow"},{firstName:"Roger",city:"Shanghai"},{firstName:"Don",city:"Jakarta"}];
const result = [].concat(jsonData1, jsonData2).reduce((acc, ele) => {
const existing = acc.find(x => x.firstName === ele.firstName);
if(!existing) return acc.concat({firstName: ele.firstName, city: ele.city || '', age: ele.age || ''});
Object.keys(ele).forEach(x => existing[x] = ele[x]);
return acc;
},[])
console.log(result);
I realize you've already accepted an answer but I figured I could provide an alternative, be it better or worse.
var jsonData1 = [{firstName: "Sam",age: "10"},{firstName: "John",age: "11"},{firstName: "Jack",age: "12"},{firstName: "Pam",age: "13"},{firstName: "Tom",age: "14"},{firstName: "Mitch",age: "15"}];
var jsonData2 = [{firstName: "Sam",city: "London"},{firstName: "John",city: "New York"},{firstName: "Jack",city: "Paris"},{firstName: "Pam",city: "Moscow"},{firstName: "Roger",city: "Shanghai"},{firstName: "Don",city: "Jakarta"}];
var defaults = {firstName: "", age: "", city: ""};
var data = [ ...jsonData1, ...jsonData2 ];
var names = [ ...new Set(data.map(i=>i.firstName)) ];
var res = names.map(n => data
.reduce((acc, jd) => jd.firstName === n ? {...acc, ...jd} : acc, defaults)
);
console.log(res);
var data combines the two arrays of data into one using spread syntax (array literals).
var names creates an array of unique names using Set.
map() iterates over the list of names, creating a single, merged object for each. That merge is done using reduce() and spread syntax (object literals).

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.

Javascript reduce function error

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)

Comparing properties and returning associated siblings

Let's say I have an arbitrarily large number of objects:
obj1 = {
name: "Fred",
score: 1
}
obj2 = {
name: "Steve",
score: 2
}
obj3 = {
name: "Billy",
score: 3
}
I'd like to easily compare the score properties of each of these objects, and then return the name property of the object with the highest score. I'm not completely certain how to easily find the highest value of an arbitrarily large group of numbers, but I'm sure I'll figure it out when the time comes. My real question is how to, after finding the highest score, return the name associated with that score's parent object. (In case of time, my program should return all highest scores.)
Here is a solution using Array.prototype.reduce.
JSBIN:
http://jsbin.com/cepofigomu/edit?html,js,console,output
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
var data = [
{
name: "Fred",
score: 1
},
{
name: "Steve",
score: 2
},
{
name: "Billy",
score: 3
}
];
var out = data.reduce(function(a, b){
return a.score > b.score ? a:b ;
}, {});
console.log(out);
Sorry I only read you did not have jQuery available after I finished writing an example.
You should be able to translate it to pure javascript though.
https://jsfiddle.net/q2x9phf5/
var scores = [];
scores.push({
name: "Fred",
score: 1
});
scores.push({
name: "Steve",
score: 2
});
scores.push({
name: "Billy",
score: 3
});
var highestScore = 0;
$.each(scores, function(index, person) {
highestScore = Math.max(highestScore, person.score);
});
var peopleWithHighestScore = $.grep(scores, function(person) {
return person.score === highestScore;
});

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);

Categories

Resources