How to get array of unique objects with conditions? - javascript

I have an array of objects.
I need to get an array with unique website name with newest date.
Sample data
"data" : [
{
"position" : 2,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 3,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 1,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-04-06",
"dateTime" : ISODate("2020-04-06T00:00:00.000Z")
},
{
"position" : 6,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 4,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 2,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 4,
"website" : "opq.com",
"owned" : true,
"date" : "2020-04-01",
"dateTime" : ISODate("2020-04-01T00:00:00.000Z")
}
]
Based on datetTime, position and website. Need a mongoDB query or Javascript code. (Earlier its with dateTime and website)
(Extracting the object from which have highest date and unique website name with top position)
Expected response
"data" : [
{
"position" : 2,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 3,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 6,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 2,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
}
],

use forEach and build an object to handle dulicates and maintain only new date item.
Get Object.values from above object.
const uniq = (arr) => {
const res = {};
arr.forEach((item) => {
if (
!res[item.website] ||
new Date(item.dateTime) > new Date(res[item.website].dateTime)
) {
res[item.website] = { ...item };
}
});
return Object.values(res);
};
// Update uniq2
const uniq2 = (arr) => {
const res = {};
arr.forEach((item) => {
if (!res[item.website]) {
res[item.website] = { ...item };
} else {
if (new Date(item.dateTime) > new Date(res[item.website].dateTime)) {
res[item.website].dateTime = item.dateTime;
}
if (item.position > res[item.website].position) {
res[item.website].position = item.position;
}
}
});
return Object.values(res);
};
const data2 = [
{
position: 2,
website: "abc.com",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 3,
website: "qwe.com",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 1,
website: "qwe.com",
owned: false,
date: "2020-04-06",
dateTime: "2020-04-06T00:00:00.000Z",
},
{
position: 6,
website: "xyz.agency",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 1,
website: "opq.com",
owned: true,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 2,
website: "opq.com",
owned: true,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 3,
website: "opq.com",
owned: true,
date: "2020-04-01",
dateTime: "2020-04-01T00:00:00.000Z",
},
];
console.log(uniq2(data2));

In javascript you can reduce it:
var data = [ { "position" : 0, "website" : "abc.com", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 0, "website" : "qwe.com", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 0, "website" : "qwe.com", "owned" : false, "date" : "2020-04-06", "dateTime" : 'ISODate("2020-04-06T00:00:00.000Z")' }, { "position" : 0, "website" : "xyz.agency", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 1, "website" : "opq.com", "owned" : true, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 1, "website" : "opq.com", "owned" : true, "date" : "2020-04-01", "dateTime" : 'ISODate("2020-04-01T00:00:00.000Z")' }];
var result = data.reduce((acc, elem)=>{
isPresent = acc.findIndex(k=>k.website==elem.website);
if(isPresent==-1){
acc.push(elem);
} else {
if(new Date(acc[isPresent].date) < new Date(elem.date)) acc[isPresent] = elem;
}
return acc;
},[]);
console.log(result);

Try this- It also takes into consideration when the object is duplicate and dateTime are same/equal :
var d = { "data" : [
{
"position" : 0,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 0,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 0,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 1,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
}
]
}
var filtered = d.data.filter((el,idx)=> {
let found = d.data.findIndex((e,i)=> e.website === el.website && idx !== i);
return found === -1 ? el:Date.parse(el.dateTime) ===
Date.parse((d.data[found]).dateTime) && (found < idx) ? el:
Date.parse(el.dateTime) <
Date.parse((d.data[found]).dateTime) ? el: false
});
console.log(filtered);

Related

Complex mongodb data how to update

