Ordering in javascript - javascript

How easy/hard is it to order collections in javascript (alphabetical and numerically).
Say I have a collection like:
var map = {
user: { id: "23434", username: "mrblah" },
user: { id: "1010", username: "johnskeet" }
};
And I want to order the collection by id and username.
Update
correction thanks:
var map = [ { id: "23434", username: "mrblah" }, { id: "1010", username: "johnskeet" } ];

var map = {
users: [
{ id: "23434", username: "mrblah" },
{ id: "1010", username: "johnskeet" }
]
};
map.users.sort(function(a, b) {
return a.id - b.id;
});
map.users.sort(function(a, b) {
return a.username.localeCompare(b.username);
});

You want your object to be an array:
var map = [
{ id: "23434", username: "mrblah" },
{ id: "1010", username: "johnskeet" },
{ id: "1220", username: "alohaguy" }
];
We need a utility to display the usernames in order so we can test our work:
var displayUsernames = function(map) {
var out = [];
for (var i=0;i<map.length;i++) {
out.push((map[i].username));
}
alert(out.join(', '));
};
If we use it: displayUsernames(map); we get mrblah, johnskeet, alohaguy
Since it's an array, so we can use .sort(), like this: map.sort();, but if we do that we still get:
mrblah, johnskeet, alohaguy
...from displayUsernames(map); because the array of objects can't be sorted the same way as if it were an array of numbers or strings.
However, if we pass the sort() function a comparison function...
var myCompareFunction = function(a, b) {
return a.username.localeCompare(b.username);
};
Then pass it into map.sort()
map.sort(myCompareFunction);
Now, when we display it again with displayUsernames(map); we get alohaguy, johnskeet, mrblah
Hope that helps.

Related

Map the nested data from other table using promise and async-await

I need the expert advice for this code. I need to know Is there any better way to solve this.
I am using the mongoose for db. I have a dataset like this:
Below is matchTable:
{
_id: 617bc0113176d717f4ddd6ce,
car: [],
status: true
},
{
_id: 617bc0113176d717f4ddd6cg,
car: [
{
aid: '5c1b4ffd18e2d84b7d6febcg',
}
],
status: true
}
And I have a Car table in which car name is there on behalf of id
like this
{ _id: ObjectId('5c1b4ffd18e2d84b7d6febce'), name: 'ford' },
{ _id: ObjectId('5c1b4ffd18e2d84b7d6febcg'), name: 'mitsubishi' },
So I want to make join the data from car table, so that response get name on behalf of aid.
Desired result will be like
{
_id: 617bc0113176d717f4ddd6ce,
car: [],
status: true
},
{
_id: 617bc0113176d717f4ddd6cg,
car: [
{
aid: '5c1b4ffd18e2d84b7d6febcg',
name: 'mitsubishi'
}
],
status: true
}
For that I have to merge the car table on matchTable. I have done this but I want to give some suggestion that is there any better way to do or is it fine. I need expert advice.
const getData = await matchTable.find(
{ status: true }
).lean().exec();
let dataHolder = [];
await Promise.all (
getData.map(async x => {
await Promise.all(
x.car.map(async y => {
let data = await Car.findOne(
{ _id: ObjectId(y.aid) },
{ name: 1 }
).lean().exec();
y.name = '';
if (data) {
y.name = data.name;
}
})
)
// If I return { ...x }, then on response it will return {}, {} on car column
dataHolder.push(x) //So I have chosen this approach
})
);
Please guide me if any better and efficient solution is there. Thanks in advance
You can make use of aggregation here.
const pipeline = [
{
$match : { status : true }
},
{
$unwind: '$matchtable',
},
{
$lookup: {
from: "cars",
localField: "car.aid",
foreignField: "_id",
as: "matchcars"
}
},
{
$addFields: {
"car.carName": { $arrayElemAt: ["$matchcars.name", 0] }
}
},
{
$group: {
_id: "$_id",
cars: { $push: "$matchcars" }
}
}
]
const result = await matchTable.aggregate(pipeline).exec();
Please make sure, aid field inside car array (in matchTable collection) is an ObjectId because its being matched to _id (which is an ObjectId) inside cars collection.

Updated nested object by matching ID

