Angular: Create 2 objects from 1 objects in an array - arrays

I have an array as shown below
[
{
"id": 42,
"name": "updateDate",
"displayName": "UPDATE DATE",
"uiControl": "DATERANGE",
"dataType": "STRING",
"uiOrder": 1,
},
{
"id": 44,
"name": "name",
"displayName": "First Name",
"uiControl": "TEXTBOX",
"dataType": "STRING",
"uiOrder": 1,
},
]
I want to filter objects in this array with the property UiControl === DATERANGE and create 2 objects from the filtered object and also append'FROM' and 'TO' to the name and the displayname property as shown below
final output:
[{
"id": 42,
"name": "fromupdateDate", // 'from'appended to name property
"displayName": "FROM UPDATE DATE", // 'FROM' appended to displayName property
"uiControl": "DATERANGE",
"dataType": "STRING",
"uiOrder": 1,
},
{
"id": 42,
"name": "toupdateDate", // 'to' appended to name property
"displayName": "TO UPDATE DATE", // 'TO' appended to displayName
"uiControl": "DATERANGE",
"dataType": "STRING",
"uiOrder": 1,
},
{ // this object stays the same
"id": 44,
"name": "name",
"displayName": "First Name",
"uiControl": "TEXTBOX",
"dataType": "STRING",
"uiOrder": 1,
}]
We can create such an array in multiple ways, but I want to find an optimized way of creating such an object.

flatMap will do what you want:
const input = [{
"id": 42,
"name": "updateDate",
"displayName": "UPDATE DATE",
"uiControl": "DATERANGE",
"dataType": "STRING",
"uiOrder": 1,
},
{
"id": 44,
"name": "name",
"displayName": "First Name",
"uiControl": "TEXTBOX",
"dataType": "STRING",
"uiOrder": 1,
},
];
const output = input.flatMap(v => v.uiControl === "DATERANGE"
? [
{ ...v,
name: `from${v.name}`,
displayName: `FROM ${v.displayName}`
},
{ ...v,
name: `to${v.name}`,
displayName: `TO ${v.displayName}`
}
]
: [v]);
console.log(output);
Note that you'll have to compile this with esnext support. See this question for more details. It also won't work out of the box on IE or Edge. If that's a problem in your use case, you'll want to use a polyfill or rely on map + reduce like this:
const output = input.map(v => v.uiControl === "DATERANGE"
? [
{ ...v,
name: `from${v.name}`,
displayName: `FROM ${v.displayName}`
},
{ ...v,
name: `to${v.name}`,
displayName: `TO ${v.displayName}`
}
]
: [v])
.reduce((acc, cur) => (acc.push(...cur), acc), []);

Related

React JS & Axios : How to post array of objects with axios form data

