How can get certain field value from dataSource in kendo UI - javascript

I have defined dataSource like this:
var dataSource = new kendo.data.DataSource({
data: [
{Id: 1, name: "Jane Doe", description: "some description", numberValue: "3000.00" },
{Id: 2, name: "John Connor", description: "description temp", numberValue: "1800.00" },
{Id: 3, name: "T-100", description: "descr tmp", numberValue: "2200.00"}
],
schema: {
model: {
id: "Id",
fields: {
Id: { type: "number" },
name: { type: "string" },
description: { type: "string" },
numberValue: { type: "number" }
}
}
}
});
How can I get certain field value from dataSource?
For example: I want get value for field numberValue (where Id = 1, or Id = 2 or Id = 3).
If I call dataSource.data.numberValue then nothing happening. Any help really appreciated. Thank you in advance.

dataSource.data is an array obviously.

Related

JavaScript reduce methods [duplicate]

This question already has answers here:
How to convert array of model objects to object with model ids as keys?
(6 answers)
Closed 2 months ago.
I want to convert an array of objects in to object with one of the properties as a key
how to convert the array
const data = [
{
id: "first",
name: "Product 1",
type: "selection"
},
{
id: "second",
name: "Product 2",
type: "text"
},
{
id: "third",
name: "Product 3",
type: "csv"
}
]
like this
{
first: { name: 'Product 1', type: 'selection' },
second: { name: 'Product 2', type: 'text' },
third: {name: 'Product 3', type: 'csv' },
}
using ARRAY.REDUCE METHOD?
try
const data = [
{
id: "first",
name: "Product 1",
type: "selection"
},
{
id: "second",
name: "Product 2",
type: "text"
},
{
id: "third",
name: "Product 3",
type: "csv"
}
]
const obj = Object.fromEntries(data.map(item => [item.id, item]));
console.log(obj);
Check this
const data = [
{
id: "first",
name: "Product 1",
type: "selection"
},
{
id: "second",
name: "Product 2",
type: "text"
},
{
id: "third",
name: "Product 3",
type: "csv"
}
]
const result = {};
data.forEach(d=>{
result[d.id] = {
name: d.name,
type:d.type
}
});
Here's how to do using Array.reduce():
const data = [
{
id: "first",
name: "Product 1",
type: "selection"
},
{
id: "second",
name: "Product 2",
type: "text"
},
{
id: "third",
name: "Product 3",
type: "csv"
}
];
const result = data.reduce((acc, obj) => {
const { id, ...rest } = obj;
acc[id] = rest;
return acc;
}, {});
console.log(result);

change text of rows in type matrix surveyjs

