How to get a field and a field into of array in MongoDB - arrays

I have this json in MongoDB:
{
_id: ObjectId("630fbb09cc9deb16a33fbcde"),
firmness: {
name: 'hard',
url: 'https://pokeapi.co/api/v2/berry-firmness/3/'
},
flavors: [
{
flavor: {
name: 'spicy',
url: 'https://pokeapi.co/api/v2/berry-flavor/1/'
},
potency: 10
},
{
flavor: {
name: 'dry',
url: 'https://pokeapi.co/api/v2/berry-flavor/2/'
},
potency: 0
},
{
flavor: {
name: 'sweet',
url: 'https://pokeapi.co/api/v2/berry-flavor/3/'
},
potency: 0
},
{
flavor: {
name: 'bitter',
url: 'https://pokeapi.co/api/v2/berry-flavor/4/'
},
potency: 0
},
{
flavor: {
name: 'sour',
url: 'https://pokeapi.co/api/v2/berry-flavor/5/'
},
potency: 10
}
],
growth_time: 2,
id: 20,
item: { name: 'pinap-berry', url: 'https://pokeapi.co/api/v2/item/145/' },
max_harvest: 10,
name: 'pinap',
natural_gift_power: 70,
natural_gift_type: { name: 'grass', url: 'https://pokeapi.co/api/v2/type/12/' },
size: 80,
smoothness: 20,
soil_dryness: 35
}
And I want to get the soil_dryness field and the field called "name" into flavor list from 'flavors' array.
How can I do that?
I tried with:
db.Berries.find({},{soil_dryness:1},{"flavors.$":1}).pretty().limit(20)
But unfortunately doesn't works.

Query
project, the soil_dryness and the names, path in arrays = array with the values, here the names
*not sure if this the output you need, if its not if you can add the expected ouput you need
Playmongo
db.Berries.aggregate(
[{"$project": {"soil_dryness": 1, "names": "$flavors.flavor.name"}},
{"$limit": 20}])

Related

Filter an array of object with another array - returning two values

I wanted to filter my seach by name and type when a user types on the search box however I am just getting back the names but not by type.
This is my array:
const projects = [
{
id: 1,
name: 'protfolio',
type: ['react']
},
{
id: 2,
name: 'RoboFriends',
type: ['react']
},
{
id: 3,
name: 'Background-gradient',
type: ['html ', 'css ', 'javaScript']
},
{
id: 4,
name: 'Election-map',
type: ['html ', 'css ', 'javaScript']
},
{
id: 5,
name: 'To-Do-List',
type: ['react']
}
]
The function I've reacted to filter over my array and return the project name and also type
const projectFilter = this.state.projects.filter( project => {
return project.name.toLowerCase().includes(this.state.searchField.toLowerCase())
|| project.type.includes(this.state.searchField);
})
Your code is fine. I checked. Make sure your searchField came as you wish.
Check this out
function myFunction() {
const projects = [
{
id: 1,
name: 'protfolio',
type: ['react']
},
{
id: 2,
name: 'RoboFriends',
type: ['react']
},
{
id: 3,
name: 'Background-gradient',
type: ['html ', 'css ', 'javaScript']
},
{
id: 4,
name: 'Election-map',
type: ['html ', 'css ', 'javaScript']
},
{
id: 5,
name: 'To-Do-List',
type: ['react']
}
]
const projectFilter = projects.filter( project => {
return project.name.toLowerCase().includes('react')
|| project.type.includes('react');
})
document.getElementById("test").innerText = (projectFilter[2].name);
}

ShieldUI Grid Multiple CustomEditors

