Replacing special character with jquery - javascript

i need to replace the special character "[" and "]" in this syntaxe:
var Data = "[{\tst??}]";
It's a json format and i need to show all the information individually so i need a tip in jquery to replace or delete the "[" and "]" they causing me troubles when i whant to show the information.
Thank you.

var Data = "...."; // here is your data
var obj = JSON.parse(Data);
after this, obj will contain:
[ { AccountNumber: '664009500',
AccountNumberLong: '230100950070',
Autorizations: [],
AvailableBalance: 0,
Balance: 2243.93,
BeneficiaryList: [ [Object], [Object], [Object], [Object], [Object] ],
CanCreditAccount: true,
CanDebitAccount: true,
CodeOffre: 'CPTCHQ11',
Currency: 'MAD',
EligibleServiceList: [ '300014', '300018', '300013', '300016' ],
Entitled: 'MONSIEUR HOUSSAM MOUBTAHIL',
IsDemat: 'true',
LstClientidTuteur: null,
OpeningDate: '/Date(1408662000000+0000)/',
OperationDate: '/Date(-62135596800000+0000)/',
OperationLastDate: '/Date(1465776000000+0000)/',
OperationOAA: null,
OperationOSD: null,
Operations: null,
RelationType: null,
SitexAccountList: [],
SitexTierList: null,
TotalBalance: 0,
TotalCredit: 32151.26,
TotalDebit: 29255.77,
legalSituation: 'MJ' } ]
nothing to change :)
you have a list of objects, if your list will have only one element - take obj[0], if more - just loop through them (using, for loop or jQuery's .each)..

Use slice to remove the first and the last char (i.e, [ and ])
CleanData=Data.slice(1, -1);
Use this to remove all [ followed by { in order to get only { and the same is for }] that are replaced with }. In this way, something like [] is not replaced
var res = Data.replace("[{","{");
var CleanData=res.replace("}]","]");

try to use : Data.toString().trim().replace("[", anotherCharacter)

Related

Using [Sequelize.Op.in] for json object arrays

I have a meta tag table which contains a tagId and a value.
I have an array of tagId,value objects
Eg : [{'tagId':2, 'value':33}, {'tagId':2, 'value':34}, {'tagId':1,
'value':34}, etc.. ]
My metaTag table consists of a virtual column which will return the {tagId,value} object for each entry in table. My question is how can I select rows with each {tagId, value} pair in the array.
In other words, I want to be able to do something like
[Sequelize.Op.in]:[{'tagId':2, 'value':33}, {'tagId':2, 'value':34}]
This doesn't work, however.
I might not have explained this well, English isn't my first language. Please ask if you need any clarification on the issue.
You can attain this by using Op.or. If I am not wrong you are trying
('tagId' = 2 and 'value' = 33) or ('tagId' = 2, 'value' = 34):
where: { [Sequelize.Op.or]: [{'tagId':2, 'value':33}, {'tagId':2, 'value':34}] }
You can add n number of values to the or array. As per your requirement.
if you want to do a in like this:
tagId in(2, 2) and value in (33, 34) then:
where: {'tagId':[2], 'value':[33, 34]}
You don't need the explicit Op.in for the array.
You can use there:
const myDeviceIds = [1, 2, 3, 4, 7, 9, 29];
const macroDevices = await MacroDevice.findAll({
where: {
deviceId: {
[Op.in]: myDeviceIds,
},
life: {
[Op.not]: null,
},
status: {
[Op.is]: null,
}
},
order: [
['id', 'DESC']
],
limit,
offset,
include: [
Macro,
Targets,
]
});

Flatten JSON data

I am trying to use Tabulator to create a list of tickets, The data is imported via AJAX url from the ticket system as a JSON as below.
{
"results": [
{
"cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"fwd_emails": [],
"reply_cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"ticket_cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"fr_escalated": false,
"spam": false,
"email_config_id": null,
"group_id": 35000204315,
"priority": 1,
"requester_id": 35020281588,
"responder_id": 35004154466,
"source": 2,
"company_id": null,
"status": 2,
"subject": "Support Needed...",
"association_type": null,
"to_emails": null,
"product_id": null,
"id": 188261,
"type": null,
"due_by": "2019-09-17T15:12:07Z",
"fr_due_by": "2019-07-01T15:12:07Z",
"is_escalated": false,
"description": "<div>Details about the issue...</div>",
"description_text": "Details about the issue...",
"custom_fields": {
"cf_category": null,
"cf_firstname": null,
"cf_surname": null,
"cf_user_trainging": null,
"cf_email_address": null,
"cf_office_365": null,
"cf_start_date": null,
"cf_permission_level": null,
"cf_hardware_type": null,
"cf_additional_information_specsoftware_etc": null,
"cf_vpn_access_required": false,
"cf_securitydistribution_group_membership": null,
"cf_mapped_network_driveslogin_script": null,
"cf_printers": null,
"cf_phone_extension": null,
"cf_ddi": null,
"cf_phone_group_membership": null,
"cf_user_who_requires_the_equipment": null,
"cf_requirment_date": null,
"cf_correctclosureused": null,
"cf_location": "A1"
},
"created_at": "2019-06-24T15:11:47Z",
"updated_at": "2019-06-24T15:59:00Z",
"associated_tickets_count": null,
"tags": []
}
],
"total": 1
}
The problem is the "custom_fields" is a JSON Object inside the main JSON object, is there a way to flatten this data out and display this as all one row in Tabulator? Any help appreciated?
My current result in Tabulator is it returns [object Object] for the custom_fields column. I would like to be able to see each of custom_fields in the row.
Handling Nested Data
There is no need to flatten the object, Tabulator can handle nested data for columns, if you use dot notation in the field name:
var table = new Tabulator("#example-table", {
columns:[
{title:"Category", field:"custom_fields.cf_category"}, //link column to nested field
],
});
Full details about nested data handling can be found in the Columns Documentation
Column Grouping
If you wanted to you could also use column grouping to show that the fields are a subset of a another property, for example we could define the top level columns as usual and then add column group to hold the custom columns
var table = new Tabulator("#example-table", {
columns:[
{title:"Subject", field:"subject"}, //standard column
{title:"Priorty", field:"priority"}, //standard column
{title:"Custom", columns:[ //column group to hold columns in custom_fields property
{title:"Category", field:"custom_fields.cf_category"},
{title:"First Name", field:"custom_fields.cf_firstname"},
]},
],
});
Full details can be found in the Column Grouping Documentation
If you're using es6+, you could easily achieve this by using rest in object destructuring and object spread.
const input = {
"results": [
{
"custom_fields": {...},
...
}
],
"total": 1
}
const expanded = input.results.map(result => {
const { custom_fields, ...rest } = result;
return { ...rest, ...custom_fields };
})
Here is a slightly different solution relying on function generators to traverse the original object, giving the possibility to eventually detect some duplicated keys.
This example can, of course, be altered by adding further checks (like whether you want to traverse all objects inside the main object and so on).
The current example takes care of:
Traversing the original object by excluding primitives and arrays.
Providing a flattenObject method that accepts an object as an argument and a callback as an eventual second argument that will be raised when a duplicated key is met. In that case, the default behavior is to take the "next" nested value as the new one. If false is returned in the callback, the current value is kept. The callback will provide the key and the value of the new value.
So, in a nutshell, the "real" code to acquire the desired result is this one:
// Case usage:
// Map the existing values.
input.results = input.results.map(i => flattenObject(i, (duplicatedKeyValuePair) => {
return false; // <-- keep the existing value if a duplicate is matched.
}));
console.log(input.results)
Of course, it's slightly more complicated then just flattening the desired property, but I wanted to give a more elastic flavour to it.
const input = {
"results": [
{
"cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"fwd_emails": [],
"reply_cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"ticket_cc_emails": [
"ram#freshdesk.com",
"diana#freshdesk.com"
],
"fr_escalated": false,
"spam": false,
"email_config_id": null,
"group_id": 35000204315,
"priority": 1,
"requester_id": 35020281588,
"responder_id": 35004154466,
"source": 2,
"company_id": null,
"status": 2,
"subject": "Support Needed...",
"association_type": null,
"to_emails": null,
"product_id": null,
"id": 188261,
"type": null,
"due_by": "2019-09-17T15:12:07Z",
"fr_due_by": "2019-07-01T15:12:07Z",
"is_escalated": false,
"description": "<div>Details about the issue...</div>",
"description_text": "Details about the issue...",
"test_duplicated_key": "hello! I should keep this!",
"custom_fields": {
"cf_category": null,
"cf_firstname": null,
"cf_surname": null,
"cf_user_trainging": null,
"cf_email_address": null,
"cf_office_365": null,
"cf_start_date": null,
"cf_permission_level": null,
"cf_hardware_type": null,
"cf_additional_information_specsoftware_etc": null,
"cf_vpn_access_required": false,
"cf_securitydistribution_group_membership": null,
"cf_mapped_network_driveslogin_script": null,
"cf_printers": null,
"cf_phone_extension": null,
"cf_ddi": null,
"cf_phone_group_membership": null,
"cf_user_who_requires_the_equipment": null,
"cf_requirment_date": null,
"cf_correctclosureused": null,
"cf_location": "A1",
"test_duplicated_key": "You should not see that."
},
"created_at": "2019-06-24T15:11:47Z",
"updated_at": "2019-06-24T15:59:00Z",
"associated_tickets_count": null,
"tags": []
}
],
"total": 1
}
/**
Traverse every property of the desired object, by returning the currently key-value pair looped. If the value is an object, it keeps traversing.
*/
function* traverseObject(obj) {
for ([key, value] of Object.entries(obj)) {
if (value && typeof(value) === 'object' && !Array.isArray(value)) {
yield* traverseObject(obj[key]);
}
else yield {key: key, value: value};
}
}
/**
Flattens the object by traversing every object inside it.
*/
function flattenObject(obj, onDuplicatedKey) {
let res = {};
for (keyValuePair of traverseObject(obj)) {
let add = true;
if (res.hasOwnProperty(keyValuePair.key)) {
add = onDuplicatedKey ? onDuplicatedKey.call(onDuplicatedKey, keyValuePair) : true; // default behavior: override with nested propeties.
}
if (add) res[keyValuePair.key] = keyValuePair.value;
}
return res;
}
/*
Sample usage.
const flattened = flattenObject(input.results[0], (record) => {
console.log('detected key value pair duplicate. Key:', record.key, ' value: ', record.value);
// true will override the value, false will not override the value.
return false;
});
*/
//console.log(flattened);
// Case usage:
// Map the existing values.
input.results = input.results.map(i => flattenObject(i, (duplicatedKeyValuePair) => {
return false; // <-- keep the existing value if a duplicate is matched.
}));
console.log(input.results);
Please note that the above case is just an example, I didn't spend much time testing every single property type, hence it can (of course) be reviewed and code quality and performances can be improved. It was just an example to show a different approach relying on different operators and logics.
As a (final) side note, I Think you can handle that with tabulator in some way, though I'm not sure you can render multiple columns out of a single property, which leads me to believe that altering the original object is probably the desired solution.

