Filter unique values from a JSON - angularjs

I am using angularjs for my page. I want to filter the values from the JSON object, So that no redundancy exists. But I didn't find any way to get the unique values from angular ng-repeat. Is there anyway to do that?
Ok, Here is some description about the question. I have a JSON in this format. This JSON I am getting from a service. So we cant expect how the repeated data occure.
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
}
]
I want the output JSON to be with no repeated elements, So that my output will be something like this
result = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
}
]

You can make use of Angular UI which has the unique filter defined.
The source can be found here.
Basically, you can then make use of the filter as follows:
<div ng-repeat="item in result | unique:'_id'">
//Body here
</div>

You can use 'unique'(aliases: uniq) filter in angular.filter module (https://github.com/a8m/angular-filter)
usage: colection | uniq: 'property'
you can filter by nested properties to : colection | uniq: 'property.nested_property'
So you can do something like that..
function MainController ($scope) {
$scope.orders = [
{ id:1, customer: { name: 'foo', id: 10 } },
{ id:2, customer: { name: 'bar', id: 20 } },
{ id:3, customer: { name: 'foo', id: 10 } },
{ id:4, customer: { name: 'bar', id: 20 } },
{ id:5, customer: { name: 'baz', id: 30 } },
];
}
HTML: We filters by customer id, i.e remove duplicate customers
<th>All customers list: </th>
<tr ng-repeat="order in orders | unique: 'customer.id'" >
<td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>
result:
All customers list:
foo 10
bar 20
baz 30

var data = [
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
},
{
_id: "500005",
subject: "Attend to the event",
date: "2 Jan, 2013"
},
{
_id: "500065",
subject: "Some task deadline",
date: "20 Sep, 2013"
},
{
_id: "500004",
subject: "Complete the task",
date: "25 Aug, 2013"
}
]
var uniqueNames = [];
var uniqueObj = [];
for(i = 0; i< data.length; i++){
if(uniqueNames.indexOf(data[i]._id) === -1){
uniqueObj.push(data[i])
uniqueNames.push(data[i]._id);
}
}
console.log('uniqueObj',uniqueObj)
http://jsfiddle.net/C97DJ/165/

Related

Mongoose | Find objects inside of an array, that each object has another array of objects to satisfy condition

I have a collection Shops. Each object in Shops collection has an array of Item objects called items.
{
_id: ObjectId(...),
shopName: 'Ice cream Shop',
items: [
<Item>{
itemName: 'Chocolate IC',
availabilities: [
{
city: 'NY',
arrivals: [
{
price: 3.99,
quantityLeft: 0,
date: 'yesterday'
},
{
price: 3.99,
quantityLeft: 40,
date: 'today'
}
]
},
{
city: 'LA',
arrivals: []
}
]
},
<Item>{
itemName: 'Strawberry IC',
availabilities: [
{
city: 'NY',
arrivals: [
{
price: 3.99,
quantityLeft: 0,
date: 'yesterday'
},
]
}
]
},
],
},
... anotherShops
I want to get list of Item objects which has overall quantityLeft more than 0 from a specific shop.
I tried this code to get all items with the name start with "Straw" from a Shop with shopName equal to 'Ice cream Shop':
const items = await Shop.aggregate()
.match({
shopName: 'Ice cream Shop',
})
.project({
items: {
$filter: {
input: "$items",
as: "item",
cond: {
$regexMatch: {
input: "$$item.itemName",
regex: `.*Straw.*`,
},
},
},
},
});
And it works. But I don't know how to sum up all quantityLeft values inside availabilities array of each item, and return only that items that has sum more than 0.
availabilities array can be an empty array [].
The city parameter also needs to be in condition. For example, only Items that are in stock in NY
I need this to get the list of items from a certain shop, and only the items that are still in stock.
Pretty hard.
I came up with this solution. If you have a better solution, please post it.
const shop = await GCShop.aggregate([
{
$match: {
shopName: 'Ice Cream Shop',
},
},
{
$unwind: "$items",
},
{
$unwind: "$items.availabilities",
},
{
$unwind: "$items.availabilities.arrivals",
},
{
$group: {
_id: "$items.id",
items_name: { $first: "$items.name" },
arrivals: {
$push: {
arrival_id: "$items.availabilities.arrivals.arrival_id",
price: "$items.availabilities.arrivals.price",
qtty: "$items.availabilities.arrivals.qtty",
},
},
totalQtty: { $sum: "$items.availabilities.arrivals.qtty" },
},
},
{
$project: {
offer_id: "$_id",
_id: 0,
offer_name: 1,
totalQtty: 1,
arrivals: 1,
},
},
{
$match: {
totalQtty: {
$gt: 0,
},
},
},
]).limit(20);

How to query a date that have 20 or more documents in mongoDB in the same day and hour

