node - Converting to JSON fails, but can print in console - javascript

I'm attempting to convert an array to JSON to be sent to a client. Here is what the data looks like in console:
[ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],
LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ],
SE: [ false, false, false ] ]
However when I run this (res being an express.js socket):
console.log(st.bot.serverStatus);
res.send(JSON.stringify(st.bot.serverStatus));
I get the output in console like expected, but I get [] from the web browser. What am I doing wrong?
PS: I am unable to change the format of the elements, they are generated by this method:
if(st.bot.serverStatus[tmp.country] !== undefined) {
st.bot.serverStatus[tmp.country][st.bot.serverStatus[tmp.country].length] = alive;
} else {
st.bot.serverStatus[tmp.country] = [ alive ];
}

It's not valid syntax at SE: and LE: since it is an array and not an object. Change the outermost [] to {} or change : to ,

Use either:
console.log(JSON.stringify({ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],
LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ],
SE: [ false, false, false ] } ));
Or:
console.log(JSON.stringify([{ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ]},
{LU: [ true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
true,
false,
false,
false ]},
{SE: [ false, false, false ] }] ));
Your JSON format is incorrect.
Use something like
jsonformatter.curiousconcept.com
or
jsoneditoronline.org
To validate your JSONs

Related

Invert boolean Values in JS object array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
This post was edited and submitted for review 3 days ago.
Improve this question
I'm dealing with object array
var data = [
{
"rdd": "Transducer Failure",
"performance": true,
"agc": true,
"snr": true,
"sos": true,
"flowvel": true
},
{
"rdd": "Detection Problem",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Ultrasonic Noise",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Process Condition Pressure",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Process Condition Temperature",
"performance": false,
"agc": true,
"snr": false,
"sos": true,
"flowvel": false
},
{
"rdd": "Fouling",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "Changes in flow profile",
"performance": false,
"agc": false,
"snr": false,
"sos": false,
"flowvel": false
},
{
"rdd": "High Velocity",
"performance": true,
"agc": true,
"snr": true,
"sos": false,
"flowvel": false
}
]
Now I want to invert value of object, whichever is false make true and vice verse. also, need to extract key's whose value is True after inversion .. I tried couple of things but no luck.
any idea ??
EDIT :
I Tried using
console.log(data);
for (var key in data) {
var obj = data[key];
Object.entries(obj).forEach(([key, value]) => {
if(value == false){
value = true;
}
})
}
console.log(data)
result remains same
You could check the type of value and get the negated value or value of not boolean.
const
data = [{ rdd: "Transducer Failure", performance: true, agc: true, snr: true, sos: true, flowvel: true }, { rdd: "Detection Problem", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Ultrasonic Noise", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Pressure", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Process Condition Temperature", performance: false, agc: true, snr: false, sos: true, flowvel: false }, { rdd: "Fouling", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "Changes in flow profile", performance: false, agc: false, snr: false, sos: false, flowvel: false }, { rdd: "High Velocity", performance: true, agc: true, snr: true, sos: false, flowvel: false }],
result = data.map(o => Object.fromEntries(Object
.entries(o)
.map(([k, v]) => [k, typeof v === 'boolean' ? !v : v])
));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to dynamically load UI elements from JSON object based on user inputs, where some JSON object has false/true which should not be loaded?

{ country : "United Kingdom" ,
format : {
recipient_name: true,
business_name: true,
post_office_box:false,
rural_route_identifier:false,
station_informaton:false,
suite_apartment_number: true,
street_address:{
street_name: true,
building_number: true,
building_name: true
},
street_address2 : false,
quarter_area: false,
neighborhood : false,
city_provinceCode : false,
postal_code: true,
city_town_locality: true,
locality_name: true,
municipality_name:false,
state: false,
province: false,
additional_information:true
},
coordi: {
Lat
Longi
}
}

using .join() based on condition in Javascript

I am trying to loop through the components array and join all the tire component descriptions with a comma (, ) if there are multiple.
let dataForTemplate = {};
incident = {
complaint_id: 55556473,
components: [{
component_id: 263,
name: 'SEAT BELTS',
description: '150000 SEAT BELTS',
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: false,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 1
},
{
component_id: 300,
name: 'TIRES',
description: '190000 TIRES',
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: true,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 17
},
{
component_id: 1025,
name: 'CHILD SEAT:VEHICLE TETHER ANCHOR',
description: '532000 CHILD SEAT:VEHICLE TETHER ANCHOR',
is_public: true,
is_vehicle: false,
is_equipment: false,
is_tire: false,
is_child_seat: true,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 4
}
]
};
Here is what I am trying:
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components.map((e) => {
console.log(e);
if (e.is_tire) {
return ${e.description};
}
}).join(' ,');
}
console.log(dataForTemplate);
Current output: {tire_components: " ,190000 TIRES ,"}
Expected output: {tire_components: "190000 TIRES"}
It should only join string with comma if there are multiple descriptions satisfying the condition.
Filter the array to get the is_tire entries, map tehm to get the description and join the result :
let dataForTemplate = {};
incident = {
complaint_id: 55556473,
components: [
{
component_id: 263,
name: "SEAT BELTS",
description: "150000 SEAT BELTS",
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: false,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 1
},
{
component_id: 300,
name: "TIRES",
description: "190000 TIRES",
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: true,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 17
},
{
component_id: 1025,
name: "CHILD SEAT:VEHICLE TETHER ANCHOR",
description: "532000 CHILD SEAT:VEHICLE TETHER ANCHOR",
is_public: true,
is_vehicle: false,
is_equipment: false,
is_tire: false,
is_child_seat: true,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 4
}
]
};
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components
.filter(e => e.is_tire)
.map(e => {
return `${e.description}`;
})
.join(" ,");
}
console.log(dataForTemplate);
let dataForTemplate = {};
incident = {
complaint_id: 55556473,
components: [
{
component_id: 263,
name: "SEAT BELTS",
description: "150000 SEAT BELTS",
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: false,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 1
},
{
component_id: 300,
name: "TIRES",
description: "190000 TIRES",
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: true,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 17
},
{
component_id: 300,
name: "TIRES",
description: "190000 TIRES example 2",
is_public: true,
is_vehicle: true,
is_equipment: false,
is_tire: true,
is_child_seat: false,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 17
},
{
component_id: 1025,
name: "CHILD SEAT:VEHICLE TETHER ANCHOR",
description: "532000 CHILD SEAT:VEHICLE TETHER ANCHOR",
is_public: true,
is_vehicle: false,
is_equipment: false,
is_tire: false,
is_child_seat: true,
is_active: true,
is_deleted: false,
risk_matrix_default_id: 4
}
]
};
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components
.filter(e => e.is_tire)
.map(e => {
return `${e.description}`;
})
.join(" ,");
}
console.log(dataForTemplate);
Array.prototype.join already does what you want, so it's just a matter of selecting the right data and calling .join (', '). Here's one technique:
const tireComponentDescriptions = (incident) =>
((incident || {}) .components || [])
.filter (i => i .is_tire)
.map (i => i.description)
.join (', ')
const incident = {complaint_id: 55556473, components: [{component_id: 263, name: "SEAT BELTS", description: "150000 SEAT BELTS", is_public: true, is_vehicle: true, is_equipment: false, is_tire: false, is_child_seat: false, is_active: true, is_deleted: false, risk_matrix_default_id: 1}, {component_id: 300, name: "TIRES", description: "190000 TIRES", is_public: true, is_vehicle: true, is_equipment: false, is_tire: true, is_child_seat: false, is_active: true, is_deleted: false, risk_matrix_default_id: 17}, {component_id: 1025, name: "CHILD SEAT: VEHICLE TETHER ANCHOR", description: "532000 CHILD SEAT: VEHICLE TETHER ANCHOR", is_public: true, is_vehicle: false, is_equipment: false, is_tire: false, is_child_seat: true, is_active: true, is_deleted: false, risk_matrix_default_id: 4}]};
console .log (tireComponentDescriptions (incident))
incident .components [0] .is_tire = true
console .log (tireComponentDescriptions (incident))
incident .components [2] .is_tire = true
console .log (tireComponentDescriptions (incident))
filter through the components, map to the description and join the resulting array afterwards
components.filter(component => component.is_tire).map(component => component.description).join(', ')
Array.map always returns an array of the same length, so will contain undefined values in your example. You could use Array.filter to first filter out any values you don't want before using join
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components.filter(e => e.is_tire).map(e => e.description).join(' ,');
}
You could also use Array.reduce to filter and map at the same time.
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components.reduce((output, e) => {
if(e.is_tire) {
output.push(e.description)
}
return output
}, []).join(' ,');
}
You could filter in advance and then map the wanted property.
let dataForTemplate = {},
incident = { complaint_id: 55556473, components: [{ component_id: 263, name: "SEAT BELTS", description: "150000 SEAT BELTS", is_public: true, is_vehicle: true, is_equipment: false, is_tire: false, is_child_seat: false, is_active: true, is_deleted: false, risk_matrix_default_id: 1 }, { component_id: 300, name: "TIRES", description: "190000 TIRES", is_public: true, is_vehicle: true, is_equipment: false, is_tire: true, is_child_seat: false, is_active: true, is_deleted: false, risk_matrix_default_id: 17 }, { component_id: 1025, name: "CHILD SEAT:VEHICLE TETHER ANCHOR", description: "532000 CHILD SEAT:VEHICLE TETHER ANCHOR", is_public: true, is_vehicle: false, is_equipment: false, is_tire: false, is_child_seat: true, is_active: true, is_deleted: false, risk_matrix_default_id: 4 }] };
if (incident.components && incident.components.length > 0) {
dataForTemplate.tire_components = incident.components
.filter(({ is_tire }) => is_tire)
.map(({ description }) => description)
.join(' ,');
}
console.log(dataForTemplate);

