Javascript: Finding unmatched objects from 2 arrays - javascript

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

Related

Remove duplicate objects from an array with Keys

How to filter array of objects by property? for example in this array if two or more objects have same properties like name and lastname I want to remove either of them and leave only unique one in an array. example arr:
[ {name: "George", lastname: "GeorgeLast", age: 12},
{name: "George", lastname: "GeorgeLast", age: 13},
{name: "Bob", lastname: "GeorgeLast", age: 12}]
result should be either
[ {name: "George", lastname: "GeorgeLast", age: 13},
{name: "Bob", lastname: "GeorgeLast", age: 12}]
or
[ {name: "George", lastname: "GeorgeLast", age: 12},
{name: "Bob", lastname: "GeorgeLast", age: 12}]
Apply the technique shown in this answer, which is:
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
...but using findIndex with some criteria rather than just indexOf.
let people = [
{ name: "George", lastname: "GeorgeLast", age: 12 },
{ name: "George", lastname: "GeorgeLast", age: 13 },
{ name: "Bob", lastname: "GeorgeLast", age: 12 }
]
let result = people.filter(
(person, index) => index === people.findIndex(
other => person.name === other.name
&& person.lastname === other.lastname
));
console.log(result);
As for whether it keeps 12-year-old George or 13-year-old George, it is a matter of how findIndex works, which so happens to return the first matching element. So in your example case it will keep 12-year-old George.
The Map object holds key-value pairs and remembers the original insertion order of the keys.
const arr = [
{ name: "George", lastname: "GeorgeLast", age: 12 },
{ name: "George", lastname: "GeorgeLast", age: 13 },
{ name: "Bob", lastname: "GeorgeLast", age: 12 }
];
const newMap = new Map();
arr.forEach((item) => newMap.set(item.name, item));
console.log([...newMap.values()]);
Another solution.
Here you don't need to iterate through the list n*n/2 times (if I count correctly).
On the other hand, this one looks less concise and uses more memory.
Use whichever you prefer.
const arr = [
{name: "George", lastname: "GeorgeLast", age: 12},
{name: "George", lastname: "GeorgeLast", age: 13},
{name: "Bob", lastname: "GeorgeLast", age: 12}
];
const obj = {}
arr.forEach(v => {
if (!obj[v.name]) {
obj[v.name] = {}
}
if (!obj[v.name][v.lastname]) {
obj[v.name][v.lastname] = v;
}
})
const result = [];
Object.values(obj).forEach(nameObj =>
Object.values(nameObj).forEach(surnObj => result.push(surnObj))
);
console.log(result)

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)

How to sort on multiple columns using JavaScript sort?

I have following array that needs to be sorted with respect to search text 'John'.
{id: 1, firstName: 'User', lastName: 'John', nickName: 'Smith'},
{id: 2, firstName: 'Test', lastName: 'John', nickName: 'Andrew'},
{id: 3, firstName: 'Test', lastName: 'Zch', nickName: 'John'},
{id: 4, firstName: 'Test', lastName: 'Mason', nickName: 'John'},
{id: 5, firstName: 'John', lastName: 'Doe'},
];
Expected Output:
Array should be first sorted with nickName (with search text) then lastName(with search text). If nickName is not present then it should sorted with respect to firstName(with search text) with ASC sorting order.
Note: It should consider search text word as 'John'
This sort resembles like Search with Sort in your mobile's contact app
[
// sort with nickName as higher relevance considering search text as John
{id: 4, firstName: 'Test', lastName: 'Mason', nickName: 'John'},
{id: 3, firstName: 'Test', lastName: 'Zch', nickName: 'John'},
// sort with lastName considering search text
{id: 2, firstName: 'Test', lastName: 'John', nickName: 'Andrew'},
{id: 1, firstName: 'User', lastName: 'John', nickName: 'Smith'},
// sort with firstName as nickName is null
{id: 5, firstName: 'John', lastName: 'Doe'},
];
I tried localeMethod
function sortByLocale(user1, user2) {
var sortByNickName = user1.nickName.toLowerCase().localeCompare(user2.nickName.toLowerCase());
var sortByLastName = user1.lastName.toLowerCase().localeCompare(user2.lastName.toLowerCase());
return sortByNickName || sortByLastName;
}
But the result is not considering search text while sorting.
One approach, I can see is creating three different arrays and sort them and combined those sorted array
Any helps would be appreciated.
Edit: Not considering the non-matched object with search text value
Just add first search by name
function checkSearch (value) {
return (value.nickName === 'John') * -3 ||
(value.lastName === 'John') * -2 ||
(value.firstName === 'John') * -1 ||
0
}
function sortByLocale(user1, user2) {
var sortBySearch = checkSearch(user1) - checkSearch(user2)
var sortByNickName = (user1.nickName || '').toLowerCase().localeCompare((user2.nickName || '').toLowerCase());
var sortByLastName = user1.lastName.toLowerCase().localeCompare(user2.lastName.toLowerCase());
return sortBySearch || sortByNickName || sortByLastName;
}
You could take two iterations for the wanted order
one for the wanted string
for the order of the rest
var data = [{ id: 1, firstName: 'User', lastName: 'John', nickName: 'Smith' },
{ id: 2, firstName: 'Test', lastName: 'John', nickName: 'Andrew' },
{ id: 3, firstName: 'Test', lastName: 'Zch', nickName: 'John' },
{ id: 4, firstName: 'Test', lastName: 'Mason', nickName: 'John' },
{ id: 5, firstName: 'John', lastName: 'Doe' }
],
search = 'john',
check = (s => (o, k) => (o[k] || '').toLowerCase() === search)(search),
keys = ['nickName', 'lastName', 'firstName'];
data.sort((a, b) => {
const
fns = [
k => d = check(b, k) - check(a, k),
k => d = (a[k] || '').localeCompare(b[k] || '')
];
let d = 0;
fns.some(fn => keys.some(fn));
return d;
});
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

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)

Loop through an object and extract elements

I'm looping through object to extract firstName, lastName values and return an array that houses number of objects
here is desired output
let result = [ {firstName: 'Art', lastName: 'Blakey'},
{firstName: 'Jimmy', lastName: 'Cobb'},
{firstName: 'Elvin', lastName: 'Jones'},
{firstName: 'Max', lastName: 'Roach'},
{firstName: 'Tony', lastName: 'Williams'}];
Here is the source object
src = {
_id: "QdKbM9S55qHxuT7vA"
firstname: "John"
lastname: "Doe"
},
_id: "QdKbM9S55qHxuT7vA"
firstname: "Jane"
lastname: "Doe"
},
Here is what I have tried:
let myData = [];
_handleSearch() {
src.map((user) => {
myData.push({
firstname: user.firstname,
lastname: user.lastname
})
})
I'm not sure what I'm doing wrong here, the result is number of nested objects unlike the desired output
First you need to change src, it is not proper, object values should be separated by ,. Check this:
src = [{
_id: "QdKbM9S55qHxuT7vA",
firstname: "John",
lastname: "Doe"
},{
_id: "QdKbM9S55qHxuT7vA",
firstname: "Jane",
lastname: "Doe"
},{
_id: "QdKbM9S55qHxuT7vA",
firstname: "Jane",
lastname: "Doe"
}]
let result = src.map((item)=>{
return {firstname: item.firstname, lastname: item.lastname}
})
console.log(result);
Try this:
let myData = [];
_handleSearch() {
myData = src.map((user) => {
return {
firstname: user.firstname,
lastname: user.lastname
}
})

Categories

Resources