How to change the value of a state object? - reactjs

I created an add button for adding new objects to the state. The logic is firstly, copy the last object of the state and after add at the end of it.
I can write the code so far but the problem is, I have to change the value and the key of the objects.
Note: I have to change the keys because I'm creating input fields with the keys and they should be unique.
This is my code :
addField() {
const index = (this.state?.fields.length)
console.log("state: ", this.state)
const newFields = [this.state.fields[index]].fill(this.state.fields[index - 1])
this.setState(
{
fields: [...this.state.fields, ...newFields],
},
() => {
console.log("updated state", this.state);
}
);
}
outputs:
state:
{
"fields": [
[
{
"key": "input_field_name",
"value": "This is a value"
},
{
"key": "field_name",
"value": "field name"
},
{
"key": "datatype",
"value": "text"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
]
]
}
updated state:
{
"fields": [
[
{
"key": "input_field_name",
"value": "This is a value"
},
{
"key": "field_name",
"value": "field name"
},
{
"key": "datatype",
"value": "text"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
],
[
{
"key": "input_field_name",
"value": "This is a value 2"
},
{
"key": "field_name",
"value": "field name's value 2"
},
{
"key": "datatype",
"value": "text"
},
{
"key": "Datatype",
"value": "Label 2"
}
]
]
}
my state:
interface IDetailsPageState {
fields: IFieldDefinition[][];
activeFields: {
key: number;
fields: IFieldDefinition[];
};
}
How can I change the key and value of these objects?
<div key={`${this.state.activeFields.key}-${field.key}`}>
<TextInput config={inputConfig} bindings={inputBindings}></TextInput>
</div>

Related

How to insert new Object element in array in MongoDB

I have this Document so structured
Process: Object
Code: 1
Descr: "Analyze"
DocumentToCreate: Array
0: Object
1: Object
2: Object
3: Object
4: Object
5: Object
Type: Object
Code:"R3"
Desc:"CV"
Condition: Object
0: Object
KeyDoc: "is not null"
1: Object
ReasonInsertion.Code: "03"
2: Object
Raw.Id: "is not null"
3: Object
Product.FlagDelete: false
6: Object
7: Object
8: Object
In the Condition object in position 5(filtered by value of Type.Code "R3") I have to add a new element, to find the correct document I should filter the one with Process.Code = 1
How can I generate a script to perform this update?
UPDATE
This the data in the document
{
"_id": {
"$oid": "728b678c11f25b32b1f59d8b"
},
"Process": {
"Code": 1,
"Desc": "Analyze"
},
"DocumentToCreate": [
{
"Type": {
"Code": "19",
"Desc": "Receipt"
}
},
{
"Type": {
"Code": "20",
"Desc": "Invoice"
}
},
{
"Type": {
"Code": "PH",
"Desc": "Photo"
}
},
{
"Type": {
"Code": "11",
"Desc": "Technical documentation"
},
"Condition": [
{
"Key": "is not null"
},
{
"Raw.Id": "04"
},
{
"ReasonInsertion.Code": "99"
}
]
},
{
"Type": {
"Code": "13",
"Desc": "Booking quote"
},
"Condition": [
{
"Key": "is not null"
},
{
"Raw.Id": "01"
}
]
},
{
"Type": {
"Code": "R3",
"Desc": "CV"
},
"Condition": [
{
"Key": "is not null"
},
{
"ReasonInsertion.Code": "00"
},
{
"Raw.Id": "is not null"
},
{
"Product.FlagDelete": false
}
]
},
{
"Type": {
"Code": "50",
"Desc": "Products for sale"
},
"Condition": [
{
"Key": "is not null"
},
{
"People.Code": "Seller"
}
]
},
{
"Type": {
"Code": "C3",
"Desc": "General conditions of sale used"
},
"Condition": [
{
"Key": "is not null"
},
{
"Discount.Code": "11"
}
]
},
{
"Type": {
"Code": "A4",
"Desc": "Discounted goods"
},
"Condition": [
{
"Object.Product.Book.Type": "K,A"
}
]
},
{
"Type": {
"Code": "AC",
"Desc": "Annex Clauses"
},
"Condition": [
{
"Raw.Id": "01"
},
{
"Object.Position": "02"
}
]
},
{
"Type": {
"Code": "W3",
"Desc": "Goods on loan"
},
"Condition": [
{
"Key": "is not null"
},
{
"People.Code": "Rent"
}
]
},
{
"Type": {
"Code": "R6",
"Desc": "Return of Goods"
},
"Condition": [
{
"Raw.Id": "04"
},
{
"ReasonInsertion.Code": "OUT"
},
{
"Object.Product.Book.IsSelled": true
}
]
}
]
}
I have to add as mentioned above a new element into object "Condition" in:
"Type": {
"Code": "R3",
"Desc": "CV"
},
"Condition": [
{
"Key": "is not null"
},
{
"ReasonInsertion.Code": "00"
},
{
"Raw.Id": "is not null"
},
{
"Product.FlagDelete": false
}
]

Snowflake - flatten multiple nested array values from json variant column

I have a JSON data from which I wanted to extract the values of key 'text' with delimited in single row. Any help to achieve the desired output is much appreciated.
Sample JSON data:
{
"expand": "schema,names",
"issues": [
{
"id": "123456",
"key": "XYZ-123",
"fields": {
"customfield_10000": "abcd",
"customfield_10001": 7,
"customfield_10002": null,
"description": {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 1"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 2"
},
{
"type": "text",
"text": "some text value 3",
"marks": [
{
"type": "link",
"attrs": {
"href": "some ref"
}
}
]
},
{
"type": "text",
"text": "some text value 4"
}
]
},
{
"type": "blockquote",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 5"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "inlineCard",
"attrs": {
"url": "some url"
}
},
{
"type": "text",
"text": "some text value 6"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 7"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 8"
},
{
"type": "text",
"text": "some text value 9",
"marks": [
{
"type": "link",
"attrs": {
"href": "some link"
}
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 10"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 11"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 12"
}
]
}
]
}
}
}
]
}
Desired output:
ISSUE_ID ISSUE_KEY CF_10000 CF_10001 CF_10002 DESCRIPTION
123456 XYZ-123 abcd 7 null some text value 1|some text value 2|some text value 3.....
I'm using the below query to get the arrays values. However I wanted the key 'text' values from arrays to be populated as above desired format.
select
ISSUE.value:id::number as ISSUE_ID,
ISSUE.value:key::varchar as ISSUE_KEY,
ISSUE.value:fields.customfield_10000::varchar as CF_10000,
ISSUE.value:fields.customfield_10001::number as CF_10001,
ISSUE.value:fields.customfield_10002::varchar as CF_10002,
ISSUE.value:fields.description.content::varchar as DESCRIPTION
from
VARIANT_TABLE,
lateral flatten( input => payload_json:issues, outer => true) as ISSUE
I have an UDF created to extract JSON array object key value into string of array, but that doesn't helped me to get the desired output from above shared JSON as it has nested arrays inside objects.
create or replace function UDF_ARRAY_OBJECT_TO_STRING_ARRAY(a array, b varchar)
returns array
language javascript
strict
comment = 'UDF to extract JSON array object key value into string of array. A refers to input array and B refers to extract which key from the array object'
as $$
return A.map(function(d) {return d[B]});
$$;
You have a lot more arrays in there than you are handling in your lateral flattens. With a few more flattens and a listagg() function, you should get there with this. Note, you might need to group by the index, rather than the field values, depending on what you are trying to get to, but this gives the result you were looking for in your example:
WITH x AS (
SELECT parse_json('{
"expand": "schema,names",
"issues": [
{
"id": "123456",
"key": "XYZ-123",
"fields": {
"customfield_10000": null,
"customfield_10001": null,
"customfield_10002": null,
"description": {
"version": 1,
"type": "doc",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 1"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 2"
},
{
"type": "text",
"text": "some text value 3",
"marks": [
{
"type": "link",
"attrs": {
"href": "some ref"
}
}
]
},
{
"type": "text",
"text": "some text value 4"
}
]
},
{
"type": "blockquote",
"content": [
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 5"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "inlineCard",
"attrs": {
"url": "some url"
}
},
{
"type": "text",
"text": "some text value 6"
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 7"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 8"
},
{
"type": "text",
"text": "some text value 9",
"marks": [
{
"type": "link",
"attrs": {
"href": "some link"
}
}
]
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 10"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 11"
}
]
},
{
"type": "paragraph",
"content": [
{
"type": "text",
"text": "some text value 12"
}
]
}
]
}
}
}
]
}') as payload_json)
select
issue.value:id::number as ISSUE_ID,
issue.value:key::varchar as ISSUE_KEY,
ISSUE.value:fields.customfield_10000::varchar as CF_10000,
ISSUE.value:fields.customfield_10001::number as CF_10001,
ISSUE.value:fields.customfield_10002::varchar as CF_10002,
listagg(content2.value:text::varchar,'|') as description
from
x,
lateral flatten( input => x.payload_json:issues, outer => true) as issue,
lateral flatten( input => issue.value:fields:description:content, outer => true) as content,
lateral flatten( input => content.value:content, outer => true) as content2
group by 1,2,3,4,5;