How can I change the tooltips in my ng grid

I would like to change the tooltip name in my button 'Export to Excel'. I have angular directive (ng-grid). The directive config is :
var defaultConfig = {
dataSource: {
dataPath: "gridCtrl.getFilteredData(true)",
deep: false,
},
bindingOptions: {
dataSource: {
dataPath: "gridCtrl.getFilteredData(true)",
deep: false,
},
columns: "gridCtrl.getColumns()"
},
remoteOperations: false,
focusStateEnabled: true,
hoverStateEnabled: true,
allowColumnReordering: true,
editing: { editEnabled: false },
paging: { enabled: true, pageSize: 25 },
pager: { showPageSizeSelector: true, allowedPageSizes: [10, 25, 50, 100] },
filterRow: { visible: true, applyFilter: "auto" },
sorting: { mode: "multiple" },
searchPanel: { width: 240, placeholder: $i18n.translate("lbl_search") },
headerFilter: { visible: true },
groupPanel: { visible: true, allowColumnDragging: true, emptyPanelText: $i18n.translate("txt_arrastrar_columna") },
allowColumnResizing: true,
columnAutoWidth: false,
columnChooser: { enabled: false, emptyPanelText: $i18n.translate("lbl_hide_columns_here"), title: $i18n.translate("lbl_column_chooser") },
rowAlternationEnabled: true,
showExpandAll: false,
summary: {
totalItems: [{
column:0,
summaryType:"count",
customizeText: function (resourceInfo){ return $i18n.translate("lbl_total_items")+": "+resourceInfo.value; }
}]
},
};
The tooltip which I want to change is 'Export to Excel'.
Thanks in advance!!!
I think you are talking about dxDataGrid so use export/texts for this as you can see in the sample code below
$("#datagrid").dxDataGrid({
"export": {
"enabled": true,
"fileName": "DataGridCusomText",
"texts": {
"exportAll": "Export everything",
"exportSelectedRows": "Export only selected",
"exportTo": "Export to excel or word"
}
}
});
As you can see in the image below

