how to form an array of inputs on form submit - javascript

i want to create a form which can submit the values as the below format
[
{
"_id": "59817f39808768495728ae94",
"updatedAt": "2017-08-02T07:33:00.578Z",
"createdAt": "2017-08-02T07:28:57.310Z",
"farmId": "tesform",
"farmName": "tesform",
"gatewayId": "123",
"gatewayName": "testform getway",
"userId": "abc#kisanraja.com",
"description": "test details 1st time",
"nodes": [
{
"permanentAddress": "",
"description": "with temparature",
"name": "Node 1",
"_id": "5981802c808768495728ae9b",
"sensors": [
{
"units": "C",
"max": 60,
"min": -6,
"name": "temparature",
"_id": "5981802c808768495728ae9c"
}
],
"shortAddress": 0
},
{
"permanentAddress": "",
"description": "with soil moisture and voltage",
"name": "Node 2",
"_id": "5981802c808768495728ae98",
"sensors": [
{
"units": "C",
"max": 60,
"min": -6,
"name": "moisture",
"_id": "5981802c808768495728ae9a"
},
{
"units": "C",
"max": 60,
"min": -6,
"name": "voltage",
"_id": "5981802c808768495728ae99"
}
],
"shortAddress": 0
}
]
}
]
I want to create the nodes and sensors dynamically using jquery. Each farm should have more than one node, and each node may have more than one sensor.

You can add different class names to different level inputs, then each them, when came cross nodes, add nodes to object, when came cross to sensors, then add sensors to array as an attributes of node object named sensors when submit the form:
$("#form").submit(function() {
//deal with them;
});

Related

Drag and drop with express and mongoose

I have a linktree clone project where you can build your portofolio from multiple urls. At the moment, the links are in a chronological order (when they were put in the database). I want to be able to modify their order but don't know what is the best aproach or where to start. The only thing that I could think of was an array stored in Page Schema with the orders of id urls(named pageLayout). I'm a beginner and still learning
The JSON generated is something like this:
{
"theme": "white",
"banner": "uploads/banners/default.png",
"avatar": "uploads/avatars/default.png",
"pageLayout": [],
"_id": "62825469a716308778488106",
"urlName": "test",
"title": "TEST",
"description": "TEST PAGE",
"category": "Photography",
"socialMedia": {
"Instagram": "test",
"Twitter": "test.twitter",
"Tiktok": "test.tiktok"
},
"__v": 0,
"blocks": [
{
"blockType": "links",
"_id": "628a646534a2f89b190bb309",
"title": "Learn Blockchain1",
"user": "628172dff7334877a4ecf19e",
"pageId": "62825469a716308778488106",
"__v": 0,
"links": [
{
"display": true,
"thumbnail": "uploads/linkThumbnails/default.png",
"_id": "628a732533d84c9cf988993b",
"blockId": "628a646534a2f89b190bb309",
"title": "First Project1",
"url": "www.test.com/ethkasbdj",
"user": "628172dff7334877a4ecf19e",
"__v": 0
},{
"display": true,
"thumbnail": "uploads/linkThumbnails/default.png",
"_id": "628a732533d84c9cf988993b",
"blockId": "628a646534a2f89b190bb309",
"title": "First Project2",
"url": "www.test.com/ethkasbdj",
"user": "628172dff7334877a4ecf19e",
"__v": 0
}
],
"id": "628a646534a2f89b190bb309"
}
],
"id": "62825469a716308778488106"

How can I .filter an object by elements within an array inside an object?

I've been playing around trying to learn in an API project using Postman and conducting tests using JavaScript. So far, I have succeeded with the help of reading on websites and watching YouTube videos. Of course, previous tests and playing around have been fairly easy but now I came to a stop. I really tried to figure this out for several weeks but I need further guidance, a push in the right direction or direct help.
What I'm trying to do is to filter out some of the response to only view objects that contain specific data.
To do that, I'm using a filter where I want all products containing a specific value inside an array "product_option_values".
My first approach was to see if I could sort products having any values from the first array, and it worked. It filters just fine.
var filterSmall = jsonData.products.filter(fs => fs.associations.product_option_values);
My next approach was to get to my goal of filtering out products according to specific values inside this array. I tried many simple .(dot) combinations and pointing to [index] to access it without any luck. (I must add that I know how to access this from a specific product, but that way doesn't work when filtering).
I've also tried other approaches such as:
var filterSmall = jsonData.products.filter(fs => fs.associations["product_option_values", 0, "name"] === "S");
and other similar combinations.
This is a very shortened sample of the structure of "products" which in its full form consists of 20 products and far more values inside of it:
{
"products": [
{
"id": 16,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Mountain fox notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "22"
},
{
"id": "23"
}
]
}
},
{
"id": 17,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Brown bear notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "23"
},
{
"id": "24"
}
]
}
}
]
}
and here is a small and expanded sample from product_option_values:
{
"product_option_values": [
{
"id": 1,
"id_attribute_group": "1",
"color": "",
"position": "0",
"name": "S"
},
{
"id": 2,
"id_attribute_group": "1",
"color": "",
"position": "1",
"name": "M"
},
{
"id": 3,
"id_attribute_group": "1",
"color": "",
"position": "2",
"name": "L"
}
]
}
How do I proceed? Did I do anything correct or even close to it?
Perhaps I've been staring at this for too long.
Thanks in advance.
If you want to compare nested attributes you have to transform the objects (e.g. by using a map operation), so that the relevant attributes are easily accessible for a comparison. If you want to filter by product_option_value id, you could do something like this:
const jsonData = {
"products": [
{
"id": 16,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Mountain fox notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "22"
},
{
"id": "23"
}
]
}
},
{
"id": 17,
"manufacturer_name": "Graphic Corner",
"quantity": "0",
"price": "12.900000",
"indexed": "1",
"name": "Brown bear notebook",
"associations": {
"categories": [
{
"id": "2"
},
{
"id": "6"
}
],
"product_option_values": [
{
"id": "23"
},
{
"id": "24"
}
]
}
}
]
};
const sample = {
"product_option_values": [
{
"id": 22,
"id_attribute_group": "1",
"color": "",
"position": "0",
"name": "S"
},
{
"id": 2,
"id_attribute_group": "1",
"color": "",
"position": "1",
"name": "M"
},
{
"id": 3,
"id_attribute_group": "1",
"color": "",
"position": "2",
"name": "L"
}
]
};
const ids = sample.product_option_values.map((el) => String(el.id));
console.log(ids);
const filtered = jsonData.products.filter((fs) => fs.associations.product_option_values.map((e) => e.id).some((f) => ids.includes(f)));
console.log(filtered);

