Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have tried to create custom language with auto complete ( intellisenses ). It's not working out. can any one help me to achieve this.
Code
https://stackblitz.com/edit/angular-7-master-emjqsr?file=src/app/app.module.ts
You're almost there.
You only need to return object like { suggestions: suggestions } instead of array in your provideCompletionItems method and you're done:
monaco.languages.registerCompletionItemProvider('dummy', {
provideCompletionItems: () => {
var suggestions = [
{
label: 'simpleText',
kind: monaco.languages.CompletionItemKind.Text,
insertText: 'simpleText',
},
{
label: 'testing',
kind: monaco.languages.CompletionItemKind.Keyword,
insertText: 'testing(${1:condition})',
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
},
{
label: 'ifelse',
kind: monaco.languages.CompletionItemKind.Snippet,
insertText: ['if (${1:condition}) {', '\t$0', '} else {', '\t', '}'].join('\n'),
insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
documentation: 'If-Else Statement',
},
];
return { suggestions: suggestions };
},
});
Ng-run Example
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 days ago.
This post was edited and submitted for review 7 days ago.
Improve this question
Define a variable car, an Object with the following properties:
model of ’Nissan’
color of ’Blue’
numWheels, a number of wheels. Value is 4
The object should also have the following method:
Method drive that returns ’Vroom!’
What I tried:
const car = {
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: function() {
return'Vroom!'
},
}
console.log (car)
What I got:
{
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: [Function: drive]
}
I have changed “console.log (‘Vroom!)”
To “return ‘Vroom!’”
And I get the result I need
Method drive that returns Vroom!
In your code, your drive() method is logging Vroom! to the console, not returning it as the value of the function. Change console.log('Vroom!') to return 'Vroom!' and use console.log(car.drive()) instead of console.log(car). Your complete code should look something like this:
const car = {
model: 'Nissan',
color: 'Blue',
numwheels: 4,
drive: function() {
return 'Vroom!';
},
}
console.log (car.drive());
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I am using javascript.I have a json data set. I am trying to convert in a particular way. Can someone write the function so that I can convert it.
Given Json data:
{
"members":[
{
"value":"view",
"code":"reservations"
},
{
"value":"view",
"code":"dashboard"
}
]
}
Trying to convert in:
{
"members": [
{
"view_reservation": true,
"edit_reservation": true,
"create_reservation": true,
"delete_reservation": true
},
{
"view_dashboard": true,
"edit_dashboard": true,
"create_dashboard": true,
"delete_dashboard": true
}
]
}
Can someone write a function in javascript to convert it...
const data = JSON.parse(jsonData);
data.members = data.members.map(member => ({
`${member.view}_${member.code}`: true,
`edit_${member.code}`: true,
`create_${member.code}`: true,
`delete_${member.code}`: true
}));
you multiply output, we can just assume, that you always get 4 rows (CRUD-like)
Anyway, the approach of converting your object/array to the desired output can be done using something like this (where x is your current object):
var y = { "members": [] };
x.members.forEach(element => { y.members.push({ [element.code + '_' + element.value]: true}) } );
you can just edit the new object in the y.members.push to your desire
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Why optional chaining is not working here?
.html
{{userItemModel?.item?.priceList[0]?.sellerUrl}}
It shows this error.
TypeError: Cannot read property '0' of undefined
"#angular/core": "~10.1.1",
"typescript": "~4.0.2"
"priceList": [
{
"amount": 14.5,
"currency": "USD",
"sellerUrl": "https://www.bay.com/itm/Lear-6910-/33372049",
"basePrice": 15,
"discount": 10
}
],
Update
Use case 1:
Parser Error: Unexpected token [, expected identifier or keyword at column 33
in [ {{userItemModel?.item?.priceList?.[0]?.sellerUrl}}
] in
Use case 2:
Parser Error: Unexpected token [, expected identifier or keyword at column 33 in [
{{userItemModel?.item?.priceList?.[0].sellerUrl}}
] in
To correctly chain array access without an exception you should use arr?.[index]:
var foo = {}
try{
console.log(foo?.bar[0])
}
catch(e){
console.log(e.message)
}
console.log(foo?.bar?.[0])
Edit: This is a known issue with angular at this time.
I tried to recreate your case with Typescript Playground to see what happen under the hood and to be sure I didn't make a typo.
Here a link where I did it : TypeScript Playground
Here the exemple :
type PriceList = {
amount: number;
currency: string;
sellerUrl: string;
basePrice: number;
discount: number;
}
type Item = {
priceList?: PriceList[]
}
type UserItemModel = {
item?: Item
}
const userItemModel: UserItemModel = {
item: {
priceList: [
{
amount: 14.5,
currency: "USD",
sellerUrl: "https://www.bay.com/itm/Lear-6910-/33372049",
basePrice: 15,
discount: 10
}
]
}
}
const firstPriceListSellerUrl = userItemModel?.item?.priceList?.[0]?.sellerUrl;
With this syntax, I don't see a problem.
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 2 years ago.
Improve this question
enter code hereJS Code:
DOCUMENTS = [{
name: 'I551Indicator',
text: ‘School Document’,
},
{
name: 'I553Indicator',
text: ‘Birth Certificate’,
}
];
User_Selected_Documents = [{
I551Indicator: false,
I553Indicator: true
}];
From the DOCUMENTS array, I have to display the text of the document for the keys whose value is true in User_Selected_Documents array.
I tried the below, seems to get the text
const test = DOCUMENTS.map(doc => doc).map(doc => doc.name).filter(DOCUMENTS.map(selctedDocuments));
trying to find the key whose value is true from User_Selected_Documents.filter(selectedDocument => Object.values(selectedDocument) === true)
dosen't seem to work.
EXPECTED RESULT: I this case it is Birth Certificate since I553Indicator is true
You need to use filter and then map. Below code works:
DOCUMENTS = [{
name: 'I551Indicator',
text: 'School Document',
},
{
name: 'I553Indicator',
text: 'Birth Certificate',
}
];
User_Selected_Documents = [{
I551Indicator: false,
I553Indicator: true
}];
const result = DOCUMENTS.filter(x => User_Selected_Documents[0][x.name]).map(x => x.text)
console.log(result[0])
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Say our JSON data comes from a single MySQL table:
someJSON = [ { name: 'bill' , sex:'M', income:50000 },
{ name: 'sara' , sex:'F', income:100000 },
...
];
And say the pseudo-code is:
"Get all the person objects of all sex:F of income > 60000`".
Are there any javascript libraries that would allow one to code such queries on this JSON data using a SQL or SQL-like syntax.
In case you are curious, some context:
I am making the front-end of a data analysis web service for my organization without knowing what the future backend will be. In the future they will migrate their data from MS Access tables to some-sort of MySQL-type database. Until then I am using static JSON files to start development and was thinking it may be helpful for them in the future to have my javascript queries appear as MySQL queries. (The current MS Access database is unreachable from the web.)
Check out jslinq:
var myList = [
{FirstName:"Chris",LastName:"Pearson"},
{FirstName:"Kate",LastName:"Johnson"},
{FirstName:"Josh",LastName:"Sutherland"},
{FirstName:"John",LastName:"Ronald"},
{FirstName:"Steve",LastName:"Pinkerton"}
];
var exampleArray = JSLINQ(myList)
.Where(function(item){ return item.FirstName == "Chris"; })
.OrderBy(function(item) { return item.FirstName; })
.Select(function(item){ return item.FirstName; });
You can try alasql.js. It is pure JavaScript client-side SQL-server, where you can do queries over JSON objects.
// Fill table with data
var data = [ { name: 'bill' , sex:'M', income:50000 },
{ name: 'sara' , sex:'F', income:100000 }];
// Do the query
console.log(alasql("SELECT * FROM ? WHERE sex='F' AND income > 60000",[data]));
Try this in Fiddle
I use Taffydb.
TaffyDB is an opensouce library that brings database features into your JavaScript applications.
http://taffydb.com/
I've seen a few linq like javascript libraries in past google searches.
Edit- here's a couple
http://linqjs.codeplex.com/
http://jslinq.codeplex.com/
http://jsinq.codeplex.com/ <-- really cool playground for this one
You may be interested in checking out MongoDB, a JSON-style data store with full queryability. Here is its query syntax:
db.users.find({'last_name': 'Smith'})
For your example question:
db.users.find({'sex': 'F', 'income' : {$gt : 60000}})
There is also JsonSql which seems to be similar like what you are looking for. Only problem is that it hasn't been updated in 12/30/2007. Still the code is there to grab and play with.
There is also a XPath style query called JSONPath that I like http://goessner.net/articles/JsonPath/
And there is this http://code.google.com/p/jfunk/
Which users jQuery style selectors to filter data
Depending on what browsers/versions you must support, I would strive to use HTML5 client-side SQL, pushing my JSON data into one or more tables and harnessing the power of true SQL queries.
Here's the draft spec: http://www.w3.org/TR/webdatabase/
I know the question is old but I just came here through a Google search. I'm just following a talk about objeq. Looks pretty promising and very much what you are searching for.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="linq.js"></script>
<script type="text/javascript">
var jsonArray = [
{ "user": { "id": 100, "screen_name": "d_linq" }, "text": "to objects" },
{ "user": { "id": 130, "screen_name": "c_bill" }, "text": "g" },
{ "user": { "id": 155, "screen_name": "b_mskk" }, "text": "kabushiki kaisha" },
{ "user": { "id": 301, "screen_name": "a_xbox" }, "text": "halo reach" }
]
// ["b_mskk:kabushiki kaisha", "c_bill:g", "d_linq:to objects"]
var queryResult = Enumerable.From(jsonArray)
.Where(function (x) { return x.user.id < 200 })
.OrderBy(function (x) { return x.user.screen_name })
.Select(function (x) { return x.user.screen_name + ':' + x.text })
.ToArray();
// shortcut! string lambda selector
var queryResult2 = Enumerable.From(jsonArray)
.Where("$.user.id < 200")
.OrderBy("$.user.screen_name")
.Select("$.user.screen_name + ':' + $.text")
.ToArray();
console.log(queryResult);
console.log(queryResult2);
</script>
</head>
<body>
</body>
</html>