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
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)
Ahoy,
i am using built in gtm plugin on wordpress/woocomerce and it gets the following content:
cartContent: {
totals: {
applied_coupons: [],
discount_total: 0,
subtotal: "2844.715447",
total: "2844.715447"
},
items: [
{
id: "K-GG-V4-S",
name: "Grill Gazowy Koler Relish v4 5 palników 17,2kW",
sku: "K-GG-V4-S",
category: "Grille Gazowe",
price: 3499,
stocklevel: 41,
quantity: 1
},
{
id: "5900000002453",
name: "Fartuch Koler",
sku: "5900000002453",
category: "Wszystkie produkty",
price: 0,
stocklevel: null,
quantity: 1
},
{
id: "6600006666664",
name: "Pokrowiec + wąż i reduktor",
sku: "6600006666664",
category: "Wszystkie produkty",
price: 0,
stocklevel: null,
quantity: 1
}
]
},
but I have to implement the entire content of the basket into something that is to change dynamically depending on the number of products:
<script>
wph('track', 'AddToCart', {
content_type: 'category',
contents: [{
id: 'PRODUKT_ID1',
name: 'NAZWA_PRODUKTU1',
category: 'KATEGORIA_PRODUKTU1',
ean: 'PRODUKT_EAN_ID1',
price: 20.15,
in_stock: true
quantity: 1
weight : 'WAGA PRODUKTU'
}, {
id: 'PRODUKT_ID2',
name: 'NAZWA_PRODUKTU2',
category: 'KATEGORIA_PRODUKTU2',
ean: 'PRODUKT_EAN_ID2',
price: 20.15,
in_stock: true
quantity: 1
weight : 'WAGA PRODUKTU'
}]
})
</script>
How can I do this in GTM? I must admit that it was a bit too much for me, because I usually didn't need the entire basket.
How Can I loop through this array of objects and change it so that the individual menu items are nested in the object menu_name?
const menus = [
{ menu_name: 'Entre', id:0 },
{
name: 'Soup',
price: 14.99,
id:1
},
{
name: 'Chips & Salsa',
price: 7.99,
id:2
},
{
name: 'Chicken Nuggets',
price: 12.99,
id:3
},
{ menu_name: 'Sides', id:4 },
{
name: 'Fries',
price: 4.99,
id:5
},
{
name: 'Drinks',
price: 2.99,
id:6
},
{
name: 'Onion Rings',
price: 5.99,
id:7
},
];
the end result should look like this for each menu_name object, where an array of menus is nested in the menu_name object
{
menu_name: 'Sides',
menu: [
{
name: 'Fries',
price: 4.99,
},
{
name: 'Drinks',
price: 2.99,
},
{
name: 'Onion Rings',
price: 5.99,
},
],
},
You can easily achieve this using reduce and object destructuring
const menus = [
{ menu_name: "Entre", id: 0 },
{
name: "Soup",
price: 14.99,
id: 1,
},
{
name: "Chips & Salsa",
price: 7.99,
id: 2,
},
{
name: "Chicken Nuggets",
price: 12.99,
id: 3,
},
{ menu_name: "Sides", id: 4 },
{
name: "Fries",
price: 4.99,
id: 5,
},
{
name: "Drinks",
price: 2.99,
id: 6,
},
{
name: "Onion Rings",
price: 5.99,
id: 7,
},
];
const result = menus.reduce((acc, curr) => {
const { menu_name } = curr;
if (menu_name) {
acc.push({ menu_name, menu: [] });
} else {
const { name, price } = curr;
acc[acc.length - 1].menu.push({ name, price });
}
return acc;
}, []);
console.log(result);
var newMenu = [];
menus.forEach(menu=>{
if(menu.menu_name){
newMenu.push({...menu, menu: []})
}else{
newMenu[newMenu.length-1].menu.push(menu)
}
});
Suppose, I have an array.
const arr = [
{
category: 'Diner',
item: [
{name: 'Chicken Rice', price: 200},
{name: 'Mutton Rice', price: 300},
],
},
{
category: 'Breakfast',
item: [
{name: 'Tea Bisuit', price: 100},
{name: 'Bread Butter', price: 300},
{name: 'Green Tea', price: 80},
],
},
];
How can I filter the array according to the item name?
For example, how can I filter the array with the item name Green Tea?
Output must be like this:
arr = [
{
category: 'Breakfast',
item: [
{name: 'Green Tea', price: 80},
],
},
];
You could map through arr, with each element, filter item to which match the term
After that, filter the arr again to reject the elements whose item is empty
const arr = [ { category: "Diner", item: [ { name: "Chicken Rice", price: 200 }, { name: "Mutton Rice", price: 300 }, ], }, { category: "Breakfast", item: [ { name: "Tea Bisuit", price: 100 }, { name: "Bread Butter", price: 300 }, { name: "Green Tea", price: 80 }, ], }, ]
const term = "Green Tea"
const res = arr
.map((categoryAndItems) => ({
category: categoryAndItems.category,
item: categoryAndItems.item.filter((item) => item.name === term),
}))
.filter((categoryAndItems) => categoryAndItems.item.length > 0)
console.log(res)
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);