How to get unique Id for multiple duplicate properties - javascript

say we have a object:
var db = [
{Id: "201" , Player: "Jon",price: "3.99", loc: "NJ" },
{Id: "202", Player: "Sam",price: "4.22", loc: "PA" },
{Id: "203" ,Player: "Sam",price: "4.22", loc: "NY" },
{Id: "204", Player: "Bill",price: "3.22", loc: "TX" },
{Id: "205" ,Player: "Dave",price: "3.99", loc: "WA" },
{Id: "206" ,Player: "Dave",price: "3.99", loc: "WI" },
];
202&203 and 205&206 have similar values for player and price but I need just one id for similar values i.e, output should be 202,205.
Can someone help me with that.

You could filter it with a hash table for look up for the same player and price values.
var db = [{ Id: "201", Player: "Jon", price: "3.99", loc: "NJ" }, { Id: "202", Player: "Sam", price: "4.22", loc: "PA" }, { Id: "203", Player: "Sam", price: "4.22", loc: "NY" }, { Id: "204", Player: "Bill", price: "3.22", loc: "TX" }, { Id: "205", Player: "Dave", price: "3.99", loc: "WA" }, { Id: "206", Player: "Dave", price: "3.99", loc: "WI" }],
filtered = db.filter(function (a) {
var key = a.Player + '|' + a.price;
if (!this[key]) {
this[key] = true;
return true;
}
}, Object.create(null))
console.log(filtered);

I think this could be a summarized answer:
var db = [{ Id: "201", Player: "Jon", price: "3.99", loc: "NJ" }, { Id: "202", Player: "Sam", price: "4.22", loc: "PA" }, { Id: "203", Player: "Sam", price: "4.22", loc: "NY" }, { Id: "204", Player: "Bill", price: "3.22", loc: "TX" }, { Id: "205", Player: "Dave", price: "3.99", loc: "WA" }, { Id: "206", Player: "Dave", price: "3.99", loc: "WI" }];
var result = db.filter((v, k) =>
(k = v.Player + v.price) in this ? false :(this[k] = true));
console.log(result);

Related

JS - manipulate variables on Object.assign

How can I manipulate variables inside - map.set(key, Object.assign({}, item));.
Array for example -
const arr = [
{
name: "daniel",
sum: "$100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "$100",
},
{
name: "daniel",
sum: "100",
},
];
desired output -
const arr = [
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
{
name: "daniel",
sum: "100",
},
];
How can I split for the example inside the assign?

MongoDB - How to unwind object of array

I am trying to manipulate a dataset to make it easy to display in mongoCharts. There are two sets of team_a and team_b, each contains a set of playerId, score, rank, and prize. I want to unwind the array inside the Object and also want to merge both arrays into one.
Sample Document:
{
team_a: {
team_score: 94,
team_name: "team_1",
players: [
{
id: "604f00d43776e45a448628f9",
username: "test_1",
score: "33",
rank: "1",
prize: 15.4,
},
{
id: "60058dd9b88cc1a1e40f2f54",
username: "test_2",
score: "31",
rank: "2",
prize: 15.4,
},
{
id: "60058dd9b88cc1a1e40f2f55",
username: "test_3",
score: "30",
rank: "3",
prize: 15.4,
}
],
},
team_b: {
team_score: 62,
team_name: "team_2",
players: [
{
id: "602ce34a39c7496600940774",
username: "test_4",
score: "32",
rank: "1",
},
{
id: "60058db6b88cc1a1e40f2f4f",
username: "test_5",
score: "30",
rank: "2",
},
],
},
}
And the desired output is:
{
team_a: [
{
username: "test_1",
score: "33",
rank: "1",
prize: 15.4,
},
{
username: "test_2",
score: "31",
rank: "2",
prize: 15.4,
},
{
username: "test_3",
score: "30",
rank: "3",
prize: 15.4,
}
],
team_b: [
{
username: "test_4",
score: "32",
rank: "1",
},
{
username: "test_5",
score: "30",
rank: "2",
},
],
all_winners: [
{
username: "test_1",
score: "33",
rank: "1",
prize: 15.4,
},
{
username: "test_2",
score: "31",
rank: "2",
prize: 15.4,
},
{
username: "test_3",
score: "30",
rank: "3",
prize: 15.4,
}
{
username: "test_4",
score: "31",
rank: "4",
},
{
username: "test_5",
score: "30",
rank: "5",
},
]
}
Any guidance or pointers are gratefully received.
Thanks.
Solution 1
1st $project stage:
team_a field with team_a.players
team_b field with team_b.players
all_winners field with $concatArrays for team_a.players and team_b.players
2nd $project stage:
Remove id field from team_a, team_b, all_winners array fields.
db.collection.aggregate([
{
$project: {
"team_a": "$team_a.players",
"team_b": "$team_b.players",
"all_winners": {
"$concatArrays": [
"$team_a.players",
"$team_b.players"
]
}
}
},
{
$project: {
"all_winners": {
id: 0
},
"team_a": {
id: 0
},
"team_b": {
id: 0
}
}
}
])
Sample Mongo Playground (Solution 1)
Solution 2
Alternative, use $unset to remove team_a.players.id and team_b.players.id as first stage.
db.collection.aggregate([
{
$unset: [
"team_a.players.id",
"team_b.players.id"
]
},
{
$project: {
"team_a": "$team_a.players",
"team_b": "$team_b.players",
"all_winners": {
"$concatArrays": [
"$team_a.players",
"$team_b.players"
]
}
}
}
])
Sample Mongo Playground (Solution 2)