coverting the string to json object

I tried to display my data in json format. Firstly i tried JSON.stringify
then it shows in string format so, then again I tried JSON.parse where I get like array of object finally I tried keeping [0] to the parse object it throws 'undefined'...
can you please tell me where I am doing wrong..
I provide the output of the following console.log..
try{
var timekeep = await Orders.findAndCountAll(
{
where : {
cid : orders_info.cid,
},
order: [
['id', 'DESC']
],
limit: 1,
}
);
console.log("RESULT => ", timekeep);
var datjsn = JSON.stringify(timekeep);
var jsonsr = JSON.parse(datjsn);
var data23 = jsonsr[0];
console.log('alpha1'+ datjsn);
console.log('delta2'+ jsonsr);
console.log('beta'+ data23);
output of the console logs
RESULT => { count: 1,
rows:
[ orders {
dataValues: [Object],
_previousDataValues: [Object],
_changed: {},
_modelOptions: [Object],
_options: [Object],
__eagerlyLoadedAssociations: [],
isNewRecord: false } ] }
alpha1 {"count":1,"rows":[ {"id":4006,"mid":1,"cid":41,"wid":7138,"oid":null,"status":null,"options":null,"starttime":"2018-08-15T06:08:55.000Z","duration":null,"ordertotal":50,"counter":null,"closetime":null}]}
delta2 [object Object]
beta undefined
var data23 = jsonsr[0];
In here you have took wrong place. Actually the data is in jsonsr.rows[0].
For this you can use,
jsonsr.rows[0];
or if the key value is not known use the Object.keys to get the all the objects in array and proceeding with loop with the condition of check the object value in array and length greater than 0 can get the all the datas.
The object is sequelize object not just object.
Try to use .get()
var timekeep = await Orders.findAndCountAll(
{
where : {
cid : orders_info.cid,
},
order: [
['id', 'DESC']
],
limit: 1,
}
);
console.log(timekeep.rows[0].get())

How to iterate an array of objects in Javascript

I have an object (obj) that contains the content of an excel sheet (using node-xlsx library).
console.log(obj) gives me the following data structure:
[ { name: 'Autostar Report',
data:
[ [Object],
[],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[Object],
[],
[object]
]}]
Each of the objects in data contain values like this:
[
8,
"Enugu - Abuja",
"Sienna Executive ( 1st )",
5,
"9,000",
"45,000",
"5,500",
"39,500"
],
[
14,
"Enugu - Lagos",
"Sienna Executive ( 1st )",
5,
"9,000",
"45,000",
"8,600",
"36,400"
]
Each of the object contained in data contains 8 values. I want to insert the values into a database table. Each of the value goes to a field in the table.
Please how do I access the values in each data object?
Note that you keep saying "object", when really the obj.data value is an array of arrays. With arrays, you access a value by numerical index.
So, given your data structure, and the fact that you have only selected javascript, you would access the data like so:
// ensure the object has a "data" attribute
if (obj.hasOwnProperty('data')) {
// get the data into a variable for convenience
var data = obj.data;
// loop over each row
for (i = 0; i < data.length; i++) {
// put the row into a variable for convenience
var row = data[i];
// since it's an array, access each value by index
console.log(row[0]);
console.log(row[1]);
console.log(row[2]);
// ... etc ...
}
}
Here's a working fiddle
When it comes to processing things like this, a library such as underscore or lodash sure come in handy.

JSON data retrieval

I have an Array nested withing an Object that's inside an Object which is also in an Object.
EDIT: My original data structure was malformed, so here is a screen cap of console.log(data):
This is being returned from an AJAX request and I'm having issue with getting to the array. The trick is that it will not always return 3 arrays per parent object, the amount of arrays is dependent on "length", or rather, "length" is a reflection of how many arrays to expect in each object that contains arrays. "OID" is the name of each array for both the "th" and "uh" objects.
There also happen to be several main objects, which are iterated with for(var i in data)
I've tried something like:
for(var i in data) {
for(var x in data[i].clicks) {
//And now I'm lost here
//I've tried getting the name of the Array from "OID"
//and going from there, that failed miserably.
}
}
How can I get at the contents of each of those arrays, it would be best if this can be done without knowing the name, quantity or length of said arrays.
Thanks in advance.
Your question and data structure is not very clear and I'm not familiar with the syntax you've used to declare your arrays, it seems incorrect to me.
If you intended your data structure be
data={
"Clicks": {
"length": 3, //Can be between 0-3
"OID": {
1: "1",
2: "2",
3: "3"
},
"th": {
1: [
null,
null,
null,
null
],
2: [
null,
null,
null,
null
],
3: [
null,
null,
null,
null
]
},
"uh": {
1: [
null,
null,
null,
null
],
2: [
null,
null,
null,
null
],
3: [
null,
null,
null,
null
]
}
}
};
and what you want to do is to iterate over all the elements in th and uh where the key comes from the entries in OID, you should be doing something like
for(var i = 1; i <= data.Clicks.length; i++){
data.Clicks.th[data.Clicks.OID[i]];
data.Clicks.uh[data.Clicks.OID[i]];
}
However, if your keys are not going to be anything but numbers, it seems like you might be better served by returning an array of arrays for each of th and uh:
data={
"Clicks": {
"th": [
[
null,
null,
null,
null
],
[
null,
null,
null,
null
],
[
null,
null,
null,
null
]
],
"uh": [
[
null,
null,
null,
null
],
[
null,
null,
null,
null
],
[
null,
null,
null,
null
]
]
}
};
and access it as
//assuming th and uh are of the same length always
for(var i = 1; i <= data.Clicks.th.length; i++){
data.Clicks.th[i];
data.Clicks.uh[i];
}

Categories