Elastic - JSON Array nested in Array

I have to index a json to Elastic which look like the below format. My problem is that the key "variable" is array that contains json objects (I thought about "nested" datatype of Elastic) but some of those objects it's possible to contain nested json arrays inside them. (see variable CUSTOMERS).
POST /example_data/data {
"process_name": "TEST_PROCESS",
"process_version ": 0,
"process_id": "1111",
"activity_id": "111",
"name": "update_data",
"username": "testUser",
"datetime": "2018-01-01 10:00:00",
"variables": [{
"name": "ΒΑΝΚ",
"data_type": "STRING",
"value": "EUROBANK"
},{
"name": "CITY",
"data_type": "STRING",
"value": "LONDON"
}, {
"name": "CUSTOMERS",
"data_type": "ENTITY",
"value": [{
"variables": [{
"name": "CUSTOMER_NAME",
"data_type": "STRING",
"value": "JOHN"
}, {
"name": " CUSTOMER_CITY",
"data_type": "STRING",
"value": "LONDON"
}
]
}
]
}, {
"name": "CUSTOMERS",
"data_type": "ENTITY",
"value": [{
"variables": [{
"name": "CUSTOMER_NAME",
"data_type": "STRING",
"value": "ΑΘΗΝΑ"
}, {
"name": " CUSTOMER_CITY ",
"data_type": "STRING",
"value": "LIVERPOOL"
}, {
"name": " CUSTOMER_NUMBER",
"data_type": "STRING",
"value": "1234567890"
}
]
}
]
}
] }
When I'm trying to index it I get the following error
{ "error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Can't merge a non object mapping [variables.value] with an object mapping [variables.value]"
}
],
"type": "illegal_argument_exception",
"reason": "Can't merge a non object mapping [variables.value] with an object mapping [variables.value]" }, "status": 400 }
Mapping
{ "example_data": {
"mappings": {
"data": {
"properties": {
"activity_id": {
"type": "text"
},
"name": {
"type": "text"
},
"process_name": {
"type": "text"
},
"process_version": {
"type": "integer"
}
"process_id": {
"type": "text"
},
"datetime": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
},
"username": {
"type": "text",
"analyzer": "greek"
},
"variables": {
"type": "nested",
"properties": {
"data_type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"value": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}}}
When I remove the variable CUSTOMERS that contains the array, then It works properly because there are only json objects.
Is there a way to handle that? Thanks in advance

Backbone/chaplinJS : How to check for key existence in object

{
"providerProperties": [
{
"key": "hotelnum",
"value": "123"
},
{
"key": "contract",
"value": "ABC"
},
{
"key": "Sequence",
"value": "1213"
},
{
"key": "lastFourDigit",
"value": "5454"
},
{
"key": "shop",
"value": "abc"
},
{
"key": "bookingEmailAddress",
"value": "abc#gmail.COM"
},
{
"key": "numOfRooms",
"value": "1"
}
}
Backbone/chaplinJS : How to check for key existence in object.
for example want to check of key "numOfRooms" is available or not.
You can use underscore method findWhere like _.findWhere(yourObject.providerProperties, {key: "numOfRooms"}); if the result is not undefined then it is present

Pull value from JSON key value pair using AngularJS

Is there a nice way in Angular's ngRepeat to pull a value out of a JSON Array depending on its key. I working with the following JSON structure and would like to output the associated value of "Name 2" for each object.
{
"Items": [
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
...
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
}
try like this
angular.forEach(data.Items,function(value,key){
angular.forEach(value,function(v){
if(v.name == "Name 2")
console.log(v);
}
})
Here is an example. It will print the Value for "Name 2":
<body ng-app="myApp">
<div ng-controller="myCtrl">
<div ng-repeat="item in records.Items">
<div ng-repeat="singleAttribute in item.Attributes">
<div ng-if="singleAttribute.Name === 'Name 2'">
{{singleAttribute.Value}}
</div>
</div>
</div>
</div>
<script>
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.records = {
"Items": [
{
"Attributes": [
{
"Name": "Name 1",
"Value": "123"
},
{
"Name": "Name 2",
"Value": "456"
},
{
"Name": "Name 3",
"Value": "789"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "987"
},
{
"Name": "Name 2",
"Value": "654"
},
{
"Name": "Name 3",
"Value": "321"
}
]
},
{
"Attributes": [
{
"Name": "Name 1",
"Value": "246"
},
{
"Name": "Name 2",
"Value": "369"
},
{
"Name": "Name 3",
"Value": "135"
}
]
}
]
};
});
</script>
</body>

Resources