I'm trying to update show_seats.showByDate.shows.showSeats.seat_details.values.seat_status
this my code .........................................................................................................................................................
db.seats.updateOne({'show_seats.showByDate.shows.showSeats.seat_details.values._id':"62a9e044ec028e60180fe28a"},{$set:{'show_seats.showByDate.shows.showSeats.seat_details.values.seat_status' : true}})
.........................................................................................................................................................
please say what did i wrong**
{
"_id" : ObjectId("62a43ac2d7213c7233cd1dee"),
"totalShowByDay" : "2",
"totalShowDays" : 4,
"movieId" : ObjectId("62953ba3cb6ae625ec9433e6"),
"screenId" : ObjectId("6293b9638dde2d92658d5513"),
"createdAt" : 1654930114438,
"showId" : ObjectId("62a43ac2d7213c7233cd14ed"),
"show_seats" : [
{
"showByDate" : {
"ShowDate" : "2022-06-11",
"shows" : [
{
"showTime" : "2022-06-11T10:00",
"showSeats" : [
[
{
"category" : "CLASSIC",
"seat_details" : [
{
"key" : "A",
"values" : [
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ee"),
"seat_number" : "1",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00",,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
}
this is what i ended up doing, I need best solution
await db.getDb().collection(coll.seat).aggregate([
{
'$unwind': {
'path': '$show_seats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details.values'
}
}, {
'$match': {
'show_seats.showByDate.shows.showSeats.seat_details.values._id': ObjectId("62a43ac2d7213c7233cd14ee")
}
}, {
$addFields: { "show_seats.showByDate.shows.showSeats.seat_details.values.seat_status": true }
}
])

Filtering realtime database data

I have the following firebase database. I would like to filter and list the orders which's deliveryDuration value is "45"
"orders" : [ null, {
"comment" : "",
"date" : "2018-07-09 10:07:18",
"deliveryDuration" : "45",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:07:33",
"deliveryDuration" : "30",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Dessert",
"details" : "(Hausgemacht)",
"name" : "Warmer Topfenstrudel",
"price" : 3.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}, {
"comment" : "",
"date" : "2018-07-10 10:13:13",
"deliveryDuration" : "10",
"delivery_address" : "",
"item" : [ {
"available" : true,
"category" : "Spezialitäten",
"details" : "Schweinschnitzel mit Parmesan gebacken auf Spaghetti Napoli",
"name" : "Piccata Milanese",
"price" : 12.9,
"quantity" : 1
} ],
"orderVerified" : false,
"telefon" : "",
"userID" : "xyc#gmail.com",
"userName" : ""
}
}
I tried writing the following function:
getNewItems: function (order) {
if (db.ref('orders').child(order).child('deliveryDuration').equalTo(45)){
return db.ref('orders').child(order).child('deliveryDuration')
}}
How could I get the filtered objects? I was thinking that that i write an if-else statement and the function returns the values what i was looking for.
I think you're looking for this:
getNewItems: function (order) {
return db.ref('orders').orderByChild('deliveryDuration').equalTo(45));
}}
This Firebase query takes each child node of the location you query, orders it by the deliveryDuration property, and then filters to only get the ones who are equal to 45.

sort JSON object alphabeticly

Im trying to use underscore.js to sort my JSON object, however it doesn't seem to sort it alphabeticly when I sort by companyName?
As you can see with this code, the third object in data.adverts has doesnt start with the letter D ?
Code looks like:
var sortedDesc = _.sortBy(data.adverts, 'companyName');
$.each(sortedDesc, function () {
Doing my stuff here.....
});
The object looks like:
data({
"title" : "Krak firma API",
"query" : "http://api.eniro.com/cs/search/basic?geo_area=danmark&country=dk&search_word=DIS&version=1.1.3&callback=jQuery22003420392591506243_1456226871368",
"totalHits" : 44,
"totalCount" : 44,
"startIndex" : 1,
"itemsPerPage" : 25,
"adverts" : [{
"eniroId" : "66139761",
"companyInfo" : {
"companyName" : "Dansk Industri Service A/S",
"orgNumber" : "20250844",
"cvrPNumber" : "1004202979",
"companyText" : null
},
"address" : {
"streetName" : "Generatorvej 17",
"postCode" : "2860",
"postArea" : "Søborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.465702,
"latitude" : 55.729974
}, {
"use" : "route",
"longitude" : 12.465662,
"latitude" : 55.730202
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "70 20 70 15",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66139761/dis",
"homepage" : "http://api.eniro.com/proxy/homepage/uANwPf5aVK2cGM31dAhxnP94Fw6wLDJmjGFCQRGYZgO51Qs6_d_2i9h4MLaWRmsw_87mP_j3IjX1e2t2Hx6_yQ==",
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dansk-industri-service-as:66139761?search_word=DIS"
}, {
"eniroId" : "66295511",
"companyInfo" : {
"companyName" : "Diss",
"orgNumber" : "30924606",
"cvrPNumber" : "1013726465",
"companyText" : null
},
"address" : {
"coName" : "Rikke Søndermølle",
"streetName" : "Hovangsvej 71",
"postCode" : "9500",
"postArea" : "Hobro",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.786483,
"latitude" : 56.649997
}, {
"use" : "route",
"longitude" : 9.786596,
"latitude" : 56.650012
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "58 52 62 61",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66295511/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/diss:66295511?search_word=DIS"
}, {
"eniroId" : "66741314",
"companyInfo" : {
"companyName" : "Moster Di's Grill og Kaffevogn",
"orgNumber" : "21651605",
"cvrPNumber" : "1017404705",
"companyText" : null
},
"address" : {
"streetName" : "Hedelandsvej 14",
"postCode" : "7400",
"postArea" : "Herning",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 8.935914,
"latitude" : 56.127568
}, {
"use" : "route",
"longitude" : 8.93635,
"latitude" : 56.127463
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "97 42 04 20",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66741314/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/moster-dis-grill-og-kaffevogn:66741314?search_word=DIS"
}, {
"eniroId" : "122768415",
"companyInfo" : {
"companyName" : "Dis Byg ApS",
"orgNumber" : "37167304",
"cvrPNumber" : "1020824162",
"companyText" : null
},
"address" : {
"streetName" : "Ved Banen 8",
"postCode" : "7100",
"postArea" : "Vejle",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.553109,
"latitude" : 55.698735
}, {
"use" : "route",
"longitude" : 9.553159,
"latitude" : 55.698833
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/122768415/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-byg-aps:122768415?search_word=DIS"
}, {
"eniroId" : "121468337",
"companyInfo" : {
"companyName" : "Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Studiestræde 21 A",
"postCode" : "1455",
"postArea" : "København K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5697907,
"latitude" : 55.6784752
}, {
"use" : "route",
"longitude" : 12.5697907,
"latitude" : 55.6784752
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 12 00 00",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/121468337/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis:121468337?search_word=DIS"
}, {
"eniroId" : "108935200",
"companyInfo" : {
"companyName" : "Seapay Dis ApS",
"orgNumber" : "36054506",
"cvrPNumber" : "1019585685",
"companyText" : null
},
"address" : {
"streetName" : "Ivar Bentsens Vej 12",
"postCode" : "4300",
"postArea" : "Holbæk",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.758894,
"latitude" : 55.717305
}, {
"use" : "route",
"longitude" : 11.758669,
"latitude" : 55.717311
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/108935200/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/seapay-dis-aps:108935200?search_word=DIS"
}, {
"eniroId" : "66303436",
"companyInfo" : {
"companyName" : "Dis-Faxe",
"orgNumber" : "33686587",
"cvrPNumber" : "1016954493",
"companyText" : null
},
"address" : {
"coName" : "Gitte Willumsen Berg",
"streetName" : "Terslev Bygade 7",
"postCode" : "4690",
"postArea" : "Haslev",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.969888,
"latitude" : 55.374341
}, {
"use" : "route",
"longitude" : 11.970108,
"latitude" : 55.374433
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "30 29 98 77",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66303436/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-faxe:66303436?search_word=DIS"
}, {
"eniroId" : "68809471",
"companyInfo" : {
"companyName" : "Dis Bygge Og Ejendomsadministration",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Tuborg Boulevard 12, 1. sal.",
"postCode" : "2900",
"postArea" : "Hellerup",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5807377,
"latitude" : 55.7271613
}, {
"use" : "route",
"longitude" : 12.5807377,
"latitude" : 55.7271613
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "24 45 71 74",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/68809471/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-bygge-og-ejendomsadministration:68809471?search_word=DIS"
}, {
"eniroId" : "93544746",
"companyInfo" : {
"companyName" : "Dis Center ApS",
"orgNumber" : "35473009",
"cvrPNumber" : "1018749374",
"companyText" : null
},
"address" : {
"streetName" : "Hammershøj 38",
"postCode" : "9460",
"postArea" : "Brovst",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.596158,
"latitude" : 57.05976
}, {
"use" : "route",
"longitude" : 9.596163,
"latitude" : 57.059895
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/93544746/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-center-aps:93544746?search_word=DIS"
}, {
"eniroId" : "66949810",
"companyInfo" : {
"companyName" : "Dis-Danmark",
"orgNumber" : "16917338",
"cvrPNumber" : "1005846555",
"companyText" : null
},
"address" : {
"coName" : "Jan Amnitzbøl Krusell",
"streetName" : "Frøkærparken 116",
"postCode" : "8320",
"postArea" : "Mårslet",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 10.152582,
"latitude" : 56.07644
}, {
"use" : "route",
"longitude" : 10.152397,
"latitude" : 56.076574
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "24 63 62 69",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66949810/dis",
"homepage" : "http://api.eniro.com/proxy/homepage/uANwPf5aVK26f2J7KyRZqimid0oj_3qIjQ0XbJhXGfpb4h41yM5odSv_D17GURWQlMNOi53PltI=",
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-danmark:66949810?search_word=DIS"
}, {
"eniroId" : "123661747",
"companyInfo" : {
"companyName" : "Dis Group Holding A/S",
"orgNumber" : "37226963",
"cvrPNumber" : "1020895019",
"companyText" : null
},
"address" : {
"streetName" : "Ørstedsvej 10",
"postCode" : "8660",
"postArea" : "Skanderborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.980758,
"latitude" : 56.069991
}, {
"use" : "route",
"longitude" : 9.9793,
"latitude" : 56.070543
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/123661747/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-group-holding-as:123661747?search_word=DIS"
}, {
"eniroId" : "109604576",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Holmbladsgade 70 B",
"postCode" : "2300",
"postArea" : "København S",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.6120585,
"latitude" : 55.6648985
}, {
"use" : "route",
"longitude" : 12.6120585,
"latitude" : 55.6648985
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "32 84 11 17",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/109604576/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:109604576?search_word=DIS"
}, {
"eniroId" : "66902798",
"companyInfo" : {
"companyName" : "Dis Holding ApS",
"orgNumber" : "29180407",
"cvrPNumber" : "1011836085",
"companyText" : null
},
"address" : {
"coName" : "Rikke Søndermølle",
"streetName" : "Hovangsvej 71",
"postCode" : "9500",
"postArea" : "Hobro",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.786483,
"latitude" : 56.649997
}, {
"use" : "route",
"longitude" : 9.786596,
"latitude" : 56.650012
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "22 99 66 96",
"label" : null
}, {
"type" : "custom",
"phoneNumber" : "40 88 83 48",
"label" : "Mobil"
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66902798/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-holding-aps:66902798?search_word=DIS"
}, {
"eniroId" : "68536140",
"companyInfo" : {
"companyName" : "Danish Ins.for Study Abroad Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Ctr By ",
"postCode" : "1199",
"postArea" : "Kh K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : null,
"latitude" : null
}, {
"use" : "route",
"longitude" : null,
"latitude" : null
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 11 51 91",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/68536140/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/danish-insfor-study-abroad-dis:68536140?search_word=DIS"
}, {
"eniroId" : "112589612",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Peder Skrams Gade 23, 1. sal.",
"postCode" : "1054",
"postArea" : "København K",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5895666,
"latitude" : 55.6773222
}, {
"use" : "route",
"longitude" : 12.5895666,
"latitude" : 55.6773222
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 15 05 30",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/112589612/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:112589612?search_word=DIS"
}, {
"eniroId" : "108478586",
"companyInfo" : {
"companyName" : "Fonden Dis",
"orgNumber" : null,
"cvrPNumber" : null,
"companyText" : null
},
"address" : {
"streetName" : "Ravnsborggade 11",
"postCode" : "2200",
"postArea" : "København N",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.5618939,
"latitude" : 55.688666
}, {
"use" : "route",
"longitude" : 12.5618939,
"latitude" : 55.688666
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "35 36 65 05",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/108478586/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fonden-dis:108478586?search_word=DIS"
}, {
"eniroId" : "67198842",
"companyInfo" : {
"companyName" : "Fabriksarbejder Klubben Abb Dis- Tribution",
"orgNumber" : "15624299",
"cvrPNumber" : "1005745320",
"companyText" : null
},
"address" : {
"streetName" : "Ligustervænget 9",
"postCode" : "7000",
"postArea" : "Fredericia",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.749819,
"latitude" : 55.610781
}, {
"use" : "route",
"longitude" : 9.749936,
"latitude" : 55.610541
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67198842/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/fabriksarbejder-klubben-abb-dis-tribution:67198842?search_word=DIS"
}, {
"eniroId" : "104892983",
"companyInfo" : {
"companyName" : "Dis-Skive",
"orgNumber" : "35575154",
"cvrPNumber" : "1019219093",
"companyText" : null
},
"address" : {
"coName" : "Lone Hjorth Nielsen",
"streetName" : "Brombærvej 10",
"postCode" : "7800",
"postArea" : "Skive",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 9.027915,
"latitude" : 56.586359
}, {
"use" : "route",
"longitude" : 9.027838,
"latitude" : 56.586196
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/104892983/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-skive:104892983?search_word=DIS"
}, {
"eniroId" : "66957596",
"companyInfo" : {
"companyName" : "Dis Udlejning ApS",
"orgNumber" : "32143237",
"cvrPNumber" : "1015238832",
"companyText" : null
},
"address" : {
"streetName" : "Generatorvej 17",
"postCode" : "2860",
"postArea" : "Søborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.465702,
"latitude" : 55.729974
}, {
"use" : "route",
"longitude" : 12.465662,
"latitude" : 55.730202
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "70 20 70 15",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66957596/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-udlejning-aps:66957596?search_word=DIS"
}, {
"eniroId" : "67034343",
"companyInfo" : {
"companyName" : "DI's Risk Management Forening",
"orgNumber" : "33336047",
"cvrPNumber" : "1016630620",
"companyText" : null
},
"address" : {
"coName" : "DI, 1787 v",
"streetName" : "H.C. Andersens Boulevard 18",
"postCode" : "1553",
"postArea" : "København V",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.568025,
"latitude" : 55.675344
}, {
"use" : "route",
"longitude" : 12.568243,
"latitude" : 55.675492
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "33 77 33 77",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67034343/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-risk-management-forening:67034343?search_word=DIS"
}, {
"eniroId" : "66269242",
"companyInfo" : {
"companyName" : "DIs Personaleforening, DIP",
"orgNumber" : "26832039",
"cvrPNumber" : "1009390797",
"companyText" : null
},
"address" : {
"coName" : "DI, 1787 v",
"streetName" : "H.C. Andersens Boulevard 18",
"postCode" : "1553",
"postArea" : "København V",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.568025,
"latitude" : 55.675344
}, {
"use" : "route",
"longitude" : 12.568243,
"latitude" : 55.675492
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "40 19 80 90",
"label" : null
}, {
"type" : "custom",
"phoneNumber" : "30 59 05 02",
"label" : "Mobil"
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66269242/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-personaleforening-dip:66269242?search_word=DIS"
}, {
"eniroId" : "66180758",
"companyInfo" : {
"companyName" : "DIS-Næstved",
"orgNumber" : "34762996",
"cvrPNumber" : "1018256270",
"companyText" : null
},
"address" : {
"coName" : "Per Hartvig-Olsen",
"streetName" : "Tornemarksvej 38",
"postCode" : "4700",
"postArea" : "Næstved",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 11.561402,
"latitude" : 55.239048
}, {
"use" : "route",
"longitude" : 11.560598,
"latitude" : 55.238628
}
]
},
"phoneNumbers" : [],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/66180758/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-næstved:66180758?search_word=DIS"
}, {
"eniroId" : "102638245",
"companyInfo" : {
"companyName" : "Dis-Sydfyn",
"orgNumber" : "35548130",
"cvrPNumber" : "1019012014",
"companyText" : null
},
"address" : {
"coName" : "Nani Krejsing",
"streetName" : "Kogtvedvej 76",
"postCode" : "5700",
"postArea" : "Svendborg",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 10.583639,
"latitude" : 55.048368
}, {
"use" : "route",
"longitude" : 10.583852,
"latitude" : 55.048192
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "65 31 60 31",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/102638245/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-sydfyn:102638245?search_word=DIS"
}, {
"eniroId" : "103192650",
"companyInfo" : {
"companyName" : "Dis-Kongeaaen",
"orgNumber" : "35585036",
"cvrPNumber" : "1019047985",
"companyText" : null
},
"address" : {
"coName" : "Jens christian Andersen",
"streetName" : "Toftager 16",
"postCode" : "6660",
"postArea" : "Lintrup",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 8.971892,
"latitude" : 55.409949
}, {
"use" : "route",
"longitude" : 8.971726,
"latitude" : 55.40997
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "74 85 54 23",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/103192650/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-kongeaaen:103192650?search_word=DIS"
}, {
"eniroId" : "67016411",
"companyInfo" : {
"companyName" : "DIS København Nord",
"orgNumber" : "31661811",
"cvrPNumber" : "1014736502",
"companyText" : null
},
"address" : {
"coName" : "Ann Christensen",
"streetName" : "Ellestien 1",
"postCode" : "2880",
"postArea" : "Bagsværd",
"postBox" : null
},
"location" : {
"coordinates" : [{
"longitude" : 12.45442,
"latitude" : 55.768364
}, {
"use" : "route",
"longitude" : 12.454607,
"latitude" : 55.768437
}
]
},
"phoneNumbers" : [{
"type" : "std",
"phoneNumber" : "22 58 86 46",
"label" : null
}
],
"companyReviews" : "http://www.dethitter.dk/brugeranmeldelse/f/67016411/dis",
"homepage" : null,
"facebook" : null,
"infoPageLink" : "http://www.krak.dk/f/dis-københavn-nord:67016411?search_word=DIS"
}
]
});
The companyName is a property inside companyInfo so you should try something like this:
var sortedDesc = _.sortBy(data.adverts, function(a){ return a.companyInfo.companyName; });
Unfortunately there is no generic "compare" function in JavaScript to return a suitable value for sort(). I'd write a compareStrings function that uses comparison operators and then use it in the sort function.
function compareStrings(a, b) {
// Assuming you want case-insensitive comparison
a = a.toLowerCase();
b = b.toLowerCase();
return (a < b) ? -1 : (a > b) ? 1 : 0;
}
places.sort(function(a, b) {
return compareStrings(a.city, b.city);
})
more detail visit here. Sorting JSON (by specific element) alphabetically

underscore - locating node based in a value

This is my 1st time using underscore...I've this simple json...
"categories" : [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
you can see I've the TAG key in several places. I need to get the entire tree based in a criteria for tag. I was using, find, filter, where and findWhere and all the time I got the same results:
var find = _.find($rootScope.webshop.categories, {tag: 'cat1'});
this works !
but if I try...
var find = _.find($rootScope.webshop.categories, {tag: 'cat11'});
No results :( Even using _.where or ._filter or ._findWhere. - The results always will be the same.
Can someone help a beginner in Underscore with something that's probably simple ?!
ty !
_.find does not recurse, you will have to build your own solution. Something like this:
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
_.findIn('children', categories, {tag: 'cat11'})
categories = [
{
"tag" : "cat1",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1",
"pt" : "Categoria 1"
},
"children" : [
{
"tag" : "cat11",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 1.1",
"pt" : "Categoria 1.1"
}
},
{
"tag" : "cat12",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 1.2",
"pt" : "Categoria 1.2"
}
}
]
},
{
"tag" : "cat2",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2",
"pt" : "Categoria 2"
},
"children" : [
{
"tag" : "cat21",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 2.1",
"pt" : "Categoria 2.1"
}
},
{
"tag" : "cat22",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 2.2",
"pt" : "Categoria 2.2"
}
}
]
},
{
"tag" : "cat3",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3",
"pt" : "Categoria 3"
},
"children" : [
{
"tag" : "cat31",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 3.1",
"pt" : "Categoria 3.1"
}
},
{
"tag" : "cat32",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 3.2",
"pt" : "Categoria 3.2"
}
}
]
},
{
"tag" : "cat4",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4",
"pt" : "Categoria 4"
},
"children" : [
{
"tag" : "cat41",
"active" : true,
"order" : 10,
"title" : {
"en" : "Category 4.1",
"pt" : "Categoria 4.1"
}
},
{
"tag" : "cat42",
"active" : true,
"order" : 20,
"title" : {
"en" : "Category 4.2",
"pt" : "Categoria 4.2"
}
}
]
}
]
_.findIn = function() {
var args = Array.prototype.slice.call(arguments, 0);
var childrenProp = args[0];
var result = _.find.apply(_, args.slice(1));
if (result !== void(8)) return result;
var arr = args[1];
for (var i = 0, l = arr.length; i < l; i++) {
args[1] = arr[i][childrenProp];
var result = _.findIn.apply(_, args);
if (result !== void(8)) return result;
}
return void(8);
}
document.write(JSON.stringify(_.findIn('children', categories, {tag: 'cat11'})));
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>

data manipulation in javascript

i have an array of information containing folders like this:
[
{
"ACLID" : 0,
"ID" : 100,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "04/12/2012 17:38:46",
"isSubFolder" : 0,
"name" : "Piyush1",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "04/12/2012 17:38:46"
},
{
"ACLID" : 0,
"ID" : 102,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/10/2012 12:38:25",
"isSubFolder" : 0,
"name" : "New",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/10/2012 12:38:25"
},
{
"ACLID" : 0,
"ID" : 103,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/14/2012 18:00:22",
"isSubFolder" : 0,
"name" : "SegFolderWithDiffPolicy",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 39,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/14/2012 18:00:22"
},
{
"ACLID" : 0,
"ID" : 104,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/17/2012 14:13:56",
"isSubFolder" : 0,
"name" : "New1",
"objectType" : 3,
-->"parentID" : 100,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/17/2012 14:13:56"
},
{
"ACLID" : 0,
"ID" : 105,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/17/2012 14:14:13",
"isSubFolder" : 0,
"name" : "abcasdna",
"objectType" : 3,
-->"parentID" : 104,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/17/2012 14:14:13"
},
{
"ACLID" : 0,
"ID" : 106,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/18/2012 12:51:39",
"isSubFolder" : 0,
"name" : "new2",
"objectType" : 3,
-->"parentID" : 100,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/18/2012 12:51:39"
}
]
now the required type of data is:
[{
name:'Piyush1',
children: [{
name: 'New1',
children:[{
name: 'abcasdna'
}]
},{
name: 'New2'
}]
},{
name: 'New'
}]
Now the thing is that there could be any number of folders and any level of hierarchy.
so is there a way i can achieve this conversion?

Categories

Resources