Compare two arrays of objects, where objects in each array have different properties

I have two result sets like this:
const resultSet1 =
[
{
"id": "1",
"version": "3",
"website": "https://xx/version/3",
"name": Ana,
"lastName": Ana,
},
{
"id": "2",
"version": "3",
"website": "https://xx/version/3",
"name": Ana,
"lastName": Ana,
}
]
const resultSet2 =
[
{
"id": "1",
"version": "2",
"birthday": "24.08.1984",
"place": "Europe",
},
{
"id": "2",
"version": "2",
"birthday": "24.08.1984",
"place": "Europe",
},
{
"id": "1",
"version": "1",
"birthday": "24.08.1984",
"place": "Europe",
},
{
"id": "2",
"version": "3",
"birthday": "24.08.1984",
"place": "Europe",
}
]
I want to compare these two result sets, based on id & version. In my const comparisonSet, I want to have elements from the first result set, whose both id & version are not present in the second result set.
const comparisonSet =
[
{
"id": "1",
"version": "3",
"website": "https://xx/version/3",
"name": Ana,
"lastName": Ana,
}
]
How can I achieve this in Javascript?
Any help would be appreciated. Thank you in advance!
You can use filter to get the desired result.
Overall complexity - O(n * 2)
resultSet1.filter(({ id, version }) =>!resultSet2.find((o) => o.id === id && o.version === version));
const resultSet1 = [{
id: "1",
version: "3",
website: "https://xx/version/3",
name: "Ana",
lastName: "Ana",
},
{
id: "2",
version: "3",
website: "https://xx/version/3",
name: "Ana",
lastName: "Ana",
},
];
const resultSet2 = [{
id: "1",
version: "2",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "2",
version: "2",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "1",
version: "1",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "2",
version: "3",
birthday: "24.08.1984",
place: "Europe",
},
];
const result = resultSet1.filter(
({
id,
version
}) =>
!resultSet2.find((o) => o.id === id && o.version === version)
);
console.log(result);
Though it is not so optimized, so you can also create a dictionary and loop up result in O(1) -
Overall complexity O(n)
const dict = resultSet2.reduce((acc, curr) => {
const { id, version } = curr;
acc[`${id}|${version}`] = curr;
return acc;
}, {});
const result = resultSet1.filter(({ id, version }) => !dict[`${id}|${version}`]);
const resultSet1 = [
{
id: "1",
version: "3",
website: "https://xx/version/3",
name: "Ana",
lastName: "Ana",
},
{
id: "2",
version: "3",
website: "https://xx/version/3",
name: "Ana",
lastName: "Ana",
},
];
const resultSet2 = [
{
id: "1",
version: "2",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "2",
version: "2",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "1",
version: "1",
birthday: "24.08.1984",
place: "Europe",
},
{
id: "2",
version: "3",
birthday: "24.08.1984",
place: "Europe",
},
];
const dict = resultSet2.reduce((acc, curr) => {
const { id, version } = curr;
acc[`${id}|${version}`] = curr;
return acc;
}, {});
const result = resultSet1.filter(({ id, version }) => !dict[`${id}|${version}`]);
console.log(result);
I would iterate through one array while filtering the other.
resultSet1.forEach(res1 => {
const filtered = resultSet2.filter(res2 => res2.id === res1.id && res2.version === res1.version);
//if filtered.length === 0 you can do what you want with res1
});
const Ana = 'Ana';
const resultSet1 = [
{ id: '1', version: '3', website: 'https://xx/version/3', name: Ana, lastName: Ana, },
{ id: '2', version: '3', website: 'https://xx/version/3', name: Ana, lastName: Ana, },
];
const resultSet2 = [
{ id: '1', version: '2', birthday: '24.08.1984', place: 'Europe', },
{ id: '2', version: '2', birthday: '24.08.1984', place: 'Europe', },
{ id: '1', version: '1', birthday: '24.08.1984', place: 'Europe', },
{ id: '2', version: '3', birthday: '24.08.1984', place: 'Europe', },
];
const idAndVersionNotInSecondResultSet = ({ id, version }) =>
resultSet2.every(({ id: i, version: v }) => i !== id || version !== v);
const comparisonSet = resultSet1.filter(idAndVersionNotInSecondResultSet);
console.log(comparisonSet);

