Related
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)
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/
I have 2 arrays of objects
var arr1 = [{id: "145", firstname: "dave", lastname: "jones"},
{id: "135", firstname: "mike",lastname: "williams"},
{id: "148", firstname: "bob",lastname: "michaels"}];
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"},
{id: "135", firstname: "mike", lastname: "williams"},
{id: "148", firstname: "bob", lastname: "michaels"}];
I want to find the objects where the id exists in only one of the arrays and either log the object to the console or push the object to a new array.
Therefore I want to end up with
var arr1 = [{id: "145", firstname: "dave", lastname: "jones"}]
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"}]
I tried using a forEach loop and splicing matching id's out of the array
arr1.forEach(function(element1, index1) {
let arr1Id = element1.id;
arr2.forEach(function(element2, index2) {
if (arr1Id === element2.id) {
arr1.splice(element1, index1)
arr2.splice(element2, index2)
};
});
});
console.log(arr1);
console.log(arr2);
But I ended up with
arr1
[ { id: '135', firstname: 'mike', lastname: 'williams' },
{ id: '148', firstname: 'bob', lastname: 'michaels' } ]
arr2
[ { id: '135', firstname: 'mike', lastname: 'williams' },
{ id: '148', firstname: 'bob', lastname: 'michaels' } ]
You could take a Set for every array's id and filter the other array by checking the existence.
var array1 = [{ id: "145", firstname: "dave", lastname: "jones" }, { id: "135", firstname: "mike", lastname: "williams" }, { id: "148", firstname: "bob", lastname: "michaels" }],
array2 = [{ id: "146", firstname: "dave", lastname: "jones" }, { id: "135", firstname: "mike", lastname: "williams" }, { id: "148", firstname: "bob", lastname: "michaels" }],
set1 = new Set(array1.map(({ id }) => id)),
set2 = new Set(array2.map(({ id }) => id)),
result1 = array1.filter(({ id }) => !set2.has(id)),
result2 = array2.filter(({ id }) => !set1.has(id));
console.log(result1);
console.log(result2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Just use !arr.some() inside a Array.prototype.filter():
const arr1 = [{id: "145", firstname: "dave", lastname: "jones"},{id: "135", firstname: "mike",lastname: "williams"},{id: "148", firstname: "bob",lastname: "michaels"}],
arr2 = [{id: "146", firstname: "dave", lastname: "jones"},{id: "135", firstname: "mike", lastname: "williams"},{id: "148", firstname: "bob", lastname: "michaels"}],
newArr1 = arr1.filter(x => !arr2.some(y => y.id === x.id)),
newArr2 = arr2.filter(x => !arr1.some(y => y.id === x.id));
console.log(newArr1, newArr2);
Hello please try using combination of filter and findindex like the below snippet and let me know.
var arr1 = [{id: "145", firstname: "dave", lastname: "jones"},
{id: "135", firstname: "mike",lastname: "williams"},
{id: "148", firstname: "bob",lastname: "michaels"}];
var arr2 = [{id: "146", firstname: "dave", lastname: "jones"},
{id: "135", firstname: "mike", lastname: "williams"},
{id: "148", firstname: "bob", lastname: "michaels"}];
let unmatchedArr1 = arr1.filter(element => {
let targetIndex = arr2.findIndex(e => element.id === e.id);
return targetIndex >= 0 ? false : true;
})
let unmatchedArr2 = arr2.filter(element => {
let targetIndex = arr1.findIndex(e => element.id === e.id);
return targetIndex >= 0 ? false : true;
})
console.log(unmatchedArr1);
console.log(unmatchedArr2);
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'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
}