I have an endpoint that accept array of objects, but I don't know how to post the data based on user input using onChange. Here's my code
const [valuesInput, setValuesInput] = useState({
title: "",
question: [{ text: "", values: "" }],
category: "",
});
const data = new FormData();
data.append("feedback_title", valuesInput.title);
data.append("feedback_question", valuesInput.question);
data.append("kategori_feedback_id", valuesInput.category);
<input key={c} type="text" onChange={(e) => {setValuesInput({...valuesInput,question: [
{text: e.target.value,values: e.target.value,},],});}} />
the things that I want to achieve are like this code :
"data": [
{
"pertanyaan_feedback_id": 1,
"kategori_feedback_id": null,
"feedback_title": "test",
"feedback_question": [
{
"values": "dsfdsf",
"text": "vvfddf"
},
{
"values": "dsfdsf",
"text": "vvfddf"
},
{
"values": "dsfdsf",
"text": "vvfddf"
},
{
"values": "dsfdsf",
"text": "vvfddf"
}
],
"created_at": "2022-07-24T04:56:06.000000Z",
"updated_at": "2022-07-24T04:56:06.000000Z"
}
are there any suggestion what should I do to post array of object based on my code above? I'm very thankful if you want to give soe advice to solve my problem..
I think when you feedback a product, you post with NewFeedback function include 2 or 3 fields pertanyaan_feedback_id and feedback_title or kategori_feedback_id if you need. After that, If have new a question, you go on post with NewQuestion function include 2 fields text and values
When getAll you have data such as expect
The last data have a id for feedback_question and data, that is very easy when update or delete.
"data": [
{
"id": 1, // Add id
"pertanyaan_feedback_id": 1,
"kategori_feedback_id": null,
"feedback_title": "test",
"feedback_question": [
{
"id": 1, // Add id
"values": "dsfdsf",
"text": "vvfddf"
},
{
"id": 2, // Add id
"values": "dsfdsf",
"text": "vvfddf"
},
{
"id": 3, // Add id
"values": "dsfdsf",
"text": "vvfddf"
},
{
"id": 4, // Add id
"values": "dsfdsf",
"text": "vvfddf"
}
],
"created_at": "2022-07-24T04:56:06.000000Z",
"updated_at": "2022-07-24T04:56:06.000000Z"
}

JSON Schema: First Item of Array

I need a JSON array of arbitrary length. Each item in the array is a JSON object, they all have same keys and types.
But the schema should make one exception: the first object doesn't need all keys, so schemas required list should be shorter for the first item.
I tried schemas with "items" and "prefixItems" without luck. It seems that "prefixItems" will be ignored independently of draft version when used with "items". Because array can be of arbitrary length, I guess I cannot use multiple schemas with "items".
{
"description": "Schema for array data",
"$schema": "http://json-schema.org/draft-2020-12/schema#",
"version": "0-0-6",
"type": "object",
"required": [
"data"
],
"properties": {
"data": {
"type": "array",
"prefixItems": [{
"type": "object",
"required": [
"name"
],
"properties": {
"name" : { "type" :"string" },
"age" : { "type" : "number" },
"city" : { "type" : "string" }
}
}],
"items":
{
"type": "object",
"required": [
"name", "age", "city"
],
"properties": {
"name" : { "type" :"string" },
"age" : { "type" : "number" },
"city" : { "type" : "string" }
}
}
}
}
}
My data:
{
"data" : [
{
"name": "Tom"
},
{
"name": "Ben",
"age": 32,
"city": "Berlin"
},
{
"name": "Mike",
"age": 40,
"city": "Boston"
}
]
}
For validation I use:
https://www.jsonschemavalidator.net/
https://jsonlint.com/
Validation of my example gives error for first item:
"Required properties are missing from object: age, city."
Use additionalItems instead of items.
Schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"prefixItems": [
{ "type": "number" },
{ "type": "string" },
{ "enum": ["Street", "Avenue", "Boulevard"] },
{ "enum": ["NW", "NE", "SW", "SE"] }
],
"additionalItems": {
"type": "number"
}
}
Data that validates:
[
1600,
"Pennsylvania",
"Avenue",
"NW",
1]
The issue you're running into is that the validators only support up to draft 7 of the standard, and using items the way we both want to was new in the 2019 draft. It's not supported yet.

Collect all property names of filtered objects based on criteria

I have an array as below
[
{
"id": 82,
"name": "fromcreate_date",
"displayName": "From Create Date",
"uiControl": "DATERANGE",
},
{
"id": 82,
"name": "tocreate_date",
"displayName": "To Create Date",
"uiControl": "DATERANGE",
},
{
"id": 83,
"name": "p_is_ut",
"displayName": "Is UT",
"uiControl": "HIDDEN",
}
]
I want to filter this array based on uiControl === "DATERANGE" and get only the name property of the filtered objects in an array as below.
outputArray = ["fromcreate_date", "tocreate_date"]
Try this simple one liner:
const outputArray = this.data.filter(x => x.uiControl === 'DATERANGE').map(x => x.name);

Unable to fetch nested data into PrimeNG table

I am very new to PrimeNG table and I tried to load nested json doc into table, but I am unable to access data of nested array.
I am able to load flatten json in p-table but in nested json unbale to fetch the nested data
EX: { field: "address", header: "Address ", title: "Person Address" }
for this code am getting [object, object] in address column
I also tried with indexing like field address[0].id but not getting
This sample data of json fetching from API
Retailers=[
{
"id":1,
"name":"Preeti Mobiles",
"address":[{
"id": 1,
"addressLine1":"31/A, 2nd cross, 1st floor",
"addressLine2":"Tirumala Towers, Mejestic",
"city":"Bengaluru",
"state":"Karnataka",
"country":"India",
"pincode":500006
},
{
"id": 2,
"addressLine1":"31/A, 2nd floor",
"addressLine2":"Hebbal",
"city":"Bengaluru",
"state":"Karnataka",
"country":"India",
"pincode":500091
}
]
},
{
"id": 2,
"name": "Nanda Electronics",
"address": [{
"id": 1,
"addressLine1": "78/A, 4nd cross, 1st floor",
"addressLine2": "Kishan Empower, RR Nagar",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"pincode": 520006
}
]
},
{
"id": 3,
"name": "Kishan Electronics",
"address": [{
"id": 1,
"addressLine1": "86/A, 1nd cross, 2st floor",
"addressLine2": "Nanda Building, Hebbala",
"city": "Bengaluru",
"state": "Karnataka",
"country": "India",
"pincode": 520036
}
]
}
]
Type script code
ngOnInit() {
this.person();
}
person(){
this.personService.getpersonList().subscribe(data=>{
this.person_details = data;
console.log(this.person_details);
});
this.retailer_list = [
{ field: "name", header: "Name", title: "person Name" },
{ field: "address", header: "Address ", title: "person Address" }
];
}
}

Sphere.IO : How can we set a custom attribute during product creation?

After we have created a productType and have its type id, how can we set custom attributes during product create API calling. Until now what I've been doing is 1st, create the product, then update the custom attribute. But, I am unable to do so with the required attributes.
you can supply all the custom attributes in the product creation payload. Example - here I provide values for attributes brand on the master variant, for a previously created product type which specifies that there is an attribute called Brand:
{
"productType": {
"typeId": "product-type",
"id": "6c06998a-c576-4a8d-8ace-dc306d70e1d1"
},
"name": {
"en": "Some Product"
},
"slug": {
"en": "product_slug_sdfsdfsdfdsdfsdfdssdf"
},
"masterVariant": {
"prices": [{
"value": {
"centAmount": 2500,
"currencyCode": "EUR"
},
"country": "US"
}],
"attributes": [{
"name": "brand",
"value": "Hugo Boss"
}]
},
"variants": [{
"prices": [{
"value": {
"centAmount": 2600,
"currencyCode": "EUR"
},
"country": "US"
}],
"attributes": [{
"name": "brand",
"value": "Hugo Boss"
}]
}]
}

Resources