Update object value in array with values in other array using hashtables or hashmap

I have two arrays Array1 and Array2, i am updating rate of object in Array1 with rate of same object (With same ID) in Array 2. I have a functions that loops through both arrays to get desired result. After going through some of the answers on Stack overflow I feel Hash table is best suited to reduce the complexity. I was just curious to understand how same can be implemented using the has maps.
let Array1 = [{
id: 1,
name: "IceCream",
details: [{
id: "12",
name: "milk",
quantity: "50",
rate: "100"
},
{
id: "13",
name: "cream",
quantity: "50",
rate: "300"
}
]
},
{
id: 2,
name: "Coffee",
details: [{
id: "14",
name: "Coffee bean",
quantity: "60",
rate: "200"
},
{
id: "15",
name: "water",
quantity: "60",
rate: "300"
}
]
},
{
id: 3,
name: "Tea",
details: [{
id: "16",
name: "Tea leaf",
quantity: "50",
rate: "700"
}]
}
]
let Array2 = [{
id: 1,
name: "IceCream",
details: [{
id: "12",
name: "milk",
quantity: "50",
rate: "500"
},
{
id: "13",
name: "cream",
quantity: "50",
rate: "700"
}
]
},
{
id: 2,
name: "Coffee",
details: [{
id: "14",
name: "Coffee bean",
quantity: "60",
rate: "800"
},
{
id: "15",
name: "water",
quantity: "60",
rate: "8000"
}
]
}
]
Array1 = Array1.map(item => {
let element = Array2.find(e => e.id == item.id);
if (element) {
item.details = item.details.map(e => {
let detail = element.details.find(d => d.id == e.id);
if (detail)
e.rate = detail.rate;
return e;
});
}
return item;
});
console.log(Array1);
Make a map of Array2's items (by id) and each of Array2's details (by id), and then you can iterate over Array1 and mutate its properties with low complexity:
const items2ById = {};
for (const item of Array2) {
items2ById[item.id] = item;
}
const items2DetailsById = {};
for (const detail of Array2.flatMap(({ details }) => details)) {
items2DetailsById[detail.id] = detail;
}
for (const item of Array1) {
if (!items2ById[item.id]) continue;
for (const detail of item.details) {
if (items2DetailsById[detail.id]) {
detail.rate = items2DetailsById[detail.id].rate;
}
}
}
Note that since you're mutating the existing objects, .map isn't really appropriate, since you don't really care to create a new array - instead, just iterate over the array and mutate it as needed.
let Array1 = [{
id: 1,
name: "IceCream",
details: [{
id: "12",
name: "milk",
quantity: "50",
rate: "100"
},
{
id: "13",
name: "cream",
quantity: "50",
rate: "300"
}
]
},
{
id: 2,
name: "Coffee",
details: [{
id: "14",
name: "Coffee bean",
quantity: "60",
rate: "200"
},
{
id: "15",
name: "water",
quantity: "60",
rate: "300"
}
]
},
{
id: 3,
name: "Tea",
details: [{
id: "16",
name: "Tea leaf",
quantity: "50",
rate: "700"
}]
}
]
let Array2 = [{
id: 1,
name: "IceCream",
details: [{
id: "12",
name: "milk",
quantity: "50",
rate: "500"
},
{
id: "13",
name: "cream",
quantity: "50",
rate: "700"
}
]
},
{
id: 2,
name: "Coffee",
details: [{
id: "14",
name: "Coffee bean",
quantity: "60",
rate: "800"
},
{
id: "15",
name: "water",
quantity: "60",
rate: "8000"
}
]
}
];
const items2ById = {};
for (const item of Array2) {
items2ById[item.id] = item;
}
const items2DetailsById = {};
for (const detail of Array2.flatMap(({ details }) => details)) {
items2DetailsById[detail.id] = detail;
}
for (const item of Array1) {
if (!items2ById[item.id]) continue;
for (const detail of item.details) {
if (items2DetailsById[detail.id]) {
detail.rate = items2DetailsById[detail.id].rate;
}
}
}
console.log(Array1);

