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/
Related
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 9 months ago.
Does someone know how to write this code?
On clicking the button, create a new table from people table, which will only contain people whose age is greater than 18 . Display the resulting array in the console
const people = [
{
firstname: "Dreddy",
lastname: "Nussgen",
age: 70,
},
{
firstname: "Yves",
lastname: "Sear",
age: 49,
},
{
firstname: "Marcel",
lastname: "Cowderay",
age: 59,
},
{
firstname: "Dag",
lastname: "Binnall",
age: 31,
},
{
firstname: "Horten",
lastname: "Claesens",
age: 75,
},
{
firstname: "Charmian",
lastname: "Harnes",
age: 10,
},
{
firstname: "Sean",
lastname: "Littlejohns",
age: 37,
},
{
firstname: "Hazlett",
lastname: "Sprouls",
age: 87,
},
{
firstname: "Marcel",
lastname: "Hasted",
age: 66,
},
{
firstname: "Cary",
lastname: "Summerson",
age: 15,
},
{
firstname: "Feodor",
lastname: "Ollett",
age: 56,
},
{
firstname: "Kelly",
lastname: "Ranahan",
age: 62,
},
{
firstname: "Madelene",
lastname: "Davie",
age: 14,
},
{
firstname: "Bent",
lastname: "Karpe",
age: 82,
},
{
firstname: "Reinaldo",
lastname: "Grimbleby",
age: 81,
},
];
You can use Array.prototype.filter() combined with Destructuring assignment
Code:
const people = [{firstname: 'Dreddy',lastname: 'Nussgen',age: 70,},{firstname: 'Yves',lastname: 'Sear',age: 49,},{firstname: 'Marcel',lastname: 'Cowderay',age: 59,},{firstname: 'Dag',lastname: 'Binnall',age: 31,},{firstname: 'Horten',lastname: 'Claesens',age: 75,},{firstname: 'Charmian',lastname: 'Harnes',age: 10,},{firstname: 'Sean',lastname: 'Littlejohns',age: 37,},{firstname: 'Hazlett',lastname: 'Sprouls',age: 87,},{firstname: 'Marcel',lastname: 'Hasted',age: 66,},{firstname: 'Cary',lastname: 'Summerson',age: 15,},{firstname: 'Feodor',lastname: 'Ollett',age: 56,},{firstname: 'Kelly',lastname: 'Ranahan',age: 62,},{firstname: 'Madelene',lastname: 'Davie',age: 14,},{firstname: 'Bent',lastname: 'Karpe',age: 82,},{firstname: 'Reinaldo',lastname: 'Grimbleby',age: 81,},]
const result = people.filter(({ age }) => age > 18)
console.log(result)
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)
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)
I want to get the first name and the gwa from each object, then arrange them according to their gwa from highest to lowest. I got the "getting the first name and gwa" part but I don't know how to arrange them according to their gwa.
I tried to use sort() but I seems to have used it in a wrong way. Can anyone please help me?
const students = [
{
firstName: "Jose",
lastName: "Valenci",
gender: "male",
gwa: 98,
interest: "tennis"
},
{
firstName: "Debby",
lastName: "Smith",
gender: "female",
gwa: 96,
interest: "singing"
},
{
firstName: "Zaira",
lastName: "Pay",
gender: "female",
gwa: 93,
interest: "crafts"
}];
const basedOnGwa = students.map ((name,grade) => {
let{lastName} = name;
let {gwa} = grade;
grade = grade.sort(grade1 => {
let {gwa} = grade1;
return grade1;
});
return grade();
})
console.log(basedOnGwa);
You can try the following code:
const students = [
{
firstName: "Jose",
lastName: "Valenci",
gender: "male",
gwa: 98,
interest: "tennis"
},
{
firstName: "Debby",
lastName: "Smith",
gender: "female",
gwa: 96,
interest: "singing"
},
{
firstName: "Zaira",
lastName: "Pay",
gender: "female",
gwa: 93,
interest: "crafts"
}];
var basedOnGwa = students.reduce((arr,v,i)=>{
return arr.concat({'firstName':v.firstName,'gwa':v.gwa});
},[]);
basedOnGwa.sort((a,b)=>{
return b.gwa - a.gwa;
})
console.log(basedOnGwa);
EDIT
If you want to use map instead of reduce you can follow the following code:
const students = [
{
firstName: "Jose",
lastName: "Valenci",
gender: "male",
gwa: 98,
interest: "tennis"
},
{
firstName: "Debby",
lastName: "Smith",
gender: "female",
gwa: 96,
interest: "singing"
},
{
firstName: "Zaira",
lastName: "Pay",
gender: "female",
gwa: 93,
interest: "crafts"
}];
var basedOnGwa = students.map((v,i)=>{
return {'firstName':v.firstName,'gwa':v.gwa};
});
basedOnGwa.sort((a,b)=>{
return b.gwa - a.gwa;
})
console.log(basedOnGwa);
My service give result as follows
$scope.ListOfPeople = [
{ PersonID: 10, FirstName: "John", LastName: "Smith", Sex: "Male" },
{ PersonID: 11, FirstName: "James", LastName: "Last", Sex: "Male" },
{ PersonID: 12, FirstName: "Mary", LastName: "Heart", Sex: "Female" },
{ PersonID: 13, FirstName: "Sandra", LastName: "Goldsmith", Sex: "Female" },
{ PersonID: 14, FirstName: "Shaun", LastName: "Sheep", Sex: "Male" },
{ PersonID: 15, FirstName: "Nicola", LastName: "Smith", Sex: "Male" }
];
I need to customise my datasource as follows
$scope.output= [
{ PersonID: 10, FirstName: "John" },
{ PersonID: 11, FirstName: "James" },
{ PersonID: 12, FirstName: "Mary" },
{ PersonID: 13, FirstName: "Sandra" },
{ PersonID: 14, FirstName: "Shaun" },
{ PersonID: 15, FirstName: "Nicola" }
];
What is the best way to do this in angularjs
You can use map to achieve your goal. Take in account that the map creates new array.
$scope.output = $scope.ListOfPeople.map(function(item){
return {
PersonID: item.PersonID,
FirstName: item.FirstName
};
});
I don't think you need to create a new array to reshape it's elements. Just delete unwanted properties from your elements.
//Iterate through the array
$scope.listOfPeople.forEach(function(obj){
//Iterate through properties
for (var property in obj) {
if (['PersonId', 'FirstName'].indexOf(property) < 0) //Delete anything you don't name here
delete obj[property];
}
})