Cannot read property aDataSort . DataTable bootstrap

I just start to learn about bootstrap and creating a simple project that can show a data into DataTable but I am having this error Cannot read property aDataSort of undefined
Feel Free to edit my code if i got it wrong
These are my code
JS
var globalPersonId = 1;
var dataTablesOrderedList = "";
$(document).ready(function () {
var dataTablesFirstBS = $('#dataTables-FIrstSample').DataTable({
responsive: true,
processing: true,
info: true,
search: true,
sort: false,
stateSave: true,
order: [[1, "asc"], [2, "asc"]],
lengthMenu: [[50, 100, 200, -1], [50, 100, 200, "All"]],
ajax: { "url": "/BS/GetFirstDataTable" },
columns:
[
{ data: "BSId", title: "", visible: false, searchable: false, sortable: false },
{ data: "Name", title: "Name", sClass: "alignCenter", sortable: false }
]
});
MyFirstBS();
function MyFirstBS() {
dataTablesOrderedList = $('#tblMyFirstBS').DataTable({
responsive: true,
processing: true,
info: true,
retrieve: true,
destroy: true,
search: true,
sort: false,
stateSave: true,
lengthMenu: [[5, 10, 20, -1], [5, 10, 20, "All"]],
ajax: {
"url": "/BS/GetFirstDataTable",
"data": function (d) {
d.BSId = globalPersonId;
}
},
columns:
[
{ data: "BSId", title: "", visible: false, searchable: false, sortable: false },
{ data: "Name", title: "Name", searchable: false, sortable: false }
]
});
}
});
Controller
public JsonResult GetFirstDataTable()
{
var Data = new List<object>();
Data = db.FirstBS.Where(x => x.BSId == x.BSId)
.Select(t =>
new
{
BSId = t.BSId,
Name = t.Name
}).ToList<object>();
return gf.DataTableAjaxHandlerClientSide(Data);
}
i solved that problem in my case by removing the order line
order: [[ 7, "desc" ]],
I solve my problem by removing the second order here is my new order now
order: [[1, "asc"]],

Categories

Resources