json object handling in JS

My given json object is as follows:
resultTable = [
{
id: "005470021180",
balance: "0.00",
accountName: "Money Master",
category: "Banking",
currency: "CAD",
entitlements: [
]
},
{
id: "005470021288",
balance: "0.00",
accountName: "Money Master",
category: "Banking",
currency: "CAD"
},
{
id: "005470044628",
balance: "-72,116.01",
accountName: "Money Master",
category: "Banking",
currency: "CAD"
},
{
id: "40592000495201",
accountName: "Business Loan",
category: "Borrowing",
cad_balance: "0.00"
},
{
id: "40592000495202",
accountName: "Business Loan",
category: "Borrowing",
cad_balance: "0.00"
},
{
id: "40592000495203",
accountName: "Business Loan",
category: "Investing",
cad_balance: "0.00"
},
{
id: "40592000495204",
accountName: "Business Loan",
category: "INVESTING",
cad_balance: "0.00"
},
{
usd_balance: "1,080.27",
id: "55300070",
accountName: "Scotia iTRADE",
category: "INVESTING",
cad_balance: "272,166.59"
}
];
I need an output in JS as :
resulttable = [
{
"Banking": {
id: "005470021180",
balance: "0.00",
accountName: "Money Master",
category: "Banking",
currency: "CAD",
entitlements: [
]
},
{
id: "005470021288",
balance: "0.00",
accountName: "Money Master",
category: "Banking",
currency: "CAD"
},
{
id: "005470044628",
balance: "-72,116.01",
accountName: "Money Master",
category: "Banking",
currency: "CAD"
}
},
{
"Borrowing": {
id: "40592000495201",
accountName: "Business Loan",
category: "Borrowing",
cad_balance: "0.00"
},
{
id: "40592000495202",
accountName: "Business Loan",
category: "Borrowing",
cad_balance: "0.00"
}
},
{
""INVESTING":{
{
id : "40592000495203",
accountName : "BusinessLoan",
category : "Investing",
cad_balance : "0.00"
},
{
id : "40592000495204",
accountName : "BusinessLoan",
category : "INVESTING",
cad_balance : "0.00"
},
{
usd_balance : "1,080.27",
id : "55300070",
accountName : "ScotiaiTRADE",
category : "INVESTING",
cad_balance : "272,166.59"
}} ];
Try something like this:
function (input)
{
var output = new Object();
for(var i = 0, len=input.length; i<len; i++)
{
if(typeof output[input[i].category] === 'undefined')
{
output[input[i].category] = [];
}
output[input[i].category].push(input[i]);
}
return output;
}
input would be your starting object, and it returns the output in the structure you want.
Or at least what I think you'd wanted.

Categories

Resources