I am looking to create following format in array in javascript.
Each item has:
url -> string (which is login URL)
name -> string
inputs -> array
And each input is:
name -> string
type -> string
value -> string
Please guide me , how to do this in JavaScript
[
{url: 'http://google.com/',
name: 'google',
inputs: [
{ name: 'search-term', type: 'string', value: 'javascript' },
{ name: 'region', type: 'country-code', value: 'IN' }
]
},
{url: 'http://yahoo.com/',
name: 'yahoo',
inputs: [
{ name: 'search-term', type: 'string', value: 'javascript' },
{ name: 'region', type: 'country-code', value: 'US' }
]
}
]
there you go.
on a related note, JSON might be worth looking at once. Though I must say, the above is not json, only json-like.
This would be tricky in a strongly typed language like Java or C#, but it's pretty easy in JavaScript.
Since JavaScript doesn't have strong typing for array values and variables, you can just create an array of object literals. Each object would contain the properties you specified. There's no need to specify the string type on each property--JavaScript will infer that for you.
While this is really easy to do, the drawback is that you're not going to get any type checking, so some code could inadvertently stick an object into one of your string fields and JavaScript won't stop it.
So just beware of the advantages and disadvantages of JavaScript's flexibility, and make sure you're doing server-side sanity checks on your data.
var items =
[
{
url: "http://...",
name: "FOO",
inputs: [
{
name: "Input1",
type: "LeTypeh"
value: "Levalueh"
},
{
name: "Input2 (Foo)",
type: "LeType2a",
value: "Levalue2j"
}
// ... (insert as many comma separated inputs as you need in the foo item
},
{
url: "http://...",
name: "BAR",
inputs: [
{
name: "Input1",
type: "LeTypeh"
value: "Levalueh"
},
{
name: "Input2 (Bar)",
type: "LeType2a",
value: "Levalue2j"
}
// ... (insert as many comma separated inputs as you need in the bar item here
}
//... (insert as many comma separated items as you need in the array here
]
Related
I have a field in my JSON that is supposed to hold the allowed values for a dropdown input.
[
{
"field": "status",
"type": "select",
"defaultValue": "open",
"allowedValues": ["open", "pending", "closed"]
},
...
]
This field must contain an array of strings or a token name (e.g. $allowedValues$) from which my code must pull the list of allowed values.
My problem is that I would like to use Ajv to validate the JSON, but apparently type string and array are not allowed to be used together.
enum InputType {
SIMPLE = "simple",
SELECT = "select",
AUTOCOMPLETE = "autocomplete",
}
interface DefaultDefinition {
...
type: InputType;
allowedValues: string | string[];
}
const defaultDefinitionSchema: JSONSchemaType<DefaultDefinition> = {
type: "object",
properties: {
...
type: {
type: "string",
enum: [InputType.AUTOCOMPLETE, InputType.SELECT, InputType.SIMPLE],
},
allowedValues: {
type: ["array", "string"],
items: { type: "string" },
}
},
required: ["allowedValues"],
additionalProperties: false,
};
Error message
Types of property 'type' are incompatible.
Type '("string" | "array")[]' is not assignable to type '"array"'.
As a workaround I have considered adding another property called allowedValuesToken of type string and making the allowedValues a string array.
The problem with this approach is, that one of those properties must be required, but never both.
I was wondering if I can specify this via if/then, but I already have a if/then condition (I could not test it yet, due to my issue with the type of allowedValues) and it seems that since I have to define the absolute list of required fields in then/else (contrary to dynamically adding/removing single fields) this is not really feasible way either.
if: {
properties: {
type: {
enum: [InputType.SIMPLE],
},
},
},
then: { required: ["type"] },
else: { required: ["type","allowedValues"] },
Can anyone tell me what is the proper way to it achieve my requirement?
I am currently having problems using my data in react-d3-tree. The data coming from the server is not compatible with the format that react-d3-tree accepts.
I was told that doing the process using a foreach function for every item will work, but I think that will be really slow, especially whe the data is too big.
The data (as shown in my console) right now is like this:
0: {
series: 'Fate'
type: 'Servant'
class: 'Saber',
},
1: {
series: 'Fate',
type: 'Servant'
class: 'Archer',
},
2: {
series: 'Fate',
type: 'Demi-servant'
class: 'Shielder',
}
And I want to achieve this structure:
[
{
name: 'Fate',
children: [
{
name: 'Servant',
children: [
{
name: 'Saber',
children: []
},
{
name: 'Archer',
children: []
}
]
},
{
name: 'Demi-servant',
children: [
{
name: 'Shielder',
children: []
}
]
}
]
}
]
This is only sample data, and later on, and I might be converting data with more children. Is there any npm package that can be helpful?
Nevermind, I actually found my answer.
I'll share these useful links in case anyone finds this question too.
https://www.npmjs.com/package/shape-json
https://www.npmjs.com/package/shape-array
Hello community :) I implemented lots of good working functionality with firebase -> realm. Now i tried to edit a structure and I am running through the wildest error messages.
What is right for sure:
Firebase sends the data
Data is Converted (e.g. Firebase has "brands" as array -> is converted to a string for Realm Schema)
The error appears when firebase updates
Not every firebase content has all fields (e.g. Like you can see out of Realm Schema some fields are optional: true)
Fields where i maybe expect an issue:
Maybe its not possible to say that the ReferentList is optional (or i implemented it wrong): See Realm Schema const ReferentsList
What i tried
Debug before realm.create (Realm set) Result: Every data came in the right format
Checked all input values if they are int, string, ...
Hopefully someone can help me here because i got completely stuck with this issue and its necesarry to continue for my project. I want to know:
The solution why or what to do
A posibility to debug realm in a better way
Thank you in advance for your time and help :)
Error message: Value not convertible to a number
Firebase datastructure
"begin" : "2017-05-15T15:50:00.000Z",
"description" : "abc",
"end" : "2017-05-15T16:15:00.000Z",
"id" : 6,
"language" : [ 1 ],
"location" : "L 1.02",
"member" : 20,
"referent" : [ 1, 3 ],
"register" : true,
"title" : "Sound of Silence",
"track" : 6,
"type" : 3,
"brands" : [ 1, 2, 3 ]
Realm Schema
const ReferentListSchema = {
name: 'ReferentList',
properties: {
id: {
type: 'int',
optional: true
}
}
}
const LanguageListSchema = {
name: 'LanguageList',
properties: {
id: 'int'
}
}
const EventSchema = {
name: 'Events',
primaryKey: 'id',
properties: {
id: 'int',
begin: {
type: 'date',
optional: true
},
end: {
type: 'date',
optional: true
},
title: 'string',
description: 'string',
register: 'bool',
member: {
type: 'int',
optional: true
},
language: {
type: 'list',
objectType: 'LanguageList'
},
location: 'string',
referent: {
type: 'list',
objectType: 'ReferentList'
},
type: 'int',
track: {
type: 'int',
optional: true
},
img: {
type: 'string',
optional: true
},
brands:{
type: 'string',
optional: true
}
}
}
Realm set
set(obj) {
realm.write(() => {
if(obj.referent){
obj.referent = obj.referent.map(function(id) {
return {id};
})
}
if (obj.language){
obj.language = obj.language.map(function(id) {
return {id};
})
}
realm.create('Events', obj, true);
});
}
Solved:!
The issue got solved through wrong data at firebase. Some Date Objects hasent been set correct.
How i got to the solution
When i tried to debugg the code i made a try/catch block around:
try{
realm.create('Events', obj, true);
}catch(error){
console.log(obj);
console.log(error);
}
Through this debug i found the right data wich was wrong. Before it just showed me all objects and afterwards the error.
I wont close this question because of the chance to help someone with the same issues.-
I used jQuery QueryBuilder (http://querybuilder.js.org/) to create the rules and generate JSON and store them in a database. Now, I need to do the reverse operation. This means,that the input will be in the form of a JSON which would get parsed and display the rules in the UI in the same format of QueryBuilder for modification/deletion. Can someone provide pointers/sample code on how to achieve this?
To achive this you have to use the *setRules * method --> see documentation here
var rules_json= {
condition: 'AND',
rules: [{
id: 'price',
operator: 'less',
value: 10.25
}, {
condition: 'OR',
rules: [{
id: 'category',
operator: 'equal',
value: 2
}, {
id: 'category',
operator: 'equal',
value: 1
}]
}]
};
$('#queryBuilder').queryBuilder('setRules', rules_json);
I have a collection in meteor that comes from a form(Form is created using aldeed:autoform). I want to extract the id of the last element/object (or most recent entry) of the collection. How can I do that??
Edit: Here is what the autoform looks like:
PlayersList = new Mongo.Collection("players");
PlayersList.allow({
insert: function(){
}
});
PlayersListSchema = new SimpleSchema({
name: {
type: String,
label: "Name",
},
age: {
type: Number,
label: "Age"
},
gender:{
type: String,
label: "Gender"
},
country: {
type: String,
label: "Country of Birth",
},
race: {
type: String,
label: "Ethnicity",
},
income: {
type: Number,
label: "Income",
},
education: {
type: String,
label: "Education Level",
},
});
PlayersList.attachSchema(PlayersListSchema)
With Each form an id is generated. I want the id of the last form in the database.
var lastEntryId = [].slice.call( *meteorCollection* ).reverse()[0].id;
or if that looked to short and easy, you can choose a shorter & faster one:
var lastEntryId = [].slice.call( *meteorCollection* ).pop().id;
or you could use a true Spartan, even faster and truly laconic:
var lastEntryId = [].pop.call( *meteorCollection* ).id;
You'll get an empty string if id property attribute has not been set.
var recent_entry = PlayersList.find().sort({_id:-1}).limit(1);
where '-1' in sort is for Newest to oldest arrangement, if you use '1' it's vice-versa.