Deleting a nested array object by comparing two conditions

I want to delete an object from a nested array by comparing another field in the Mongo database by using mongoose in javascript. Here is my schema.
{
"userId": "5e80e3021c5c461433807173",
"selected": [
{
"productId": "5e80e664602315159a2959da",
"selectedQty": 1,
"isAvailable": true,
"name": "Nike Mountain Shoe",
"category": "Shoes",
"qty": 2497,
"price": 1500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
},
{
"productId": "5e80e4893db96d14947dce80",
"selectedQty": 1,
"isAvailable": false,
"name": "Blimper Men Shoe",
"category": "Shoes",
"qty": 2497,
"price": 500,
"imageUrl": "aHR0cHM6Ly93d3cucmFua2FuZHN0eWxlLmNvbS9tZWRpYS9wcm9kdWN0cy9qL2phbWVzLXBlcnNlLWxvbmctc2xlZXZlLXQtc2hpcnQtbWVucy1sb25nLXNsZWUuanBn"
}
]
}
How can I delete a product by comparing userId and productId?

Change JSON format/layout

I have a universal variable on my website which includes line items with relevant details. These line items are reflective of what the user has in their cart. I am integrating with a third party who require the data passed through to them to be formatted slightly different. The below is the data layer currently on my website:
"lineItems": [
{
"product": {
"id": "s83y016b5",
"sku_code": "s83y016b5",
"url": "/en-gb/jeans/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/shirt.jpeg",
"name": "Jeans",
"manufacturer": "",
"category": "Everyday Wear",
"stock": 116,
"currency": "GBP",
"unit_sale_price": 16,
"unit_price": 16,
"size": "6-9 Months",
"color": "Indigo"
},
"quantity": 1
}
]
The below is what format the third party needs:
"lineItems": [
{
"sku": "s83y016b5",
"name": "Jeans",
"description": "A super great pair of jeans.",
"category": "Everyday Wear",
"other": {"fieldName": "This can be a string or any value you like"}
"unitPrice": 11.99,
"salePrice": 11.99,
"quantity": 2,
"totalPrice": 23.98
"imageUrl": "http://www.my-website.com/a/p/shirt.jpeg",
"productUrl": "http://www.my-website.com/index.php/shirt.html",
}]
Obviously this needs to be dynamic based on the products in the cart. What I intend to do is use javascript to amend the data and send this to the third party via Google Tag Manager.
Any help would be greatly appreciated. Any questions welcome.
This should be close to what you're looking for.
let oldLineItems = "your object";
let newLineItems = {};
newLineItems.lineItems = [];
for (let i in oldLineItems.lineItems) {
newLineItems.lineItems[i] = {};
for (let key in oldLineItems.lineItems[i].product)
{
newLineItems.lineItems[i][key] = oldLineItems.lineItems[i].product[key];
}
}
See code below.
I'm not sure how your lineItems object is set up, but below I just created an array called line Items. If line items is a key in an object which I suspect from your snippet above, you will have to go a level deeper in the for loops used in my example below.
Simply add further details to the new object in the nested for in loops below.
var lineItems =
[
{
"product": {
"id": "s83y016b5",
"sku_code": "s83y016b5",
"url": "/en-gb/jeans/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/shirt.jpeg",
"name": "Jeans",
"manufacturer": "",
"category": "Everyday Wear",
"stock": 116,
"currency": "GBP",
"unit_sale_price": 16,
"unit_price": 16,
"size": "6-9 Months",
"color": "Indigo",
"description": 'Some random description'
},
"quantity": 1
},
{
"product": {
"id": "s83y01699",
"sku_code": "s83y01699",
"url": "/en-gb/pants/p/s83y016b5",
"image_url": "http://www.my-website.com/a/p/pants.jpeg",
"name": "Pants",
"manufacturer": "",
"category": "Casual Wear",
"stock": 90,
"currency": "au",
"unit_sale_price": 14,
"unit_price": 14,
"size": "6-9 Months",
"color": "Indigo",
"description": 'Some random description'
},
"quantity": 14
},
];
var newLineItems = [];
for(var char in lineItems){
// Adding some values to newLineItems.
newLineItems.push({
sku: lineItems[char].product.sku_code,
name: lineItems[char].product.name,
description: lineItems[char].product.description,
category: lineItems[char].product.category,
quantity: lineItems[char].quantity
});
}
console.log(JSON.stringify(newLineItems));