I am building best employee survey project using surveyjs. When working in matrix type, I get trouble how to change text "employee1,employee2,employee3" with real name employee
see picture.
so far here is my code:
var json = {
title: "Software developer survey.",
pages: [
{
title: "Part 1",
questions: [
{
type: "matrix",
name: "Question1",
//isAllRowRequired: true,
title: "Please indicate if you agree or disagree with the following statements",
columns: [
{
value: 1,
text: "Strongly Disagree"
}, {
value: 2,
text: "Disagree"
}, {
value: 3,
text: "Neutral"
}, {
value: 4,
text: "Agree"
}, {
value: 5,
text: "Strongly Agree"
}
],
rows: [
{
value: "valueEmployee1",
text: "Employee1 ![Employee1](https://bezone.files.wordpress.com/2008/02/soeharto.jpg =50x50)"
}, {
value: "valueEmployee2",
text: "Employee2"
}, {
value: "valueEmployee3",
text: "Employee3"
}
]
},
//
]
},
thank you for your help
You can add matrix rows dynamically:
var json = {
questions: [
{
type: "matrix",
name: "score",
title: "Please score the employees in the list",
columns: [
{
value: 1,
text: "Bad"
}, {
value: 2,
text: "So-so"
}, {
value: 3,
text: "Good enough"
}, {
value: 4,
text: "Good"
}, {
value: 5,
text: "Excellent"
}
],
rows: [
]
}
]
};
window.survey = new Survey.Model(json);
var q = survey.getQuestionByName("score");
var employees = [{ id: "id1", name: "John"}, { id: "id2", name: "Ben"}, { id: "id3", name: "Chack"}]
employees.forEach(function(employee) {
q.rows.push(new Survey.ItemValue(employee.id, employee.name));
});
Here is the working sample - https://plnkr.co/edit/OMBt1n02Qc8f5znUmOHE?p=preview

Converting Flat JSON To Parent-Child relational Structure

we want to convert Below JSON to Parent-Child Relation for a tree view structure, based on parent Link and Display Order.
if parent link is empty it is parent
if parent link exist it should add as a child
items should sort based on display order
[{
"ParentLink":{ },
"Id":1,
"Title":"Home ITSD test new",
"Url":{
"Description":"#",
"Url":"https://technologies.sharepoint.com/"
},
"DisplayOrder":1,
"IsActive":true,
"ID":1},{
"ParentLink":{
},
"Id":2,
"Title":"Link6",
"Url":{
"Description":"#",
"Url":"https://technologies.sharepoint.com/"
},
"DisplayOrder":2,
"IsActive":true,
"ID":2},{"ParentLink":{
"Title":"Link6"
},
"Id":3,
"Title":"link7",
"Url":{
"Description":"#",
"Url":"https://technologies.sharepoint.com/"
},
"DisplayOrder":21,
"IsActive":true,
"ID":3},{
"ParentLink":{
"Title":"Link6"
},"Id":4,
"Title":"link8",
"Url":{
"Description":"#",
"Url":"https://technologies.sharepoint.com/"
},
"DisplayOrder":22,
"IsActive":true,
"ID":4},{
"ParentLink":{
"Title":"link8"
},
"Id":5,
"Title":"link9",
"Url":{
"Description":"#",
"Url":"https://technologies.sharepoint.com/"
},
"DisplayOrder":221,
"IsActive":true,
"ID":5}]
is there any other libraries already available or any easy way to do so
While Title and ParentLink.Title do not have matching values, you could take both values as lower case letters and use both to generate a tree by iterating the given array and use an object for assigning nodes and children.
For the wanted order, I suggest to sort the data in advance and take the nodes as the actual order for generating the tree. This is easier than to sort later only parts.
Just wondering, why has the data two id properties?
var data = [{ ParentLink: {}, Id: 1, Title: "Home ITSD test new", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 1, IsActive: true, ID: 1 }, { ParentLink: {}, Id: 2, Title: "Link6", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 2, IsActive: true, ID: 2 }, { ParentLink: { Title: "Link6" }, Id: 3, Title: "link7", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 21, IsActive: true, ID: 3 }, { ParentLink: { Title: "Link6" }, Id: 4, Title: "link8", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 22, IsActive: true, ID: 4 }, { ParentLink: { Title: "link8" }, Id: 5, Title: "link9", Url: { Description: "#", Url: "https://technologies.sharepoint.com/" }, DisplayOrder: 221, IsActive: true, ID: 5 }],
tree = function (data, root) {
var r = [], o = {};
data.forEach(function (a) {
var id = a.Title.toLowerCase(),
parent = a.ParentLink && a.ParentLink.Title && a.ParentLink.Title.toLowerCase();
a.children = o[id] && o[id].children;
o[id] = a;
if (parent === root) {
r.push(a);
} else {
o[parent] = o[parent] || {};
o[parent].children = o[parent].children || [];
o[parent].children.push(a);
}
});
return r;
}(data, undefined);
console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

get total amount for each value for specific key in nested array

Array:
[
{
id: 1,
title: "Test 1",
eventTypeId: 2,
eventType: {
id: 2,
name: "Event X",
category: {
id: 1,
name: "Study 1"
}
}
},
{
id: 2,
title: "Test 2",
eventTypeId: 2,
eventType: {
id: 2,
name: "Event Y",
category: {
id: 1,
name: "Study 2"
}
}
},
{
id: 3,
title: "Test 3",
eventTypeId: 2,
eventType: {
id: 2,
name: "Event Z",
category: {
id: 2,
name: "Study 1"
}
}
}
];
The data comes from API request and inside the success function I'm attributing the response to $scope.initialDataLength as below:
var getDataLength = function () {
$http.get(urlData).success(function (data) {
$scope.initialDataLength = data;
}
}
View:
<td class="col-md-2">
{{ initialDataLength.length }}
</td>
But I want to get the length of the name of the Category object.
I was trying something like:
angular.forEach (data, function (typeId) {
var type = typeId.eventType;
angular.forEach (type, function (category) {
var cat = category.name;
console.log("cat", cat.length)
})
});
But it shows me the following error:
Cannot read property 'length' of undefined
Could anybody explain me, where is my mistake?
you don't need the inner forEach(). You already have the object reference and you just need to access properties of that object
angular.forEach (data, function (typeId) {
console.log(typeId.eventType.category.name)
});

Push data to array without index

I'd like to know how can i add this
{ post_id: 1, text: "text", creation: "date" }
to the "posts" in a array like this
var posts = [
{
post_id: 5,
text: "text",
creation: "date"
},
{
group: "favPosts",
posts: [
{ post_id: 2, text: "text", creation: "date" },
{ post_id: 7, text: "text", creation: "date" }
]
},
{
post_id: 8,
text: "text",
creation: "date"
}
]
I've already tried searching a hundred times but i can't seem to find a answer that suits my case, I've been trying to insert it with commands like slice and push but i can't seem to get it right, i'm still a beginner in javascript.
PS: I've come a solution but i don't think it's very "efficient";
addToGroup("favPosts");
function addToGroup(group) {
for(id in posts) {
if(posts[id].group == group){
posts[id].posts.push({ post_id: 10, text: "did it", creation: "date" });
console.log(posts[id]);
}
}
}
This should work with a push, I don't see why this wouldn't work.
Here is an example, I just used angular to easily display in HTML, not needed for what you are doing:http://plnkr.co/edit/PBCYxfjMqV3jzggMHJZ7?p=preview
var people = [
{
name: "Joe",
surname: "surname"
},
{
group: "name",
people: [{ name: "name", surname: "surname" }, { name: "name", surname: "surname" }]
},
{
name: "James",
surname: "surname"
}
];
var person = { name: "Jennifer", surname: "surname" };
people.push(person);

Categories