Hello I want to print innermost content from following query,
How can I access plans > personal > title, and print the content on screen.
Thanks a lot!
I get data from data.js which is shown below:
export default [
{
sys: {
id: "1"
},
fields: {
name: "cloud hosting",
slug: "cloud-hosting",
type: "hosting",
price: 100,
size: 200,
capacity: 1,
featured: true,
description:
"The power of SSD + Simplicity of cPanel",
extras: [
"Plush pillows and breathable bed linens",
"Soft, oversized bath towels",
"Full-sized, pH-balanced toiletries",
"Complimentary refreshments",
"Adequate safety/security",
"Internet",
"Comfortable beds"
],
plans:[
{
fields:{
personal:{
title: "personal"
}
}
}
]
}
}
];
You can make use of flatMap. I'm assuming your data is an array.
var data = [{ sys: { id: "1" }, fields: { name: "cloud hosting", slug: "cloud-hosting", type: "hosting", price: 100, size: 200, capacity: 1, featured: true, description: "The power of SSD + Simplicity of cPanel", extras: [ "Plush pillows and breathable bed linens", "Soft, oversized bath towels", "Full-sized, pH-balanced toiletries", "Complimentary refreshments", "Adequate safety/security", "Internet", "Comfortable beds" ], plans: [{ fields: { personal: { title: "personal" } } }] }}];
result = data.flatMap(({fields})=>fields.plans.map(k=>(k.fields.personal.title)));
console.log(result);
Related
Trying to use the filter method to find the trails that are at least 4 miles in the hikingBermudaObject. Is there a better way i can do this?
const hikingBermuda = {
author: "Cecile Davidson",
price: 17.95,
title: "Hiking Bermuda",
sections: {
0: {
sectionName: "Preface",
chapters: [
{
name: "Trail Locator",
page: 12,
},
],
},
1: {
sectionName: "Introduction",
chapters: [
{
name: "Map Legend",
page: 24,
},
{
name: "Rating System",
page: 22,
},
],
},
2: {
sectionName: "West End",
chapters: [
{
name: "Botanical Gardens",
page: 63,
},
{
name: "Rockaway, Whale Bay Park",
page: 45,
},
{
name: "Somerset Bridge, Hog Bay Park",
page: 39,
},
],
},
},
trails: [
{
name: "Somerset to Hamilton Railway Trail",
page: 75,
section: "West End",
distanceInMiles: 11.7,
rating: "Hardy",
},
{
name: "City of Hamilton",
page: 69,
section: "West End",
distanceInMiles: 2.4,
rating: "Easy",
},
{
name: "St. George's Point",
page: 131,
section: "East End",
distanceInMiles: 3.6,
rating: "Moderate",
},
{
name: "South Shore Beaches",
page: 53,
section: "West End",
distanceInMiles: 5.0,
rating: "Moderate",
},`enter code here`
],`enter code here`
};
Description
There are some errors in the above JSON.
The below is an example of how this would be a good use case for JavaScript's Array.filter function.
Example
const hikingBermuda = {
author: 'Cecile Davidson',
price: 17.95,
title: 'Hiking Bermuda',
sections: {
0: {
sectionName: 'Preface',
chapters: [{
name: 'Trail Locator',
page: 12
}]
},
1: {
sectionName: 'Introduction',
chapters: [{
name: 'Map Legend',
page: 24
},
{
name: 'Rating System',
page: 22
}
]
},
2: {
sectionName: 'West End',
chapters: [{
name: 'Botanical Gardens',
page: 63
},
{
name: 'Rockaway, Whale Bay Park',
page: 45
},
{
name: 'Somerset Bridge, Hog Bay Park',
page: 39
}
]
}
},
trails: [{
name: 'Somerset to Hamilton Railway Trail',
page: 75,
section: 'West End',
distanceInMiles: 11.7,
rating: 'Hardy'
},
{
name: 'City of Hamilton',
page: 69,
section: 'West End',
distanceInMiles: 2.4,
rating: 'Easy'
},
{
name: "St. George's Point",
page: 131,
section: 'East End',
distanceInMiles: 3.6,
rating: 'Moderate'
},
{
name: 'South Shore Beaches',
page: 53,
section: 'West End',
distanceInMiles: 5.0,
rating: 'Moderate'
}
]
}
const mileLimit = 4
const trails = hikingBermuda.trails.filter((el) => {
return el.distanceInMiles <= mileLimit
})
console.log(trails)
My issue today is with MongoDB and mongoose in javascript. So first off I have this schema:
var playerModel = new Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "Users",
},
class: {
type: String,
required: true,
},
level: {
type: Number,
default: 1,
},
spells: {
type: [String],
default: ["", "", "", "", ""],
},
inventory: [], <- IMPORTANT
toolbelt: {
axe: [{}],
pickaxe: [{}],
fishingrod: [{}],
hammer: [{}],
saw: [{}],
sewingkit: [{}],
knife: [{}],
gemkit: [{}],
food: [{}],
potion: [{}],
},
gear: {
weapon: [{}],
head: [{}],
chest: [{}],
gloves: [{}],
legs: [{}],
shoes: [{}],
earrings: [{}],
ring: [{}],
necklace: [{}],
},
gold: {
type: Number,
default: 0,
},
bagSize: {
type: Number,
default: 20,
},
skills: {
type: [userSkills],
default: defaultSkills,
},
});
and my problem is with inventory: [].
In my code I manually push items in my array which save properly in the database, but whenever I wanna modify a field of one of my array object, it won't update it like a qty for example:
player.inventory[i].qty += 1; //Where player is my model containing my inventory
...
player.save()
.then((doc) => console.log("DOC", doc))
.catch((err) => console.log("ERR", err));
and the doc returned look something like this:
...
inventory: [
{
_id: 626a1468cdbeced19102942d,
slug: 'crude_campfire',
qty: 1,
charges: 19,
efficiency: -0.2
},
{ _id: 626a14e7530afad2c5c1894c, slug: 'wheat', qty: 1 },
{ _id: 626a14f1530afad2c5c1894d, slug: 'carrot', qty: 1 },
{ _id: 626a150f530afad2c5c1894e, slug: 'potato', qty: 1 },
{ _id: 626a155b530afad2c5c1894f, slug: 'red_bream', qty: 1 },
{ _id: 626a15b5530afad2c5c18950, slug: 'bone', qty: 1 },
{ _id: 626a15c9530afad2c5c18951, slug: 'clam_shell', qty: 1 },
{ _id: 626a15d5530afad2c5c18952, slug: 'stone', qty: 8 },
{ _id: 626a15df530afad2c5c18953, slug: 'taeroot', qty: 1 },
{ _id: 626a15e9530afad2c5c18954, slug: 'shiny_pendant', qty: 1 },
{ _id: 626a15fd530afad2c5c18955, slug: 'sickleweed', qty: 1 },
{ _id: 626a1625530afad2c5c18956, slug: 'ceruleaf', qty: 1 },
{ _id: 626a1dba272403ea21fb71ef, slug: 'stone', qty: 1 },
{ _id: 626a1e4030a144f8179789e0, slug: 'birch_log', qty: 22 }, <- IMPORTANT
{ _id: 626a1e72372733f90cfdc003, slug: 'tree_twig', qty: 2 },
{ _id: 626a1e9a372733f90cfdc004, slug: 'honey', qty: 2 }
],
...
where you can see that my birch_log qty is now at 22. But whenever I go check in the database,
I don't know what's going on, and I'm kind of tired right, so I'll go to bed hoping someone can save me tomorrow :)
Thanks and have a good night ! :)
To update the object having {slug: birch_log}, you can use the following query:
player.update( {user : 123456 , inventory.slug : "birch_log" } ,
{$inc : {"inventory.$.qty" : 1} });
instead of using save() with promise, you can use update() query with async await. It will be short and clean code.
This answer fixed my problem
Since i'm using an "anonymous" array or whatever, mongoose doesn't know what changes to track in it so I need to tell it that it's been modified !
Say I have the following dataLayer:
{
ecommerce: {
currencyCode: "USD",
purchase: {
actionField: {
id: "1a6d5021",
affiliation: "Online Store",
revenue: 40,
tax: 0,
shipping: "",
coupon: ""
},
products: [
{
name: "Product 1",
id: "123",
price: 40,
category: null,
quantity: 1,
coupon: "disc10",
type: "Service A"
},
{
name: "Product 4",
id: "456",
price: 40,
category: null,
quantity: 1,
coupon: "disc10",
type: "Service B"
}
]
}
}
}
So in the product array, category always has value null. How can I push the same value as type respectively for each product, whilst leaving everything else in the dataLayer untouched?
Ultimately the final result that I am trying to achieve would be like this:
{
ecommerce: {
currencyCode: "USD",
purchase: {
actionField: {
id: "1a6d5021",
affiliation: "Online Store",
revenue: 40,
tax: 0,
shipping: "",
coupon: ""
},
products: [
{
name: "Product 1",
id: "123",
price: 40,
category: "Service A",
quantity: 1,
coupon: "disc10",
type: "Service A"
},
{
name: "Product 4",
id: "456",
price: 40,
category: "Service B",
quantity: 1,
coupon: "disc10",
type: "Service B"
}
]
}
}
}
It be easy with a single product, but I quite can't find how to do it when multiple products.
Thanks in advance for your help.
If I understand your requirement correctly that you want to assign the type value in the category for each products object. If Yes, Its a straight forward.
Working Demo :
const productObj = {
ecommerce: {
currencyCode: "USD",
purchase: {
actionField: {
id: "1a6d5021",
affiliation: "Online Store",
revenue: 40,
tax: 0,
shipping: "",
coupon: ""
},
products: [{
name: "Product 1",
id: "123",
price: 40,
category: null,
quantity: 1,
coupon: "disc10",
type: "Service A"
},
{
name: "Product 4",
id: "456",
price: 40,
category: null,
quantity: 1,
coupon: "disc10",
type: "Service B"
}
]
}
}
};
productObj.ecommerce.purchase.products.forEach((obj) => obj.category = obj.type);
console.log(productObj);
There are two options.
Push the whole ecommerce object again, with all fields set now. It results in a bit of a mess in DL and certain timing issues one has to keep in mind when implementing tracking.
Remove/delay the first ecommerce push till you have all info and only push the ecommerce object once.
In most cases, 2 is the best option. 1 can be justified when the event relying on the ecommerce object has to fire before categories become available to the front-end.
Try
function createCategoryFn(category) {
return (properties) => {
return {
name: "",
id: "",
price: 0,
category: category,
quantity: 1,
coupon: "",
type: category,
...properties
};
};
}
const createSportsProduct = createCategoryFn('Sports');
const tennisProduct = createSportsProduct({ name: 'tennis racket', id: 1, price: 100 });
const basketballProduct = createSportsProduct({ name: 'basketball', id: 2, price: 100 });
console.log(tennisProduct);
console.log(basketballProduct)
So I have a object array I am using to store data for my app. It's in a store file, using mobx. How come when I write some code to alter or add to a object in the array, that data is deleted upon a refresh?
Is there anyway to permanently alter the object so it persists through different sessions? (trying to avoid using a database, using the object as my database).
Here's a sample of the data
Bars: [
{
Name: "Bull and Finch Neighborhood Pub",
Address: "11520 24 St SE",
Latitude: 50.95231,
Longitude: -114.00132,
Reviews: [],
Likes: 0,
Specials: [
{
Monday: [
{ Desc: "Burgers", Price: "50% Off", Cat: "Burgers" },
{ Desc: "Coors Banquet Pints", Price: 5.5, Cat: "Drinks" }
],
Tuesday: [
{ Desc: "Pizza and a Jug", Price: 26, Cat: "Pizza" },
{ Desc: "Draft Jugs", Price: "2$ Off", Cat: "Drinks" }
],
Wednesday: [
{ Desc: "Wings", Price: 0.45, Cat: "Wings" },
{ Desc: "Import Draught", Price: "1$ Off", Cat: "Drinks" }
],
Thursday: [
{ Desc: "Steak Sandwich", Price: 7.99, Cat: "Sandwiches" },
{ Desc: "Desserts", Price: "50% Off", Cat: "Dessert" },
{ Desc: "Bottles of Wine", Price: "50% Off", Cat: "Drinks" }
],
Friday: [
{ Desc: "Entrees", Price: "2$ Off", Cat: "Entrees" },
{ Desc: "Original 16 Pints", Price: 5.5, Cat: "Drinks" }
],
Saturday: [
{ Desc: "Boneless Wings", Price: 7.95, Cat: "Wings" },
{ Desc: "Specialty Drinks", Price: "1$ Off", Cat: "Drinks" }
],
Sunday: [
{ Desc: "BBQ Brisket", Price: 14.99, Cat: "Entrees" },
{ Desc: "Ribs", Price: 0.45, Cat: "Entrees" },
{ Desc: "Jack Daniels", Price: 5.25, Cat: "Drinks" }
]
}
]
}
Simply put your data needs to be persisted and reloaded upon each request either using a database or local storage.
It sounds like the best way forward for you would be to use Node.js to write to file, and save your object to file. Then later read from file.
Take a look at the fs API for how to do that.
https://nodejs.org/api/fs.html
I am building best employee survey project using surveyjs. When working in matrix type, I get trouble how to change text "employee1,employee2,employee3" with real name employee
see picture.
so far here is my code:
var json = {
title: "Software developer survey.",
pages: [
{
title: "Part 1",
questions: [
{
type: "matrix",
name: "Question1",
//isAllRowRequired: true,
title: "Please indicate if you agree or disagree with the following statements",
columns: [
{
value: 1,
text: "Strongly Disagree"
}, {
value: 2,
text: "Disagree"
}, {
value: 3,
text: "Neutral"
}, {
value: 4,
text: "Agree"
}, {
value: 5,
text: "Strongly Agree"
}
],
rows: [
{
value: "valueEmployee1",
text: "Employee1 ![Employee1](https://bezone.files.wordpress.com/2008/02/soeharto.jpg =50x50)"
}, {
value: "valueEmployee2",
text: "Employee2"
}, {
value: "valueEmployee3",
text: "Employee3"
}
]
},
//
]
},
thank you for your help
You can add matrix rows dynamically:
var json = {
questions: [
{
type: "matrix",
name: "score",
title: "Please score the employees in the list",
columns: [
{
value: 1,
text: "Bad"
}, {
value: 2,
text: "So-so"
}, {
value: 3,
text: "Good enough"
}, {
value: 4,
text: "Good"
}, {
value: 5,
text: "Excellent"
}
],
rows: [
]
}
]
};
window.survey = new Survey.Model(json);
var q = survey.getQuestionByName("score");
var employees = [{ id: "id1", name: "John"}, { id: "id2", name: "Ben"}, { id: "id3", name: "Chack"}]
employees.forEach(function(employee) {
q.rows.push(new Survey.ItemValue(employee.id, employee.name));
});
Here is the working sample - https://plnkr.co/edit/OMBt1n02Qc8f5znUmOHE?p=preview