Is it possible to iterate over the array, excluding the first element (omit the first object in the array)?
CODE:
let multipleDemo =[];
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }
];
for(var i =0; i < people.length; i++) {
multipleDemo.push(people[i]);
people.splice(people[i], 1000);
console.log(multipleDemo);
console.log(people);
}
Example code: https://plnkr.co/edit/UJfRUs6dAT1NC1EnOvqA?p=preview
I want to leave { name: 'Adam', email: 'adam#email.com', age: 12, country: 'United States' } in array people. Rest of elements I want to put in array multipleDemo
I want such as FINISH EFFECT:
let multipleDemo =[,
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }];
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' }
];
You can use Array.prototype.slice() to modify your arrays to get your desired output.
let people = [{
name: 'Adam',
email: 'adam#email.com',
age: 12,
country: 'United States'
},
{
name: 'Amalie',
email: 'amalie#email.com',
age: 12,
country: 'Argentina'
},
{
name: 'Estefanía',
email: 'estefania#email.com',
age: 21,
country: 'Argentina'
},
{
name: 'Adrian',
email: 'adrian#email.com',
age: 21,
country: 'Ecuador'
},
{
name: 'Wladimir',
email: 'wladimir#email.com',
age: 30,
country: 'Ecuador'
},
{
name: 'Samantha',
email: 'samantha#email.com',
age: 30,
country: 'United States'
},
{
name: 'Nicole',
email: 'nicole#email.com',
age: 43,
country: 'Colombia'
},
{
name: 'Natasha',
email: 'natasha#email.com',
age: 54,
country: 'Ecuador'
},
{
name: 'Michael',
email: 'michael#email.com',
age: 15,
country: 'Colombia'
},
{
name: 'Nicolás',
email: 'nicolas#email.com',
age: 43,
country: 'Colombia'
}
];
let multipleDemo = people.slice(1);
people = people.slice(0, 1);
console.log(multipleDemo);
console.log('--------------------');
console.log(people);
You can use Array Destructuring to unpack and assign remaining part of the array to a variable using rest pattern and use .forEach() to iterate over them as follows:
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const [first, ...rest] = arr;
rest.forEach(v => console.log(v));
.as-console-wrapper { max-height: 100% !important; top: 0; }
There are many ways to achieve this. but the easiest and concise solution would be using the filter(). Which returns an array that contains each element where the condition is met.
let people = [
{ name: 'Adam', email: 'adam#email.com', age: 12,
country: 'United States' },
{ name: 'Amalie', email: 'amalie#email.com', age: 12,
country: 'Argentina' },
{ name: 'Estefanía', email: 'estefania#email.com', age: 21,
country: 'Argentina' },
{ name: 'Adrian', email: 'adrian#email.com', age: 21,
country: 'Ecuador' },
{ name: 'Wladimir', email: 'wladimir#email.com', age: 30,
country: 'Ecuador' },
{ name: 'Samantha', email: 'samantha#email.com', age: 30,
country: 'United States' },
{ name: 'Nicole', email: 'nicole#email.com', age: 43,
country: 'Colombia' },
{ name: 'Natasha', email: 'natasha#email.com', age: 54,
country: 'Ecuador' },
{ name: 'Michael', email: 'michael#email.com', age: 15,
country: 'Colombia' },
{ name: 'Nicolás', email: 'nicolas#email.com', age: 43,
country: 'Colombia' }
];
let multipleDemo = people.filter((v, k) => k !== 0);
people = people.filter((v, k) => k === 0);
console.log(multipleDemo)
console.log(people)
You can use .slice() to omit n elements from the beginning.
Array.slice(1) means you take the array starting from index 1 to the end.
You can also define until which element you want to slice off.
Array.slice(1, 3) will slice the elements of index 1 and 2. It will be Amalie and Estefanía in this case.
let people = [
{ name: "Adam", email: "adam#email.com", age: 12, country: "United States" },
{ name: "Amalie", email: "amalie#email.com", age: 12, country: "Argentina" },
{ name: "Estefanía", email: "estefania#email.com", age: 21, country: "Argentina" },
{ name: "Adrian", email: "adrian#email.com", age: 21, country: "Ecuador" },
{ name: "Wladimir", email: "wladimir#email.com", age: 30, country: "Ecuador" },
{ name: "Samantha", email: "samantha#email.com", age: 30, country: "United States" },
{ name: "Nicole", email: "nicole#email.com", age: 43, country: "Colombia" },
{ name: "Natasha", email: "natasha#email.com", age: 54, country: "Ecuador" },
{ name: "Michael", email: "michael#email.com", age: 15, country: "Colombia" },
{ name: "Nicolás", email: "nicolas#email.com", age: 43, country: "Colombia" }
];
let multipleDemo = people.slice(1);
multipleDemo.forEach(function (current) {
console.log(current.name);
});
Since you simply want to copy the elements from people array to multipleDemo array excluding the first element, you could use slice() array method.
multipleDemo = people.slice(1)
.slice(1) will copy the contents of people array from index1 without reference to the multipleDemo array.
.slice() in MDN
Related
Group array of objects from response.
[
{ name: ramesh, age: 12, city: mumbai },
{ name: ramesh, age: 12, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
];
I want to group like this.
[
{ name: ramesh, age: 12, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: amit, age: 13, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: ganesh, age: 14, city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
{ name: "", age: "", city: mumbai },
];
I tried with filter, reduce functions in Angular
need to find duplicate value and first value of duplicate will show exact and other duplicates value will convert in empty string
use the reduce method of arrays to create a new array of objects with the grouped data
const arr = [
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'ramesh', age: 12, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'amit', age: 13, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'},
{name: 'ganesh', age: 14, city: 'mumbai'}
];
const groupedArray = arr.reduce((acc, curr) => {
const existingObj = acc.find(obj => obj.name === curr.name && obj.age === curr.age && obj.city === curr.city);
if (existingObj) {
acc.push({name: '', age: '', city: curr.city});
} else {
acc.push(curr);
}
return acc;
}, []);
console.log(groupedArray);
I have 2 arrays with objects.
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
],
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
],
I need to get index from users1 of that parts that are contains in users2.
As a result I should get [{1: true}, {3: true}].
I try in this way but it's not correct
const arr = user1.filter((item, index) => user2.map(i) => {
if(i.id === item.id) return {index: true}
return null;
})
You could create a Set of all ids from users2 (a Set is somewhat like an array, but allows you to quickly check if a value is in it using .has() and it only stores unique values). Once you have the set, you can use .reduce() to create a new array, where you can use .concat() if the set has the id of the object from users1:
const users1 = [{ id: 1, name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' }, { id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 3, name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const users2 = [{ id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const u2Ids = new Set(users2.map(user => user.id));
const res = users1.reduce((acc, obj, i) => u2Ids.has(obj.id) ? acc.concat({[i]: true}) : acc, []);
console.log(res);
When creating your objects you need to ensure you use computed property names when adding the index as a key {[i]: true} so that the value at i is used rather than the literal i name. The above can also be written with a for..of loop (by accessing Array.prototype.entries()), which is more efficient and (in my opinion) more readable:
const users1 = [{ id: 1, name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' }, { id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 3, name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const users2 = [{ id: 2, name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' }, { id: 4, name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }, ];
const res = [];
const u2Ids = new Set(users2.map(user => user.id));
for(const [i, obj] of users1.entries()) {
if(u2Ids.has(obj.id))
res.push({[i]: true});
}
console.log(res);
First your array don't contains ids I filtered name for now.Also filter will not work here bcs if you use filter that will just filter your array and index will no longer be the same try following instead
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' }
];
const arr = [];
users1.forEach((item, idx) =>{
if(users2.some(u=> u.name == item.name)){
arr.push({[idx]:true})
}
});
console.log(arr)
Just another approach - with Array.reduce()
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const arr = users1.
reduce((previousValue, currentValue, index) => {
// change here to find by id
if (users2.some(u2 => u2.name === currentValue.name) ){
// map to whatever you want
const valueToSave = {[index] : true};
return [...previousValue, valueToSave];
} else {
return previousValue;
}
}, []);
console.log(arr);
#festina, to keep it simple we map on users2, as we are trying to find objects of users1 in users2, we get index using findIndex using (id/name any unique key available) and then filter by index > 0, and format the result in required format
const users1 = [
{ name: 'John', age: 15, gender: 'M', city: 'London', country: 'UK' },
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Jinny', age: 30, gender: 'F', city: 'London', country: 'UK' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
const users2 = [
{ name: 'Jack', age: 20, gender: 'M', city: 'New York', country: 'USA' },
{ name: 'Key', age: 15, gender: 'M', city: 'Leeds', country: 'UK' },
];
// your solution
const result = users2.map((user, index) => users1.findIndex(u => u.name === user.name))
.filter(index => index > 0)
.map((i => { return { [i]: true} }));
console.log(result)
I have an array of objects to be converted to an object with an array of unique values assigned to the same keys.
How can I get this output?
//input
let users = [
{ id: 1, name: "Alan", surname: "Davis", age: 34, isAdmin: 'yes'},
{ id: 2, name: "John", surname: "Doe", age: 35, isAdmin: 'no'},
{ id: 3, name: "Monica", surname: "Bush", age: 25, isAdmin: 'no'},
{ id: 4, name: "Sandra", surname: "Back", age: 23, isAdmin: 'no'},
{ id: 5, name: "Jacob", surname: "Front", age: 34, isAdmin: 'yes'},
];
//output
let unique = {
id: [1, 2, 3 ,4 ,5],
name: ['Alan', 'John', 'Monica', 'Sandra', 'Jacob'],
surname: ['Davis', 'Doe', 'Bush', 'Back', 'Front'],
age: [34, 35, 25, 23],
isAdmin: ['yes', 'no'],
}
You can use Array.prototype.reduce() and for all of the items iterate over with Array.prototype.forEach() and creating a unique array with Set
Code:
const users = [{ id: 1, name: 'Alan', surname: 'Davis', age: 34, isAdmin: 'yes' },{ id: 2, name: 'John', surname: 'Doe', age: 35, isAdmin: 'no' },{ id: 3, name: 'Monica', surname: 'Bush', age: 25, isAdmin: 'no' },{ id: 4, name: 'Sandra', surname: 'Back', age: 23, isAdmin: 'no' },{ id: 5, name: 'Jacob', surname: 'Front', age: 34, isAdmin: 'yes' },]
const unique = users.reduce((a, c) => (
Object
.entries(c)
.forEach(([k, v]) => a[k] = [...new Set([...(a[k] || []), v])]),
a
), {})
console.log(unique)
Here's one way
Iterate over the keys of the first object in the array to produce a list of keys
Create an array of the corresponding values. Filter the array for dupes.
Wrap the keys and values back into an object
let users = [
{ id: 1, name: "Alan", surname: "Davis", age: 34, isAdmin: 'yes'},
{ id: 2, name: "John", surname: "Doe", age: 35, isAdmin: 'no'},
{ id: 3, name: "Monica", surname: "Bush", age: 25, isAdmin: 'no'},
{ id: 4, name: "Sandra", surname: "Back", age: 23, isAdmin: 'no'},
{ id: 5, name: "Jacob", surname: "Front", age: 34, isAdmin: 'yes'},
];
let unique = Object.fromEntries(
Object.keys(users[0]).map(k =>
[k, users.map(u => u[k]).filter((value, i, arr) => arr.indexOf(value) === i)]
)
);
console.log(unique);
I am attempting to check whether all age groups from teen to centenarian are represented in an array.
My logic path has been:
Create an array with pre-defined decade-based age brackets [1, 2, 3, ... , 10]
Isolate the age value of each element in the sample array of objects
Divide those ages by 10 to get what decade the age falls in
parseInt that number to get an integer and put resultant figures in new array
Compare that array with the pre-defined age bracket array created initially.
The expected output is true but I am instead getting false.
Another logic issue I am facing is that the oldest age in the sample array, 128, reduces to 12 after dividing/parsing whereas it would be preferable for it to check if the int is > 10 instead of specifically 10.
Code below:
const devAges = list.map((list) => {
return list.age
})
const devAgesDiv = devAges.map(i => i / 10);
for(i = 0; i < devAgesDiv.length; i++){
devAgesDiv[i] = parseInt(devAgesDiv[i]);
}
function allAges(list) {
const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
return ageBrackets.every((ageBracket) =>
list.some((listItem) => listItem.ageBracket === ageBracket)
);
}
console.log(allAges(list)); // output false;
Sample array below:
var list = [
{ firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
{ firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
{ firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
{ firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
{ firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
{ firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
{ firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
{ firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];
You can make two updates to get the desired result:
devAgesDiv - check if the list age is over 100 and return 10 otherwise divide by 10; you can parseInt the result of that test
You can use includes in allAges to check every ageBracket is represented
See below:
const list = [
{ firstName: 'Harry', lastName: 'K.', country: 'Brazil', continent: 'Americas', age: 19, language: 'Python' },
{ firstName: 'Kseniya', lastName: 'T.', country: 'Belarus', continent: 'Europe', age: 29, language: 'JavaScript' },
{ firstName: 'Jing', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' },
{ firstName: 'Noa', lastName: 'A.', country: 'Israel', continent: 'Asia', age: 40, language: 'Ruby' },
{ firstName: 'Andrei', lastName: 'E.', country: 'Romania', continent: 'Europe', age: 59, language: 'C' },
{ firstName: 'Maria', lastName: 'S.', country: 'Peru', continent: 'Americas', age: 60, language: 'C' },
{ firstName: 'Lukas', lastName: 'X.', country: 'Croatia', continent: 'Europe', age: 75, language: 'Python' },
{ firstName: 'Chloe', lastName: 'K.', country: 'Guernsey', continent: 'Europe', age: 88, language: 'Ruby' },
{ firstName: 'Viktoria', lastName: 'W.', country: 'Bulgaria', continent: 'Europe', age: 98, language: 'PHP' },
{ firstName: 'Piotr', lastName: 'B.', country: 'Poland', continent: 'Europe', age: 128, language: 'JavaScript' }
];
const devAgesDiv = list.map(item => parseInt(item.age > 100 ? 10 : item.age / 10));
const ageBrackets = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function allAges(list) {
return ageBrackets.every(ageBracket => list.includes(ageBracket));
}
console.log(allAges(devAgesDiv)); // true
What is wrong in this if condition. I am getting the wrong result. I need to get equal values in these two objects and diff between them.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
for (let i = 0; i < firstArr.length; i++) {
for (let j = 0; j < secondArr.length; j++) {
if (firstArr[i].name == secondArr[j].name) {
console.log('eq', firstArr[i].city, secondArr[j].city)
}
if (firstArr[i].name != secondArr[j].name) {
console.log('not found in second array', firstArr[i].city)
}
if (secondArr[j].name != firstArr[i].name) {
console.log('not found in first array', secondArr[j].city)
}
}
}
Currently you compare each element of the first array with each element of the second array. You could instead use Array.prototype.some and Array.prototype.every to filter the arrays and to find the intersection resp. difference. Then you can map the objects to the city names.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
function intersect(lhs, rhs) {
return lhs.filter(el => rhs.some(el2 => el.name === el2.name)).map(el => el.city);
}
function diff(lhs, rhs) {
return lhs.filter(el => rhs.every(el2 => el.name !== el2.name)).map(el => el.city);
}
console.log(intersect(firstArr, secondArr));
console.log(diff(firstArr, secondArr));
console.log(diff(secondArr, firstArr));
I loop the first array first and find matches in second array. If there is a match, diff is displayed. If there is no such match, then the correct text is being displayed. An array is built along the way, which is used to simplify the loop on the second array.
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
let names = [];
for (let first of firstArr) {
let matches = secondArr.filter((second) => (first.name === second.name));
if (matches.length) {
console.log('eq', first.city, matches[0].city)
} else {
console.log('not found in second array', first.city);
}
names.push(first.name);
}
for (let second of secondArr) {
if (names.indexOf(second.name) === -1) console.log('not found in first array', second.city);
}
Try this:
const firstArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 23, city: 'Berlin' }, { name: 'Sara', age: 28, city: 'Paris' }, { name: 'Rash', age: 20, city: 'Dubai' } ];
const secondArr = [{ name: 'tom', age: 22, city: 'Madrid' }, { name: 'Alex', age: 27, city: 'Berlin' }, { name: 'Hary', age: 29, city: 'London' }, ];
var eq = [], uniqueInFirst = [], uniqueInSecond = [];
for (let i = 0; i < firstArr.length; i++) {
var secondArrCities = Object.values(secondArr).map ((obj) => {return obj.city})
if (secondArrCities.includes(firstArr[i].city)) {
eq.push(firstArr[i].city)
} else {
uniqueInFirst.push(firstArr[i].city)
}
}
for (let i = 0; i < secondArr.length; i++) {
var firstArrCities = Object.values(firstArr).map ((obj) => {return obj.city})
if (!firstArrCities.includes(secondArr[i].city)) {
uniqueInSecond.push(secondArr[i].city)
}
}
console.log(eq)
console.log(uniqueInFirst)
console.log(uniqueInSecond)