Add count value to object with same key values (array of objects) - javascript

Hello I have kind of complicated iteration to be done over an array of objects. I have array like this:
[
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
]
I would like to add count property to each object that counts objects with same name and surname... So it should be now:
[
{ name: 'Jacob', lastName: 'Smith', count: 4 },
{ name: 'Ann', lastName: 'Smith', count: 2 },
{ name: 'Ann', lastName: 'Nansen', count: 1' },
]

You can use Array.reduce and Object.values
Convert array in an object with key as name and last name combination with value being the resulting object.
From the object, get all values as the final result
let arr = [{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' }];
let result = Object.values(arr.reduce((a,{name, lastName}) => {
let key = `${name}_${lastName}`;
a[key] = a[key] || {name, lastName, count : 0};
a[key].count++;
return a;
}, {}));
console.log(result);

const hash = [];
for(const { name, lastName } of persons) {
const key = name + "/" + lastName;
if(!hash[key]) hash[key] = {
name,
lastName,
count: 0,
};
hash[key].count++;
}
const result = Object.values(hash);

You could use JSON.stringify to combine name and last name in a safe way. I like using a Map to group the records with the same keys together:
const data = [{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' }];
const keyed = data.map(o => [JSON.stringify([o.name, o.lastName]), o]);
const map = new Map(keyed.map(([key, {name, lastName}]) =>
[key, {name, lastName, count: 0}]));
keyed.forEach(([key, o]) => map.get(key).count++);
const result = Array.from(map.values());
console.log(result);

let arr=[
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
];
let outerArr=[];
for(arrValue of arr)
{
delete arrValue.dob
let index=outerArr.findIndex(item=> item.name==arrValue.name &&
item.lastName==arrValue.lastName);
if(index==-1)
{
let arrFind=arr.filter(item=> item.name==arrValue.name &&
item.lastName==arrValue.lastName)
arrValue.count=arrFind.length
outerArr.push(arrValue)
}
}
console.log('result',outerArr)

You can achieve this by reducing the original Array.
As you iterate through the people you can check if they have already been "grouped" using Array.some - if they haven't, push your built person Object to the previously returned Array.
const getInstances = ({ name, lastName }, data) => data.filter(d => d.name === name && d.lastName === lastName).length
const people = [
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Jacob', lastName: 'Smith', dob: '1991-08-21' },
{ name: 'Ann', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Nansen', dob: '1983-01-01' },
{ name: 'Jacob', lastName: 'Smith', dob: '1985-06-15' },
{ name: 'Jacob', lastName: 'Smith', dob: '1995-11-29' },
{ name: 'Ann', lastName: 'Smith', dob: '2010-11-29' },
]
const groupedPeople = people.reduce((group, person, i, people) => {
const alreadyBeenGrouped = group.some(({ name, lastName }) => name === person.name && lastName === person.lastName)
if (!alreadyBeenGrouped) {
group.push({
name: person.name,
lastName: person.lastName,
count: getInstances(person, people)
})
}
return group
}, [])
console.log(groupedPeople)

Related

Efficiently Combining Multiple Array Method Transformations in Javascript

I have a working program that satisfies the conditions below but I'm wondering if there is a more efficient solution. Currently, I apply 4 different Javascript array method transformations which results in returning a new array for each transformation for a total of 4 new arrays. Can these transformations be combined to only create 1 additional array instead of 4 new arrays? I could chain together the calls but I was thinking it might be possible to combine them all into reduce() method but I'm not sure how that would look or if there is some better solution.
The criteria that needs to be satisfied:
Only include employees from the Google organization but allow this to be passed as an input parameter
Last names should be unique (no duplicate last names)
Employees should be sorted by ID (ascending)
Each employee should have an added property called fullName that is a combination of first and last names, separated by a space
const GOOGLE_ORG = 'Google';
const employees = [
{
id: 3,
firstName: 'John',
lastName: 'Doe',
organization: 'Google',
},
{
id: 7,
firstName: 'Jake',
lastName: 'Smith',
organization: 'Google',
},
{
id: 1,
firstName: 'Jane',
lastName: 'Doe',
organization: 'Google',
},
{
id: 2,
firstName: 'Vanessa',
lastName: 'Smith',
organization: 'Meta',
},
{
id: 5,
firstName: 'Sarah',
lastName: 'Hernandez',
organization: 'Meta',
},
{
id: 8,
firstName: 'Jessica',
lastName: 'Morales',
organization: 'Google',
},
{
id: 4,
firstName: 'Paul',
lastName: 'Stark',
organization: 'Google',
},
{
id: 6,
firstName: 'Peter',
lastName: 'Brown',
organization: 'Meta',
},
];
const transformArray = (org) => {
const filteredByOrg = employees.filter((employee) => employee.organization === org);
const addedFullName = filteredByOrg.map((employee) => ({
...employee,
fullName: employee.firstName + ' ' + employee.lastName,
}));
const uniqueByLastName = [...addedFullName.reduce((map, obj) => map.set(obj.lastName, obj), new Map()).values()];
return uniqueByLastName.sort((a, b) => a.id - b.id);
};
transformArray(GOOGLE_ORG);
Yes, you can use Array.prototype.reduce to minimize creating arrays and the number of iterations.
const GOOGLE_ORG = 'Google';
const employees = [
{
id: 3,
firstName: 'John',
lastName: 'Doe',
organization: 'Google',
},
{
id: 7,
firstName: 'Jake',
lastName: 'Smith',
organization: 'Google',
},
{
id: 1,
firstName: 'Jane',
lastName: 'Doe',
organization: 'Google',
},
{
id: 2,
firstName: 'Vanessa',
lastName: 'Smith',
organization: 'Meta',
},
{
id: 5,
firstName: 'Sarah',
lastName: 'Hernandez',
organization: 'Meta',
},
{
id: 8,
firstName: 'Jessica',
lastName: 'Morales',
organization: 'Google',
},
{
id: 4,
firstName: 'Paul',
lastName: 'Stark',
organization: 'Google',
},
{
id: 6,
firstName: 'Peter',
lastName: 'Brown',
organization: 'Meta',
},
];
const transformArray = (org) => {
return (employees.reduce((lastNames => ((acc, curr) => {
if(!lastNames.has(curr.lastName) && curr.organization === org) {
lastNames.add(curr.lastName)
acc.push({...curr, fullName: `${curr.firstName} ${curr.lastName}`})
}
return acc;
}))(new Set()), []).sort((a, b) => a.id - b.id));
};
console.log(transformArray(GOOGLE_ORG));
You could chain all operations.
Actually it returns a different reuslt by filtering unique lastnames and sorting after or by sorting first and then filtering.
An example of a closure:
Take this line for filtering with a Set:
.filter((s => ({ lastName }) => !s.has(lastName) && s.add(lastName))(new Set))
A closure takes variables into the scope of the calling function and returns another function. This function has access to the variable.
Let's have a look with a different formatting:
.filter(
(s => ({ lastName }) => !s.has(lastName) && s.add(lastName))
(new Set)
)
Here, the function is called with new Set and this function is returned
({ lastName }) => !s.has(lastName) && s.add(lastName)
as callback for the filter method.
const
GOOGLE_ORG = 'Google',
employees = [{ id: 3, firstName: 'John', lastName: 'Doe', organization: 'Google' }, { id: 7, firstName: 'Jake', lastName: 'Smith', organization: 'Google' }, { id: 1, firstName: 'Jane', lastName: 'Doe', organization: 'Google' }, { id: 2, firstName: 'Vanessa', lastName: 'Smith', organization: 'Meta' }, { id: 5, firstName: 'Sarah', lastName: 'Hernandez', organization: 'Meta' }, { id: 8, firstName: 'Jessica', lastName: 'Morales', organization: 'Google' }, { id: 4, firstName: 'Paul', lastName: 'Stark', organization: 'Google' }, { id: 6, firstName: 'Peter', lastName: 'Brown', organization: 'Meta' }],
result = employees
.filter(({ organization }) => organization === GOOGLE_ORG)
.filter((s => ({ lastName }) => !s.has(lastName) && s.add(lastName))(new Set))
.sort((a, b) => a.id - b.id)
.map(o => ({ ...o, fullName: o.firstName + o.lastName }));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to get same values from 2 arrays with objects in ES6 Javascript?

So I have here two different arrays.
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
What I want is to get the same values from the two arrays using ES6 (filter if possible) or in any way that fits to achieve the output. The result that I want to get is :
[{ firstName: "John", lastName: "Doe" }]
You can try using Array.prototype.filter() and Array.prototype.some()
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
const res = test.filter(t1 => test2.some(t2 =>
t1.firstName == t2.firstName
&& t1.lastName == t2.lastName
));
console.log(res);
Thi swill still works if you have object with properties other than firstName and lastName
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
const tempTest2 = test2.map(item => JSON.stringify(item));
const result = test.filter(item => tempTest2.includes(JSON.stringify(item)));
console.log(result);
You can do:
const test = [{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Michael', lastName: 'Sins' },{ firstName: 'Alex',lastName: 'Brown' }]
const test2 = [{ firstName: 'Lisa', lastName: 'Shore' },{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Justin',lastName: 'Park' }]
const getFullName = ({ firstName, lastName }) => firstName + lastName
const test2Names = test2.map(getFullName)
const result = test.filter((o) => test2Names.includes(getFullName(o)))
console.log(result)

lodash find returns only one result

I'm using lodash find, and on my test, it is only returning one result, is this the expected response? how to find all instances?
var users = [
{ firstName: "John", lastName: "Doe", age: 28, gender: "male" },
{ firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
{ firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
{ firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
];
var encontre = _.find(users, { lastName: "Doe" })
console.log("usuario encontre::", encontre)
response
usuario encontre:: { firstName: 'John', lastName: 'Doe', age: 28,
gender: 'male' }
so how to seethe 2 users with lastName: Doe?
thanks
Try with _.filter as _.find returns the first matched element.
_.filter will return an array of all matched elements.
var users = [
{ firstName: "John", lastName: "Doe", age: 28, gender: "male" },
{ firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
{ firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
{ firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
];
var encontre = _.filter(users, { lastName: "Doe" })
console.log("usuario encontre::", encontre)
jsfiddle for ref : https://jsfiddle.net/c_Dhananjay/b6ngxhvp/

array of object into array of object but different (javascript)

Using javascript, I have this array:
people = [{name: carlo, lastname: Garcia, age: 28},
{name: maria, lastname: pia, age: 20},
{name: elly, lastname: martinez, age: 25}]
I would like to transform it into:
arrayList = [{data: {name: carlo, lastname: garcia, age: 28}, checked: false},
{data: {name: carlo, lastname: garcia, age: 28}, checked: false},
{data: {name: carlo, lastname: garcia, age: 28}, checked: false}]
How can I do it?
You want to use Array.prototype.map
var people = [{ name: "carlo", lastname: "Garcia", age: 28 }, { name: "maria", lastname: "pia", age: 20 }, { name: "elly", lastname: "martinez", age: 25 }]
var arrayList = people.map(person => ({
data: person,
checked: false,
}))
console.log(arrayList);
Just use map(). Return an object with data and checked fields.
Where:
data is set to each person
checked is set to false
people = [{name: 'carlo', lastname: 'Garcia', age: 28},{name: 'maria', lastname: 'pia', age: 20},{name: 'elly', lastname: 'martinez', age: 25}]
arrayList = people.map(p => ({ data: p, checked: false }))
console.log(arrayList)

create Json with dynamic index

I'd like create json with this structure inside the cycle:
{ id_foto:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ] }
I wish it were a variable id_foto
so that:
if (id_foto == n.foto_moderata) {
// add new { firstName: 'Anna', lastName: 'Smith' }
} else {
// create new "node" like
{ id_foto(NEW INDEX):
[ { firstName: 37, lastName: 'Doe' },] }
}
The Final result like:
{ 10:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ]
11:
[ { firstName: fff, lastName: 'fff' },
{ firstName: 'fff', lastName: 'fff' } ]
}
Then take all user of 11 index
One way to achieve a sequential ids for your data is to create:-
1. a place to store the current value of your id,
2. a function to increment and return your serial
You could store the current id value in your data object like so:-
{
seq : 11,
10:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ],
11:
[ { firstName: fff, lastName: 'fff' },
{ firstName: 'fff', lastName: 'fff' } ]
}
and then use the following to increment & return next sequence id
function id_foto() {
return ++your_object.seq;//get,increment and return incremented value
}

Categories

Resources