I have an array with nested objects that I need to update from another array of objects, if they match.
Here is the data structure I want to update:
const invoices = {
BatchItemRequest: [
{
bId: "bid10",
Invoice: {
Line: [
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10110" },
},
},
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "11110" },
},
Amount: 2499,
},
],
},
},
{
bId: "bid10",
Invoice: {
Line: [
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10110" },
},
},
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10111" },
},
Amount: 2499,
},
],
},
},
],
};
Here is the array of objects I want to update it from:
const accounts = [
{ AccountCode: "10110", Id: "84" },
{ AccountCode: "11110", Id: "5" },
{ AccountCode: "10111", Id: "81" },
];
I want to update invoices, using accounts, by inserting Id if AccountCode matches, to get the following structure:
const invoices = {
BatchItemRequest: [
{
bId: "bid10",
Invoice: {
Line: [
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10110", Id: "84" },
},
},
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "11110", Id: "5" },
},
Amount: 2499,
},
],
},
},
{
bId: "bid10",
Invoice: {
Line: [
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10110", Id: "84" },
},
},
{
SalesItemLineDetail: {
ItemAccountRef: { AccountCode: "10111", Id: "81" },
},
Amount: 2499,
},
],
},
},
],
};
I have tried various methods, such as the following:
const mapped = invoices.BatchItemRequest.map((item1) => {
return Object.assign(
item1,
accounts.find((item2) => {
return item2 && item1.Invoice.Line.ItemAccountRef.AccountCode === item2.AccountCode;
})
);
});
Problem with this approach (it doesn't work as I think I need to do another nested map), but it also creates a new array, only including the nested elements of invoices.
Does anyone know a good approach to this?
This isn't the cleanest of code but it gets the job done:
function matchInvoiceWithAccount(invoices, accounts) {
const mappedInvoices = invoices.BatchItemRequest.map((request) => {
// Shouldn't modify input parameter, could use Object.assign to create a copy and modify the copy instead for purity
request.Invoice.Line = request.Invoice.Line.map((line) => {
const accountCode = line.SalesItemLineDetail.ItemAccountRef.AccountCode;
// If accounts was a map of AccountCode to Id you would't need to search for it which would be more effective
const account = accounts.find((account) => account.AccountCode === accountCode);
if (account) {
line.SalesItemLineDetail.ItemAccountRef.Id = account.Id;
}
return line;
});
return request;
});
return {
BatchItemRequest: mappedInvoices,
};
}
What you could and probably should do to improve this is to not modify the input parameters of the function, but that requires that you in a better way copy the original, either using Object.assign or spread operator.
At first, it will be good to create Map from your accounts array. We will go one time for array with O(n) and then will read ids by code with O(1). And nested fors is O(m*n), that will be much more slower at big arrays.
const idsByAccountCodes = new Map();
accounts.forEach((data) => {
idsByAccountCodes.set(data.AccountCode, data.Id);
})
or shorter:
const idsByAccountCode = new Map(accounts.map((data) => [data.AccountCode, data.Id]))
then if you want to mutate original values you can go through all nesting levels and add values
for ( const {Invoice:{ Line: line }} of invoices.BatchItemRequest){
for ( const {SalesItemLineDetail: {ItemAccountRef: item}} of line){
item.Id = idsByAccountCodes.get(item.AccountCode) || 'some default value'
// also if you don't have ids for all codes you need to define logic for that case
}
}
If you don't need to mutate original big object "invoices" and all of nested objects, then you can create recursive clone of if with something like lodash.cloneDeep

How to filter JSON data by properties that contain certain strings?

I have a JSON Object that looks like this:
{
'name': 'Bob',
'friends': [
{
'name' : 'Ashley (Family)'
},
{
'name' : 'Steven (Non-Family)'
},
{
'name' : 'Chris (Family)'
}
]
}
How can I filter the above, so that it returns only the friends that are family? i.e. friends who's name contains '(Family)'?
function filterFriends (friends) {
return friends.filter(function(i) {
if (i.name.indexOf('(Family)') > -1) {
return i.name;
}
});
}
But the above doesn't seem to work... I don't know if I'm on the right track?
Other than a) using the phrase "JSON Object" which makes no sense and b) relying on sloppy automatic casting of booleans, you really don't have a problem. This "answer", with minor technical improvements will demonstrate that your code is just fine.
var data = {
name: 'Bob',
friends: [
{
name: 'Ashley (Family)'
},
{
name: 'Steven (Non-Family)'
},
{
name: 'Chris (Family)'
}
]
};
var family = data.friends.filter(f => f.name.indexOf('(Family)') > -1);
console.log(family);
// [{name: 'Ashley (Family)'}, {name: 'Chris (Family)'}]
If you want to write it into a function
function isFamily(name) {
return name.indexOf('(Family)') > -1;
}
function getFamily(friends) {
return friends.filter(f => isFamily(f.name));
}
var family = getFamily(data.friends);
ES5
var family = data.friends.filter(function(f) {
return f.name.indexOf('(Family)') > -1);
});
console.log(family);
Filter method should always return boolean value, this looks like returning always the string with the name.
Take a look to docs for .filter method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Filter array of objects to a single matching object in mongodb/meteor