i am wondering if anybody has used two or more shieldui grid custom editors on the same page. How would this work with the getCustomEditorValue function (updated with their stock example)? I am guessing that i need to do something inside of the getCustomEditorValue function to differentiate the 2 dropdowns?
$("#grid1").shieldGrid({
dataSource: {
data: products,
schema: {
fields: {
id: { path: "ProductID", type: Number},
name: { path: "ProductName", type: String, nullable: false },
quantity: { path: "SupplierID", type: Number },
price: { path: "UnitPrice", type: Number },
price2: { path: "UnitPrice2", type: Number },
units: { path: "UnitsInStock", type: Number },
discontinued: { path: "Discontinued", type: Boolean },
myDate: { path: "d", type: Date }}}
},
events: {
editorCreating: function(e) {
if (e.field == "price") {
e.options = { max: 50 };
}
}
},
rowHover: false,
columns: [
{ field: "id" },
{ field: "name", width: "200px" },
{ field: "quantity" },
{ field: "price", editor: myCustomEditor },
{ field: "price2", editor: myCustomEditor2 },
{ field: "units" },
{ field: "discontinued" },
{ field: "myDate", format: "{0:MM/dd/yyyy}" }
],
events: {
getCustomEditorValue: function (e) {
e.value = $("#test").swidget().value();
$("#test").swidget().destroy();
}
},
editing: {
enabled: true,
event: "doubleclick
type: "cell"
}
});
function myCustomEditor(cell, item) {
$('<div id="test"/>')
.appendTo(cell)
.shieldDropDown({
dataSource: {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
},
value: !item["price"] ? null : item["price"].toString()
}).swidget().focus();
}
function myCustomEditor2(cell, item) {
$('<div id="test2"/>')
.appendTo(cell)
.shieldDropDown({
dataSource: {
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
},
value: !item["price2"] ? null : item["price2"].toString()
}).swidget().focus();
}
Thanks to support for the answer:
getCustomEditorValue: function (e) {
if ($("#dropdown1").length > 0) {
e.value = $("#dropdown1").swidget().value();
$("#dropdown1").swidget().destroy();
}
if ($("#dropdown2").length > 0) {
e.value = $("#dropdown2").swidget().value();
$("#dropdown2").swidget().destroy();
}
}

Provide hints to user while searching

I am stuck on the functionality to show user hints while searching of products in application.I do have an Api which returns me all category-Listing with its sub-categories/child.I have save it in my local storage but i am really confused that how can i make loop to display characters in hint list just below the search bar while user is typing something in search bar.I know the process but do not know how to implement in all nested child for every category.
I have found this below mentioned example but again its working for only simple Array ..
Example :- http://codepen.io/calendee/pen/pCwyx
Can any one suggest me that how can i use the above logic for my array
[
{
id: "165",
title: "Women's Fashion",
url: "womens-fashion",
children: [
{
id: "176",
title: "Women's Aparel",
url: "women-s-aparel",
children: [
{
id: "221",
title: "Formal",
url: "formal"
},
{
id: "222",
title: "Casual",
url: "casual"
},
{
id: "223",
title: "Traditional",
url: "traditional"
},
{
id: "224",
title: "Nightwears",
url: "nightwears"
},
{
id: "225",
title: "Winter wears",
url: "winter-wears"
},
{
id: "240",
title: "Party Wear",
url: "party-wear"
}
]
},
{
id: "220",
title: "Western Wear",
url: "western-wear",
children: [
{
id: "318",
title: "Glam & Luxe Style Wears",
url: "glam-luxe-style-wears"
},
{
id: "412",
title: "Dresses",
url: "dresses"
},
{
id: "413",
title: "Tops & Tees",
url: "tops-tees"
},
{
id: "414",
title: "Bottom Wear",
url: "bottom-wear"
}
]
},
{
id: "305",
title: "Ethnic Wear",
url: "ethnic-wear",
children: [
{
id: "407",
title: "Sarees",
url: "sarees"
},
{
id: "408",
title: "Lehnga Choli",
url: "lehnga-choli"
},
{
id: "409",
title: "Gown",
url: "gown"
},
{
id: "410",
title: "Salwar Kameez & Churidar",
url: "salwar-kameez-churidar"
},
{
id: "411",
title: "Kurtis",
url: "kurtis"
}
]
},
{
id: "306",
title: "Jewellery",
url: "jewellery",
children: [
{
id: "307",
title: "Anklets",
url: "anklets"
},
{
id: "308",
title: "Bridal Set",
url: "bridal-set"
},
{
id: "309",
title: "Mala",
url: "mala"
},
{
id: "310",
title: "Mangalsutra",
url: "mangalsutra"
},
{
id: "311",
title: "Necklace Set",
url: "necklace-set"
},
{
id: "312",
title: "Ring",
url: "ring"
},
{
id: "316",
title: "Bangles",
url: "bangles"
},
{
id: "317",
title: "Earings",
url: "earings"
}
]
},
{
id: "415",
title: "Leggings & Jeggings",
url: "leggings-and-jeggings"
},
{
id: "416",
title: "Lingeries & Nightwears",
url: "lingeries-and-nightwears"
},
{
id: "417",
title: "All Women Clothing",
url: "all-women-clothing"
}
]
},
{
id: "404",
title: "Men's Fashion",
url: "mens-fashion",
children: [
{
id: "177",
title: "Men's Aparel",
url: "men-s-aparel",
children: [
{
id: "226",
title: "Casual",
url: "casual"
},
{
id: "227",
title: "Formal",
url: "formal"
},
{
id: "230",
title: "Others",
url: "others"
},
{
id: "303",
title: "Winter",
url: "winter"
},
{
id: "239",
title: "Party Wear",
url: "party-wear"
}
]
},
{
id: "229",
title: "Ethnic",
url: "ethnic",
children: [
{
id: "418",
title: "Indo Western",
url: "indo-western"
},
{
id: "419",
title: "Kurta Pyjama",
url: "kurta-pyjama"
},
{
id: "420",
title: "Kurta",
url: "kurta"
},
{
id: "421",
title: "Sherwanis",
url: "sherwanis"
}
]
},
{
id: "228",
title: "Western",
url: "western",
children: [
{
id: "422",
title: "Jeans",
url: "jeans"
},
{
id: "423",
title: "Polo & Tees",
url: "polo-tees"
},
{
id: "424",
title: "Shirts",
url: "shirts"
},
{
id: "425",
title: "Trousers",
url: "trousers"
},
{
id: "426",
title: "Shorts & 3/4",
url: "shorts-and-3-4"
}
]
},
{
id: "427",
title: "Inner wear",
url: "inner-wear"
},
{
id: "428",
title: "Sleep & Lounge Wear",
url: "sleep-and-lounge-wear"
},
{
id: "429",
title: "Knit Wear",
url: "knit-wear"
},
{
id: "430",
title: "Suits & Blazers",
url: "suits-and-blazers"
},
{
id: "431",
title: "Sweat Shirts",
url: "sweat-shirts"
}
]
}
]