Functional approach to transform an array of objects into a hierarchical structure with aggregates

I'm looking for a functional solution to map this object into a hierarchical structure and group by practice, subjectarea, and asset so that this object:
var data = {
"research": [
{ "practice": "Talent Management", "id": "20992", "title": "Creating a Culture of Leadership", "subjectArea": "Talent Strategy" },
{ "practice": "Talent Management", "id": "20993", "title": "Talent Economics", "subjectArea": "Talent Strategy" },
{ "practice": "Talent Management", "id": "20990", "title": "Finding the Right Talent", "subjectArea": "Recruiting" },
{ "practice": "Human Resources", "id": "20994", "title": "Tips from the Front", "subjectArea": "HR Training" },
{ "practice": "Human Resources", "id": "20995", "title": "Reward Programs", "subjectArea": "Employee Engagement" },
]
}
gets transformed into this (count the number of items for a practice and add the subject areas for the practice as children, count the number of items for a subject area within the practice and add the asset (id and title) as children for the subject area):
{
"name": "research",
"children": [
{
"name": "Talent Management", "size": 3, "children": [
{
"name": "Talent Strategy", "size": 2, "children": [
{ "name": "Creating a Culture of Leadership", "id": "20992", "size": 1 },
{ "name": "Talent Economics", "id": "20993", "size": 1 }
]
},
{
"name": "Recruiting", "size": 1, "children": [
{ "title": "Finding the Right Talent", "id": "20990", "size": 1 },
]
}
]
},
{
"name": "Human Resources", "size": 2, "children": [
{
"name": "HR Training", "size": 1, "children": [
{ "name": "Tips from the Front", "id": "20994", "size": 1 },
]
},
{
"name": "Employee Engagement", "size": 1, "children": [
{ "name": "Reward Programs", "id": "20995", "size": 1 },
]
}
]
},
]
}
I haven't been able to sort out the map and reduce combination to use to yield the correct result with counts, and appreciate any insight.
It might not be what SO is for (as per comments above), but in case anyone needs to transform an array of flat objects into a hierarchical structure that you can simply specify the nesting order, number of levels, and field names, here is a recursive function to do so - https://github.com/lance-p/configurable-transform-flat-array-to-nested. Hope it might be of help to others.

Categories

Resources