I have a query which looks as so:
var writer = Writers.findOne({
_id: writerId,
books: {
$elemMatch: {
id: Books.findOne({ slug: bookSlug })._id
}
}
});
However, this will return the full list of classes in the query.
{
name: "H.P. Lovecraft",
books: [{
id: "1234",
slug: "at-the-mountains-of-madness"
}, {
id: "5678",
slug: "herbert-west-reanimator"
}]
}
Would there be a way to eliminate all information except for the one item in the list I want and make it into an object? That is to say, I want my final result to be:
{
name: "H.P Lovecraft",
book: {
id: "1234",
slug: "herbert-west-reanimator"
}
}
How would this be done in Meteor with mongodb?
One approach you could take is to use the $elemMatch projection operator with the findOne() query. For the document with _id equal to writerId, the $elemMatch projection returns only the first matching element from the array:
var bookId = Books.findOne({ slug: bookSlug })._id,
writer = Writers.findOne({ _id: writerId },
{ books: { $elemMatch: { id: bookId } },
_id: 0,
name: 1
}
);
Another approach would be to use Underscore library's _.find() method to return the specific array element:
var bookId = Books.findOne({ slug: bookSlug })._id,
writer = Writers.findOne({
_id: writerId,
books: {
$elemMatch: {
id: bookId
}
}
}),
book = _.find(writer.books, function(book) {return book.id === bookId});

Angularjs array of nested objects from array

I have an array of position and nested user objects like so:
[
position: {
title: Developer,
user: { name: Albert }
},
position: {
title: CEO,
user: { name: Smith }
}
]
How do I get an array of [user, user] with Angularjs?
Assuming this is pseudo-code (as it's missing a few { and } and you'd need variables called Developer, Albert, CEO & Smith - I've fixed these in the working example) you can use Array.map:
var arr = [{
Position0: {
title: Developer,
user: { name: Albert }
},
{
Position1 : {
title: CEO,
user: { name: Smith }
}
}];
var result = arr.map(function(val, idx) {
return val["Position" + idx].user.name;
});
Working Example
I just tried this code, and works ...
var positions, users;
users = [];
positions = [
{
title: 'Developer',
user: { name: 'Albert' }
},
{
title: 'CEO',
user: { name: 'Smith' }
}
];
angular.forEach(positions, function(position) {
users.push(position.user);
});
console.log(users);
Firstly this thing does not need angularjs.
The way you can do that is using for each loop:
var users = [];
var positions = [{
Position0: {
title: 'Developer',
user: { name: 'Albert' }
},
Position1 : {
title: 'CEO',
user: { name: 'Smith' }
}
}]
for(var i =0; i< positions.length; i++){
for(var j in positions[i]){ //used to loop on object properties
if(positions[i].hasOwnProperty(j)){ //checking if the property is not inherited
if(positions[i][j].user){ //checking if the property exist, then only will push it to array, so that array doesnot have undefined values
users.push(positions[i][j].user);
}
}
}
}
users array will have the users.
Use angular.forEach():
var arr1=[]
var arr=[
position: {
title: Developer,
user: { name: Albert }
},
position: {
title: CEO,
user: { name: Smith }
}
];
angular.forEach(arr,function(value,key){
arr1[key]=value.position.user;
});
console.log(arr1);

Categories

Resources