Protractor promise produces array that overrides previous values [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
I have angular ui grid that I am trying to get values from its cells. Here is my list in table
id name
1 AUSTRIA
2 BELGIUM
3 BULGARIA
4 CROATIA
5 CZECH REPUBLIC
This is code I run:
element(by.id('grid1')).all(by.repeater('(rowRenderIndex, row) in rowContainer.renderedRows track by $index')).then(function (items) {
for (var i = 0; i < items.length; i++) {
var country = <any>{}; //typescript code
self.gridTestUtils.dataCell('grid1', i, 0).getText().then(
function (valueId) {
country.id = valueId
});
self.gridTestUtils.dataCell('grid1', i, 1).getText().then(
function (valueName) {
country.name = valueName;
self.countryList.push(country)
console.log(self.countryList)
});
}
});
And this is result
[ { id: 1, name: 'AUSTRIA' }]
[ { id: 1, name: 'BELGIUM' },
{ id: 1, name: 'BELGIUM' }]
[ { id: 1, name: 'BULGARIA' },
{ id: 1, name: 'BULGARIA' },
{ id: 1, name: 'BULGARIA' } ]
[ { id: 1, name: 'CROATIA' },
{ id: 1, name: 'CROATIA' },
{ id: 1, name: 'CROATIA' },
{ id: 1, name: 'CROATIA' } ]
[ { id: 1, name: 'CZECH REPUBLIC' },
{ id: 1, name: 'CZECH REPUBLIC' },
{ id: 1, name: 'CZECH REPUBLIC' },
{ id: 1, name: 'CZECH REPUBLIC' },
{ id: 1, name: 'CZECH REPUBLIC' } ]
I expect result would look like:
[ { id: 1, name: 'AUSTRIA' }]
[ { id: 1, name: 'AUSTRIA' },
{ id: 1, name: 'BELGIUM' }]
[ { id: 1, name: 'AUSTRIA' },
{ id: 1, name: 'BELGIUM' },
{ id: 1, name: 'BULGARIA' } ]
[ { id: 1, name: 'AUSTRIA' },
{ id: 1, name: 'BELGIUM' },
{ id: 1, name: 'BULGARIA' },
{ id: 1, name: 'CROATIA' } ]
[ { id: 1, name: 'AUSTRIA' },
{ id: 1, name: 'BELGIUM' },
{ id: 1, name: 'BULGARIA' },
{ id: 1, name: 'CROATIA' },
{ id: 1, name: 'CZECH REPUBLIC' } ]
What is wrong with my code? What should I do that I have expected array
The problem is that i in your loop changes as the loops goes through, and in your async calls you are using i and getting a promise, but it's only a reference, so at the time the promises get resolved, the will have the reference to the variable with the last value of the loop. A workaround is to create an IIFE:
element(by.id('grid1')).all(by.repeater('(rowRenderIndex, row) in rowContainer.renderedRows track by $index')).then(function (items) {
for (var i = 0; i < items.length; i++) {
var country = <any>{}; //typescript code
(function (i, country) {
self.gridTestUtils.dataCell('grid1', i, 0).getText().then(function (valueId) {
country.id = valueId;
});
self.gridTestUtils.dataCell('grid1', i, 1).getText().then(function (valueName) {
country.name = valueName;
self.countryList.push(country)
console.log(self.countryList)
});
})(i, country);
}
});

Kendo update scheduler options dynamically

I am using KendoUI scheduler with AngularJS.
I am declaring the scheduler options in accordance with the documentation, pretty standard stuff. See below:
What I would like to do is be able to update $scope.schedulerOptions and have those changes reflected in the UI. When I make changes to $scope.schedulerOptions nothing changes in the UI.
Any ideas on how to do this?
$scope.schedulerOptions = {
date: new Date("2013/6/13"),
startTime: new Date("2013/6/13 07:00 AM"),
height: 600,
views: [
"day",
{ type: "workWeek", selected: true },
"week",
"month",
],
eventTemplate: "<span class='custom-event'>{{dataItem.title}}</span>",
allDayEventTemplate: "<div class='custom-all-day-event'>{{dataItem.title}}</div>",
timezone: "Etc/UTC",
dataSource: {
batch: true,
transport: {
read: {
url: "http://demos.telerik.com/kendo-ui/service/tasks",
dataType: "jsonp"
},
update: {
url: "http://demos.telerik.com/kendo-ui/service/tasks/update",
type: "PUT"
},
create: {
url: "http://demos.telerik.com/kendo-ui/service/tasks/create",
type: "POST"
},
destroy: {
url: "http://demos.telerik.com/kendo-ui/service/tasks/destroy",
type: "DELETE"
},
parameterMap: function(options, operation) {
if (operation !== "read" && options.models) {
return {models: kendo.stringify(options.models)};
}
}
},
schema: {
model: {
id: "taskId",
fields: {
taskId: { from: "TaskID", type: "number" },
title: { from: "Title", defaultValue: "No title", validation: { required: true } },
start: { type: "date", from: "Start" },
end: { type: "date", from: "End" },
startTimezone: { from: "StartTimezone" },
endTimezone: { from: "EndTimezone" },
description: { from: "Description" },
recurrenceId: { from: "RecurrenceID" },
recurrenceRule: { from: "RecurrenceRule" },
recurrenceException: { from: "RecurrenceException" },
ownerId: { from: "OwnerID", defaultValue: 1 },
isAllDay: { type: "boolean", from: "IsAllDay" }
}
}
},
filter: {
logic: "or",
filters: [
{ field: "ownerId", operator: "eq", value: 1 },
{ field: "ownerId", operator: "eq", value: 2 }
]
}
},
resources: [
{
field: "ownerId",
title: "Owner",
dataSource: [
{ text: "Alex", value: 1, color: "#f8a398" },
{ text: "Bob", value: 2, color: "#51a0ed" },
{ text: "Charlie", value: 3, color: "#56ca85" }
]
}
]
};
According to this post at telerik forum you can change the values after initialization modifying HTML to <div kendo-scheduler="sched" k-options="options"></div> and the accessing sched as a model variable $scope.sched.setOptions(...);

Resources