get values from different arrays javascript - javascript

I am using yahoo's placemaker to extract location names from text. From this I get a callback function which gives me 2 different types of arrays.
this is the code am using but I CANT ANY of the values I want.
Placemaker.getPlaces(text,function(o){
if (typeof o.match!=='undefined' && o.match.length==1){
latitude=o.match.place.centroid.latitude, longitude=o.match.place.centroid.longitude;
console.log(latitude,longitude);}
if(typeof o.match !=='undefined'&& o.match.length==2){
latitude=o[match].place.centroid.latitude,
longitude=o[match].place.centroid.longitude;
console.log(latitude,longitude);
}
The first array looks like this
({
match: {
place: {
woeId: "29007292",
type: "Town",
name: "Jubila, West Bengal, IN",
centroid: {
latitude: "23.1626",
longitude: "87.7889"
}
},
reference: [{
woeIds: "29007292",
placeReferenceId: "2",
placeIds: "1",
start: "14",
end: "20",
isPlaintextMarker: "1",
text: "jubila",
type: "plaintext",
xpath: null
}, {
woeIds: "29007292",
placeReferenceId: "3",
placeIds: "1",
start: "82",
end: "88",
isPlaintextMarker: "1",
text: "jubila",
type: "plaintext",
xpath: null
}]
}
})
and the other one like this
({
match: [{
place: {
woeId: "23424950",
type: "Country",
name: "Spain",
centroid: {
latitude: "39.8949",
longitude: "-2.98831"
}
},
reference: {
woeIds: "23424950",
placeReferenceId: "1",
placeIds: "1",
start: "64",
end: "70",
isPlaintextMarker: "1",
text: "Espa\xF1a",
type: "plaintext",
xpath: null
}
}, {
place: {
woeId: "24865675",
type: "Continent",
name: "Europe",
centroid: {
latitude: "52.9762",
longitude: "7.85784"
}
},
reference: {
woeIds: "24865675",
placeReferenceId: "2",
placeIds: "2",
start: "93",
end: "99",
isPlaintextMarker: "1",
text: "Europa",
type: "plaintext",
xpath: null
}
}]
})

change
if (typeof o.match!=='undefined' && o.match.length==1){
to
if (typeof o.match!=='undefined'){
For second:
for(var i in o.match) {
console.log(o.match[i].place.centroid.latitude); //wil show all lat's in loop
}
or if you want first lattitude only:
console.log(o.match[0].place.centroid.latitude); //will show only first lat

Related

format json is not true

I have a site that send me a json but this json has wrong format and I don't know what I should to do to convert this to the right format .
I want to use this to my python code.
this is my json that sent by site.
{
product_id: 11,
user_id: 44,
data: {
"Wood shear wall design": {
"Height of wall": { value: "4", unit: "ft" },
"Length of wall": { value: "4", unit: "ft" },
"Shear force": { value: "1000", unit: "lb" },
"sheathing grade": {
value: '{"id":1136,"item":"Sheathing","section_product_item":"30_77"}',
unit: "N/A",
},
"sheathing material": {
value: '{"id":1137,"item":"OBS","section_product_item":"30_77"}',
unit: "N/A",
},
"sheathing type": {
value: '{"id":1138,"item":"3\\/8","section_product_item":"30_77"}',
unit: "N/A",
},
"stud size": {
value: '{"id":1139,"item":"2_3","section_product_item":"30_77"}',
unit: "N/A",
},
"number of nails": { value: "1", unit: "N/A" },
"stud type": {
value: '{"id":1143,"item":"6d","section_product_item":"30_77"}',
unit: "N/A",
},
"nail spacing": {
value: '{"id":1144,"item":"2","section_product_item":"30_77"}',
unit: "ft",
},
"modulus of elasticity": { value: "1", unit: "psf" },
"HD capacity": { value: "1", unit: "lb" },
"HD deflection": { value: "1", unit: "in" },
"number of opening": {
value: '{"id":1149,"item":"0","section_product_item":"30_77"}',
unit: "N/A",
},
},
},
type: 1,
number_of_projects: 1,
}
I write this in every online formatter and insomnia but in all of them raise syntax error for json.
there is any library or function that can solve this problem?
The text you provided uses single-quotes ('), while json requires double-quotes ("). So to make it valid json you can probably take it as a string and just replace all ' with ".
s = s.replace("'", '"')
Then do json.loads(s) to get a dictionary from the json. Just make sure that there aren't any single-quotes in the json that are actually meant to be single-quotes and don't just annotate keys.
Edit: on a second look the whole "json" looks pretty weird, are you sure this is supposed to be valid json?

Finding an element in an array and adding it to another javascript

Below is a snippet of the JSON file that I'm working with and in the array "target_indices" you'll see the indices of the data that I want to extract. I seem to be stuck in matching up if the field_index has my target index in it, I want to add it to the const schools. Any help here would be appreciated!
"fields": [{"type":"int","id":"_id"},{"type":"text","id":"Centre Code"},{"type":"text","id":"Centre Name"},{"info":{"notes":"","type_override":"","label":""},"type":"text","id":"Centre Type"},{"type":"text","id":"Centre Status"},{"type":"text","id":"Host Centre Code"},{"type":"text","id":"Host Centre Name"},{"type":"text","id":"Official Low Year Level"},{"type":"text","id":"Official High Year Level"},{"type":"text","id":"Officer In Charge Title"},{"type":"numeric","id":"School Band"},{"type":"timestamp","id":"Show Holiday Date"},{"type":"text","id":"Internet Site"},{"type":"text","id":"Phone Number"},{"type":"text","id":"Restrict Contact Outside Teaching Hours"},{"type":"text","id":"Fax Number"},{"type":"text","id":"Actual Address Line 1"},{"type":"text","id":"Actual Address Line 2"},{"type":"text","id":"Actual Address Line 3"},{"type":"numeric","id":"Actual Address Post Code"},{"type":"text","id":"Postal Address Line 1"},{"type":"text","id":"Postal Address Line 2"},{"type":"text","id":"Postal Address Line 3"},{"type":"numeric","id":"Postal Address Post Code"},{"type":"text","id":"Education Geographic Region"},{"type":"text","id":"Federal Electorate"},{"type":"text","id":"State Electorate"},{"type":"text","id":"Local Government Area"},{"type":"text","id":"Statistical Area Level2"},{"type":"numeric","id":"Statistical Area Level2 Code"},{"type":"text","id":"Remoteness Area"},{"type":"text","id":"Enrolment Effective Date"},{"type":"numeric","id":"All Student Count"},{"type":"text","id":"Campus All Student Count"},{"type":"numeric","id":"ABN"},{"info":{"notes":"","type_override":"","label":""},"type":"text","id":"Sector"},{"type":"text","id":"Non-State Sector"},{"type":"numeric","id":"Longitude"},{"type":"numeric","id":"Latitude"}],
"records":
[2,"0591","Abercorn State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-08-10T00:00:00","www.abercornss.eq.edu.au","(07) 4167 5190","Y","(07) 4167 5135","","957 Wuruma Dam Road","Abercorn",4627,"957 Wuruma Dam Road","Abercorn","",4627,"Central Queensland","Flynn","Callide","North Burnett (R)","Monto - Eidsvold",319021508,"Outer Regional Australia","2019 July",18,"",22101246877,"State","",151.127031,-25.135955],
[3,"1275","Abergowrie State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-07-03T00:00:00","www.abergowriess.eq.edu.au","(07) 4777 4672","N","(07) 4777 4686","","5 Venables Road","Abergowrie",4850,"5 Venables Road","Abergowrie","",4850,"North Queensland","Kennedy","Hinchinbrook","Hinchinbrook (S)","Ingham Region",318011465,"Remote Australia","2019 July",4,"",87244066343,"State","",145.88351,-18.474697],
fetch('./qldopendata-json/school_locations.json')
.then(response => {
return response.json();
})
.then(schoolData => {
const schools = [];
const target_indices = [2,7,8,12,13,17,18,19,37,38];
schoolData.records.forEach((school_value) => {
const tidied_school = {};
schoolData.fields.forEach((field_name, field_index) => {
tidied_school[field_index] = school_value[field_index]
if (field_index in target_indices){ //this is where i need help
schools.push(tidied_school);
}
})
console.log(schools);
})
})
You can use reduce().
Example below:
const schoolData = { fields: [ { type: "int", id: "_id" }, { type: "text", id: "Centre Code" }, { type: "text", id: "Centre Name" }, { info: { notes: "", type_override: "", label: "" }, type: "text", id: "Centre Type", }, { type: "text", id: "Centre Status" }, { type: "text", id: "Host Centre Code" }, { type: "text", id: "Host Centre Name" }, { type: "text", id: "Official Low Year Level" }, { type: "text", id: "Official High Year Level" }, { type: "text", id: "Officer In Charge Title" }, { type: "numeric", id: "School Band" }, { type: "timestamp", id: "Show Holiday Date" }, { type: "text", id: "Internet Site" }, { type: "text", id: "Phone Number" }, { type: "text", id: "Restrict Contact Outside Teaching Hours" }, { type: "text", id: "Fax Number" }, { type: "text", id: "Actual Address Line 1" }, { type: "text", id: "Actual Address Line 2" }, { type: "text", id: "Actual Address Line 3" }, { type: "numeric", id: "Actual Address Post Code" }, { type: "text", id: "Postal Address Line 1" }, { type: "text", id: "Postal Address Line 2" }, { type: "text", id: "Postal Address Line 3" }, { type: "numeric", id: "Postal Address Post Code" }, { type: "text", id: "Education Geographic Region" }, { type: "text", id: "Federal Electorate" }, { type: "text", id: "State Electorate" }, { type: "text", id: "Local Government Area" }, { type: "text", id: "Statistical Area Level2" }, { type: "numeric", id: "Statistical Area Level2 Code" }, { type: "text", id: "Remoteness Area" }, { type: "text", id: "Enrolment Effective Date" }, { type: "numeric", id: "All Student Count" }, { type: "text", id: "Campus All Student Count" }, { type: "numeric", id: "ABN" }, { info: { notes: "", type_override: "", label: "" }, type: "text", id: "Sector", }, { type: "text", id: "Non-State Sector" }, { type: "numeric", id: "Longitude" }, { type: "numeric", id: "Latitude" }, ], records: [ [ 2, "0591", "Abercorn State School", "State School", "Open", "", "", "Prep Year", "Year 6", "Principal", 5, "2020-08-10T00:00:00", "www.abercornss.eq.edu.au", "(07) 4167 5190", "Y", "(07) 4167 5135", "", "957 Wuruma Dam Road", "Abercorn", 4627, "957 Wuruma Dam Road", "Abercorn", "", 4627, "Central Queensland", "Flynn", "Callide", "North Burnett (R)", "Monto - Eidsvold", 319021508, "Outer Regional Australia", "2019 July", 18, "", 22101246877, "State", "", 151.127031, -25.135955, ], [ 3, "1275", "Abergowrie State School", "State School", "Open", "", "", "Prep Year", "Year 6", "Principal", 5, "2020-07-03T00:00:00", "www.abergowriess.eq.edu.au", "(07) 4777 4672", "N", "(07) 4777 4686", "", "5 Venables Road", "Abergowrie", 4850, "5 Venables Road", "Abergowrie", "", 4850, "North Queensland", "Kennedy", "Hinchinbrook", "Hinchinbrook (S)", "Ingham Region", 318011465, "Remote Australia", "2019 July", 4, "", 87244066343, "State", "", 145.88351, -18.474697, ], ], };
const targetIndices = [2, 7, 8, 12, 13, 17, 18, 19, 37, 38];
const schools = schoolData.records.reduce((a, b) => {
const requiredObject = targetIndices.reduce((acc, t) => {
acc.push(b[t]);
return acc;
}, []);
a.push(requiredObject);
return a;
}, []);
console.log(schools);
Hope this helps you out. You will need to create the target_indices_names manually with this, there is probably another way to do it dynamically, but since it is only 10 properties.
var schoolData = '[[2,"0591","Abercorn State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-08-10T00:00:00","www.abercornss.eq.edu.au","(07) 4167 5190","Y","(07) 4167 5135","","957 Wuruma Dam Road","Abercorn",4627,"957 Wuruma Dam Road","Abercorn","",4627,"Central Queensland","Flynn","Callide","North Burnett (R)","Monto - Eidsvold",319021508,"Outer Regional Australia","2019 July",18,"",22101246877,"State","",151.127031,-25.135955],'+
'[3,"1275","Abergowrie State School","State School","Open","","","Prep Year","Year 6","Principal",5,"2020-07-03T00:00:00","www.abergowriess.eq.edu.au","(07) 4777 4672","N","(07) 4777 4686","","5 Venables Road","Abergowrie",4850,"5 Venables Road","Abergowrie","",4850,"North Queensland","Kennedy","Hinchinbrook","Hinchinbrook (S)","Ingham Region",318011465,"Remote Australia","2019 July",4,"",87244066343,"State","",145.88351,-18.474697]]'
var parsedSchoolData = JSON.parse(schoolData);
const schools = [];
const target_indices_names = ['one','two','three','four','five','six','seven','eight', 'nine', 'ten'];
const target_indices = [2,7,8,12,13,17,18,19,37,38];
parsedSchoolData.forEach(element => {
// New Object
var obj = new Object();
// Pass through all Target Indices
target_indices.forEach(neededIndex => {
console.log(neededIndex);
var propertyName = target_indices_names[target_indices.indexOf(neededIndex)];
// Set Property to Value
obj[propertyName] = element[neededIndex];
});
// Push/Add the created Object to the list
schools.push(obj);
});
console.log(schools[0]);

Filter an array of objects based on another [TS/JS]

I have two dropdowns - where each dropdown should filter an objects key. The dropdowns should not exclude each other, or both values from dropdown should work indenpentedly from each other (ie both dropdown values does not need to be true for filtering).
When I select an item from the dropdown, I get one array with two objects, for each dropdown:
[
{
"name": "Type",
"value": [
"calibration"
],
"selected": [
{
"text": "calibration"
}
]
},
{
"name": "Function group",
"value": [
"1 - Test",
"2 - Programming"
],
"selected": [
{
"text": "1 - Test"
}
]
}
]
Above shows two objects, for the two different dropdowns - one with name "type" and one with "Function group".
The "value" in the object above is all of the dropdown items.
"selected" holds the selected item from the dropdown and the filtering should be based on that.In this case we have selected "calibration" and "Test".
The "type" dropdown should filter on the data "category" field while the "function group" should filter on "groupDescription" field. The data that needs to be filtered based on the mentioned keyes and selected values looks like this:
const mockData = [
{
operationDetails: {
id: '5119-03-03-05',
number: '41126-3',
description: 'Clutch wear, check. VCADS Pro operation',
category: 'calibration', //type dropdown
languageCode: 'en',
countryCode: 'GB'
},
functionDetails: {
groupId: 411,
groupDescription: 'Test', //function group dropdown
languageCode: '',
countryCode: ''
},
lastPerformed: '2021-02-22',
time: 20,
isFavorite: false
}
,
{
operationDetails: {
id: '5229-03-03-05',
number: '41126-3',
description: 'Defective brake pad',
category: 'calibration', ///type dropdown
languageCode: 'en',
countryCode: 'GB'
},
functionDetails: {
groupId: 411,
groupDescription: 'Programming', //function group dropdown
languageCode: '',
countryCode: ''
},
lastPerformed: '2020-01-22',
time: 20,
isFavorite: false
}
]
Playground with mock data and response example from dropdown here.
How to filter the data based on the values from the dropdown objects, for each key its responsible for?
It's not the prettiest code, but it does work. The one thing that you'd want to watch out for is the regex. It would be better to not have to parse and do a straight match like category, but if your cases are static then you should be able to figure out if this will work every time. It would also be nice to have a field key in filterDetails so you know which field to try to match in the actual data and you could program that in.
const filterDetails = [
{
name: "Type",
value: ["calibration"],
selected: [
{
text: "calibration",
},
],
},
{
name: "Function group",
value: ["1 - Test", "2 - Programming"],
selected: [
{
text: "Test",
},
],
},
];
const mockData = [
{
operationDetails: {
id: "5119-03-03-05",
number: "41126-3",
description: "Clutch wear, check. VCADS Pro operation",
category: "calibration", //type
languageCode: "en",
countryCode: "GB",
},
functionDetails: {
groupId: 411,
groupDescription: "Test", //function group
languageCode: "",
countryCode: "",
},
lastPerformed: "2021-02-22",
time: 20,
isFavorite: false,
},
{
operationDetails: {
id: "5229-03-03-05",
number: "41126-3",
description: "Defective brake pad",
category: "calibration", ///type
languageCode: "en",
countryCode: "GB",
},
functionDetails: {
groupId: 411,
groupDescription: "Programming", //function group
languageCode: "",
countryCode: "",
},
lastPerformed: "2020-01-22",
time: 20,
isFavorite: false,
},
];
console.log(
"filtered mockData: ",
mockData.filter(({ operationDetails, functionDetails }) => {
let groupDescriptionMatch = false;
let categoryMatch = false;
for (const details of filterDetails) {
if (
details.name === "Type" &&
details.selected[0].text === operationDetails.category
)
categoryMatch = true;
if (details.name === "Function group") {
let parsedGroup = details.selected[0].text.match(/[a-zA-Z]+/g);
if (parsedGroup[0] === functionDetails.groupDescription) {
groupDescriptionMatch = true;
}
}
}
return groupDescriptionMatch && categoryMatch;
})
);

Select nested array object and replace it

I got an array (as result of a mongoDB query) with some elements like this:
{
"_id": "ExxTDXJSwvRbLdtpg",
"content": [
{
"content": "First paragraph",
"language":"en",
"timestamp":1483978498
},
{
"content": "Erster Abschnitt",
"language":"de",
"timestamp":1483978498
}
]
}
But I need to get just a single content field for each data array element, which should be selected by the language. So the result should be (assuming selecting the english content):
{
"_id": "ExxTDXJSwvRbLdtpg",
"content": "First paragraph"
}
instead of getting all the content data...
I tried to do it with find(c => c.language === 'en), but I don't know how to use this for all elements of the data array. Maybe it is also possible to get the data directly as a mongodb query??
You could iterate the array and replace the value inside.
var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }];
array.forEach(a => a.content = a.content.find(c => c.language === 'en').content);
console.log(array);
Version with check for content
var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }, { _id: "no_content" }, { _id: "no_english_translation", content: [{ content: "Premier lot", language: "fr", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }];
array.forEach(function (a) {
var language;
if (Array.isArray(a.content)) {
language = a.content.find(c => c.language === 'en');
if (language) {
a.content = language.content;
} else {
delete a.content;
}
}
});
console.log(array);
Given that _id and language are input variables, then you could use this aggregate command to get the expected result:
db.collection.aggregate([{
$match: {
_id: _id,
}
}, {
$unwind: '$content'
}, {
$match: {
'content.language': language,
}
}, {
$project: {
_id: 1,
content: '$content.content'
}
}])
var aobjs = [{
"_id": "ExxTDXJSwvRbLdtpg",
"content": [
{
"content": "First paragraph",
"language":"en",
"timestamp":1483978498
},
{
"content": "Erster Abschnitt",
"language":"de",
"timestamp":1483978498
}
]
}];
var result = aobjs.map(o => ({ id: o._id, content: o.content.find(c => c.language === 'en').content }));
This returns an object for each with just id and content. In this example, result would be:
[ { id: 'ExxTDXJSwvRbLdtpg', content: 'First paragraph' } ]

Meteor AutoForm adding users to select

I know I am close on this, but just cant seem to sort out what I am missing.
I am trying to add all users as options to a select in an autoform quickform. I was able to get it working with another collection, but when I use the same code for the users the value shows up as the _id but I cannot get the label to return.
Here is the structure of my users:
{
"_id": "s3EYXXK5N8NExHrke",
"emails": [{
"address": "admin#gmail.com",
"verified": false
}],
"profile": {
"firstName": "Joe",
"lastName": "Smuck",
"licenseNumber": "1234567",
"accountType": "administrator"
},
"roles": [
"administrator"
],
"createdAt": "2016-12-02T21:51:11.844Z",
"services": {
"password": {
"bcrypt": "$2a$10$NheMU2x/8RvcMxNHeWxbQOpHlWAQmopvk3KrMG9oo5ruTir2ARf8W"
},
"resume": {
"loginTokens": [{
"when": "2016-12-02T21:51:11.948Z",
"hashedToken": "8PktpX6kqK6yM+LMrqRaoqXCbwYG6gdO7MH9V/Th/dI="
}, {
"when": "2016-12-03T03:01:06.600Z",
"hashedToken": "ihn93xaN6rE8fvwBHZ3p8H6z0T7o7WChQoqD4dlkSpw="
}, {
"when": "2016-12-05T14:37:41.147Z",
"hashedToken": "7QE7HxcmDrZPFI3Omn5c1o73pMa3XzOBj3RbquCmo6U="
}]
}
}
}
I am trying to print the firstName to the select. Here is the schema I have now:
inspector: {
type: String,
label: "Inspector",
autoform: {
firstOption: 'Choose an Inspector',
options: function() {
return Meteor.users.find({}, {
sort: {
profile: 1,
firstName: 1
}
}).map(function(c) {
return {
label: c.firstName,
value: c._id
};
});
}
}
},
I would appreciate any help someone can offer!
To tie this off it should have been:
inspector: {
type: String,
label: "Inspector",
autoform: {
firstOption: 'Choose an Inspector',
options: function() {
return Meteor.users.find({}, {
sort: {
profile: 1,
firstName: 1
}
}).map(function(c) {
return {
label: c.profile.firstName,
value: c._id
};
});
}
}
},

Categories

Resources