JSON data formetter in a particular way [closed] - javascript

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

Related

Create an object “car” and have one of its properties display a string [closed]

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());

ES6: Get value from an array based on values of another 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 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])

How to create Monaco Editor custom language in angular? [closed]

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

How to filter data from array using the id? [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 years ago.
Improve this question
I am using a API and that api give me list of users detail, I want to filter the details of user according to logged in user id, How can i filter data using specific id from array.
Any solution appreciated...!
I am considering you are getting array of objects.
suppose you have array a. then
for (var i =0;i<a.length,a++){
if(a[i].user_id == <specific user_id>){
// your code
break;
}
}
It's tough to make a suggestion without more information, but for example, if your data looks like apiData below, then you could simply make a new array and push all the items with the chosen userId property value to it:
const apiData = [
{ userId: 1, favoriteColor: "blue" },
{ userId: 2, favoriteColor: "yellow" },
{ userId: 1, favoriteFood: "pasta" },
{ userId: 2, favoriteFood: "cookies" },
];
const filteredData = [];
apiData.forEach(function(item){
if(item.userId == 1){
filteredData.push(item);
}
});
console.log(filteredData);
include underscore.js in your app .
._filter(x => x.apiResopnseId == yourId); //return filtered users detail

Get value in Json with ajax [closed]

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 4 years ago.
Improve this question
I have an API that returns JSON like this:
[{
"Code": "001",
"Name": "xyz",
"Members": [{
"FullName": "User1"
}]
}, {
"Code": "002",
"Name": "asd",
"Members": [{
"FullName": "User2"
}]
}]
I want to replace Name and member in my label matching with "Code" and i want to do that with ajax. How can achieve the matching with Code?
This is my implementation:
$('#btnFind').on('click', function () {
var Code = $("#input_edit_code").val();
$.ajax({
type: "get",
url: "/mvc/ComplaintHandlerSquadGetListPage",
success: function () {
alert();
},
error: function (ex) {
alert(ex.title);
}
});
})
You can use javascript's filter and map Array's functions.
Let's pretend you store the code typed by the user inside this codeNumber var
var codeNumber = '002';
Your array of members:
var members = [{"Code":"001","Name":"xyz","Members":[{"FullName":"User1"}]},{"Code":"002","Name":"asd","Members":[{"FullName":"User2"}]}];
A function to filter your array by Code :
var byCode = function(member) {
return member.Code === codeNumber;
};
A function to get the name of the filtered member :
var getName = function(member) {
return member.Name;
};
You get the member's name
var memberName = members.filter(byCode).map(getName).toString();
console.log(memberName);
I don't believe AJAX and simple JSON alone can do what you want.
JSON is a way to structure objects - JavaScript Object Notation. It only structures data.
AJAX is an Asynchronous means of making an HttpRequest. It asks a server for something.
If you use AJAX to ask for a JSON file. You will get the JSON data.
I believe you have two options:
Use AJAX to request the data from the server. The server will then have to load the JSON file, find the correct data and return the correct data to AJAX.
Use AJAX to get the JSON data. Find the correct data with JavaScript.
Inside the success handler, you can compare something like this:
success: function(response) {
// yourObject that matches the code
var yourObject = response.filter(function(item) {
return item.Code === Code
});
// wherever you want to show this name
$("body").text(yourObject.Name);
}
$.ajax({
type: "get",
url: "/mvc/ComplaintHandlerSquadGetListPage",
success: function (result) {
jQuery(result).each(function(i, item){
if(item.Code == Code)
{
$('#myid').text(item.Name);
}
},
error: function (ex) {
alert(ex.title);
}
});

Categories