Backbone/chaplinJS : How to check for key existence in object - backbone.js

{
"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

Related

How to change the value of a state object?

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>

MongoDB How to search in nested objects?

How i can search for the value "20044" in all fields "Barcode" and just in field "Barcode" in a nested object without specifying an absolute path e.g. "Item.Item.Item.Barcode" in MongoDB?
My current solutions:
Search in all text fields, not only in "Barcode" fields
find({$text: {$search: '20044'}})
Search in one specifying absolute path and not in all "Barcode" fields
find({'Item.Item.Item.Barcode': '20044'})
This is my databse object:
{
"_id": {
"$oid": "633d7cc238d7f8dafeace6f5"
},
"Number": "2",
"Item": [
{
"Type": "FrameElement",
"Item": [
{
"Type": "Frame",
"Barcode": "20011"
},
{
"Type": "Frame",
"Barcode": "20012"
},
{
"Type": "SashElement",
"Item": [
{
"Type": "Sash",
"Barcode": "20021"
},
{
"Type": "Sash",
"Barcode": "20022"
},
{
"Type": "GlassBarElement",
"Item": [
{
"Type": "GlassBar",
"Barcode": "20031"
},
{
"Type": "GlassBar",
"Barcode": "20032"
}
]
}
]
},
{
"Type": "Glass",
"Barcode": "20016"
},
{
"Type": "GlassBarElement",
"Item": [
{
"Type": "GlassBar",
"Barcode": "20043"
},
{
"Type": "GlassBar",
"Barcode": "20044"
}
]
}
]
}
]
}

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

How to filter dimensional array in React

I have a dimensional array like this
{
"items": [
{
"id": "file",
"value": "File",
"childs": [
{
"value": "New",
"status": 1
},
{
"value": "New",
"status": 2
},
{
"value": "New",
"status": 1
}
]
},
{
"id": "file",
"value": "File",
"childs": [
{
"value": "New",
"status": 1
},
{
"value": "New",
"status": 2
},
{
"value": "New",
"status": 1
}
]
}
]
}
How to filter items and own childs by childs.status
You have to use filter method in order to filter your array of items based on specific test, in your case when status 3 has been found, Here is a working example:
var json = {
"items": [
{
"id": "file",
"value": "File",
"childs": [
{
"value": "New",
"status": 1
},
{
"value": "New",
"status": 2
},
{
"value": "New",
"status": 1
}
]
},
{
"id": "file",
"value": "File",
"childs": [
{
"value": "New",
"status": 1
},
{
"value": "New",
"status": 3
},
{
"value": "New",
"status": 1
}
]
}
]
};
var result = json.items.filter(function(item) {
item.childs = item.childs.filter( function (child) {
return child.status === 3;
});
return item.childs.length > 0;
});
console.log({items: result});
I've setup a repl in which i've drawn out the method to filter your json. You can find it here; feel free to play around with it.
#younel's answer mutates the original array, I've also updated my repl with his approach to show how the original json is mutated.
Essence
look through each item in items
if a child in item matches the filter criteria, then keep the item and the filtered children

Sugar CRM 6.5 API

I am new to the Sugar CRM 6.5 API. I am having trouble getting my url to post data to the leads module in Sugar. Here is the posting url,
https://yoursite/service/v4_1/rest.php?method=set_entry&input_type=JSON&response_type=JSON&rest_data={"name_value_list":{"last_name":"Peter","status":"New","email1":"test#test.com","lead_source":"test"},"module_name":"Leads"}
Here is my response,
{
"id": "459bf4a2-0b3b-5857-feb5-56fe81e621eb",
"entry_list": {
"modified_by_name": {
"name": "modified_by_name",
"value": "admin"
},
"id": {
"name": "id",
"value": "459bf4a2-0b3b-5857-feb5-56fe81e621eb"
},
"name": {
"name": "name",
"value": " "
},
"date_entered": {
"name": "date_entered",
"value": "2016-04-01 14:10:12"
},
"date_modified": {
"name": "date_modified",
"value": "2016-04-01 14:10:12"
},
"modified_user_id": {
"name": "modified_user_id",
"value": "1"
},
"created_by": {
"name": "created_by",
"value": "1"
},
"deleted": {
"name": "deleted",
"value": 0
},
"full_name": {
"name": "full_name",
"value": " "
},
"do_not_call": {
"name": "do_not_call",
"value": false
},
"converted": {
"name": "converted",
"value": false
},
"lead_source": {
"name": "lead_source",
"value": "Web Site"
},
"status": {
"name": "status",
"value": "New"
},
"jjwg_maps_lat_c": {
"name": "jjwg_maps_lat_c",
"value": 0
},
"jjwg_maps_lng_c": {
"name": "jjwg_maps_lng_c",
"value": 0
}
}
}
When I view the leads, I get a blank record created. Any idea's, Thanks
The format of name_value_list is as follow:
{
"name_value_list":
"full_name": {"name": "full_name", "value": "John"}
}

Resources