I have a mongoDB database structure like this
{
_id: ObjectId,
name: string,
scheduledDate: ISOString
}
I want to return all scheduledDates that repeat the same scheduledDate day 2 times or more across all the database
Example:
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-15T05:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-15T07:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:20:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-18T02:44:00.000Z"
},
{
_id: ObjectId,
name: 'example1',
scheduledDate: "2022-04-10T05:44:00.000Z"
}
In this example 2022-04-15 repeats 2 times and 2022-04-18 repeat 3 times, so both match the criteria (2 times or more) so I want to return both date day
Is this possible?
Like this:
{
scheduledDate:"2022-04-15T00:00:00.000Z"
},
{
scheduledDate:"2022-04-18T00:00:00.000Z"
}
And one more question, is possible to do the same with hours? A list of specific hours of scheduledDate that repeat across all database X times
Use $group with $date and $dateTrunc
db.collection.aggregate([
{
$group: {
_id: {
$dateTrunc: {
date: { $toDate: "$scheduledDate" },
unit: "day"
}
},
count: { $sum: 1 }
}
},
{
$match: {
count: { $gt: 1 }
}
},
{
$project: {
_id: 0,
scheduledDate: "$_id"
}
}
])
mongoplayground

got error Expected an assignment or function call and instead saw an expression no-unused-expressions

I want to show full object including comments(which is the array of objects) in the div upon one single command but i got error on the map function of comments.please help me out from this ....
const fields2 = [
{
month: 30,
unitrate: 2,
budget: 6000,
comments: [
{
id: 4500,
rating: 11111111111,
comment: "Imagine all the eatables, living in conFusion!",
author: "John Lemon",
date: "2012-10-16T17:57:28.556094Z"
},
{
id: 1,
rating: 22222222222,
comment:
"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author: "Paul McVites",
date: "2014-09-05T17:57:28.556094Z"
},
]
}
];
render() {
return (
<div>
{ fields2.map((ob,index)=>{ return (ob.month +" " + ob.comments.map((ok,index)=>{ ok } )) }) }
</div>
)
}
you can use the below mentioned code
{ fields2.map((ob,index)=>{ return (ob.month +" " + JSON.stringify(ob.comments,null,4)) }) }

Using d3js sort an array of arrays

I have an array where each element in the array (representing a Supreme Court Chief Justice) contains an array of data points (the Chief Justice's name, number of disputes and year).
For each element in the top-level array, I need to sort the lower-level array in ascending order by year.
I attempted to sort year by ascending order using the following:
nested_data.forEach(function(d) {
d.values.sort(d3.ascending(d.values.year));
});
But it did not sort the lower-level array in ascending order by year.
Here is the console.log of my nested data to show its structure.
Array[5]
0:Object
key:"Vinson"
values:Array[7]
0:Object
chief:"Vinson"
disputes:142
year:Tue Jan 01 1946 00:00:00 GMT+0800 (CST)
1:Object
2:Object
3:Object
4:Object
5:Object
6:Object
1:Object
key:"Warren"
values:Array[16]
2:Object
key:"Burger"
values:Array[17]
3:Object
key:"Rehnquist"
values:Array[19]
4:Object
key:"Roberts"
values:Array[11]
How can I sort the lower-level array in ascending order by year?
This is the function:
nested_data.forEach(function(d) {
d.values.sort(function(a, b) {
return d3.ascending(+a.year, +b.year)
});
});
Your original function had two problems:
Since you're sorting the values arrays, you don't need to repeat values in the compareFunction
When specified, compareFunction uses two parameters, normally named a and b. You have to put d3.ascending inside compareFunction. Have a look here.
Here is a demo with a bogus data:
var data = [{
key: "foo",
values: [{
chief: "Bob",
year: 1982
}, {
chief: "Tom",
year: 1977
}, {
chief: "Carla",
year: 2010
}, {
chief: "Ana",
year: 1999
}]
}, {
key: "bar",
values: [{
chief: "Bill",
year: 2009
}, {
chief: "Ted",
year: 2014
}]
}, {
key: "baz",
values: [{
chief: "Fred",
year: 1998
}, {
chief: "Su",
year: 1992
}, {
chief: "Charlie",
year: 1999
}, {
chief: "Alice",
year: 1979
}]
}, ];
data.forEach(function(d) {
d.values.sort(function(a, b) {
return d3.ascending(+a.year, +b.year)
});
});
console.log(data);
<script src="https://d3js.org/d3.v4.min.js"></script>

MONGODB - adding a field in collection to a field in Item array

Suppose I have a document of the following prototype.
{
cust_id: "abc123",
ord_date: ISODate("2012-11-02T17:04:11.102Z"),
status: 'A',
price: 50,
items: [
{ sku: "xxx", qty: 25, price: 1 },
{ sku: "yyy", qty: 25, price: 1 }
]
}
My requirement is to get the total price for SKU "XXX" which is 50 * 25 (Price times quantity). How can I achieve this query's result in MONGODB?
db.YOUR_COLLECTION.aggregate({
$match: {
items.sku: "xxx"
},
$project: {
"product": {$multiply: ["$items.qty","$items.price"]},
_id: 0
}
});
I am able to achieve the solution to this by separating the query into two blocks as below.
var pipeline1 = [
{
{
"$unwind": "$items"
},
$match: {
items.sku: "xxx"
},
$project: {
"product":
{
$multiply: ["$items.qty","$items.price"]
},
_id: 0
}
}];
R = db.tb.aggregate( pipeline );

Resources