I have an array on objects like this,
[
{
"link": "link1",
"model": "model1",
"role": "role1",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role3",
"access": "true"
},
{
"link": "link1",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link1",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role2",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link1",
"model": "model2",
"role": "role4",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model1",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model1",
"role": "role4",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role1",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role2",
"access": "true"
},
{
"link": "link2",
"model": "model2",
"role": "role3",
"access": "false"
},
{
"link": "link2",
"model": "model2",
"role": "role4",
"access": "true"
}
]
With respect to this question, the input was a CSV sample with very less number of rows. In real I have a large CSV file and I found a way to import it and convert to JSON using d3.js, but the problem is I'm unable to convert it to the desired format. I tried using the data.forEach and the output was really weird and I couldn't understand why I get model2 alone in both the links.
Code:
d3.csv('data.csv', function(data) {
var newData = {};
data.forEach(function(e, i) {
newData[e.link] = {};
newData[e.link][e.model] = {};
})
d3.select('main').append('pre')
.text(JSON.stringify(newData, null, ' '));
});
Output:
{
"link1": {
"model2": {}
},
"link2": {
"model2": {}
}
}
Desired Output:
"link1": {
"model1": {
"role1": true,
"role2": true,
"role3": true,
"role4": true,
},
"model2": {
"role1": false,
"role2": false,
"role3": false,
"role4": false,
}
},
"link2": {
"model1": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
},
"model2": {
"role1": false,
"role2": true,
"role3": false,
"role4": true,
}
}
Any help will be much appreciated. Thanks in advance.
In your forEach, you're not assigning the role and access pair on each iteration, and you're always emptying each section.
Change your forEach with this...
data.forEach(function(e, i) {
// Check if this "link" already exists exists, if not, create it.
if (!newData[e.link])
newData[e.link] = {};
// Check if this "model" already exists inside of this "link", if not, create it.
if (!newData[e.link][e.model])
newData[e.link][e.model] = {};
newData[e.link][e.model][e.role] = e.access;
});
I hope it helps
Related
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 1 year ago.
Improve this question
I am using typescript.
I want to create a new array that compares the code values of the arrays groupA and groupB and adds same:true to the same codes and same:false to the ones that are not the same. I want to combine the same codes into one.
I want to create an array like newGroup.
"groupA": [
{
"code": "aaaaa",
"name": "group1",
"editable": false,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
},
]
"groupB": [
{
"code": "aaaaa",
"name": "group1",
},
{
"code": "bbbb",
"name": "group2",
},
]
"newGroup": [
{
"code": "aaaaa",
"name": "group1",
"editable": false,
"same":true,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
"same":true,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
"same":false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
"same":false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
"same":false,
},
]
const groupA =[
{
"code": "aaaaa",
"name": "group1",
"editable": false,
},
{
"code": "bbbb",
"name": "group2",
"editable": false,
},
{
"code": "cccc",
"name": "group3",
"editable": false,
},
{
"code": "dddd",
"name": "group4",
"editable": false,
},
{
"code": "eeee",
"name": "group5",
"editable": false,
}
]
const groupB=[
{
"code": "aaaaa",
"name": "group1",
},
{
"code": "bbbb",
"name": "group2",
}
] ;
const newGroup = groupA.map(a => {
const found = groupB.find(b=> (b.code===a.code && b.name===a.name));
return {...a,...found,same:!!found};
})
console.log(newGroup);
when i use relation referencesMAny with array in loopback. i have got error when i remove referencesMAny relation it's working. i am using loopback 3
{ "error": {
"statusCode": 500,
"name": "Error",
"message": "5d53eaa49b826748f0b72ca5is not an ObjectID string",
}}
it's my json schema :
{
"name": "Products",
"plural": "Products",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mixins": {
"TimeStamp": true
},
"properties": {
"name": {
"type": "string",
"default": ""
},
"image": {
"type": [],
"required": true,
"default": []
},
"about": {
"type": "string",
"required": true,
"default": ""
},
"categoryId": {
"type": [],
"required": false,
"default": []
},
"userId": {
"type": "string",
"required": true,
"default": ""
},
"storeId": {
"type": "string",
"required": true,
"default": ""
},
"location": {
"type": "geopoint",
"required": false
},
"price": {
"type": "object",
"required": true,
"default": {
"discount": "",
"actualRate": "",
"finalRate": ""
}
},
"createdAt": {
"type": "date"
},
"updatedAt": {
"type": "date"
}
},
"validations": [],
"relations": {
"userDeatils": {
"type": "belongsTo",
"model": "Users",
"foreignKey": "userId"
},
"categories": {
"type": "referencesMany",
"model": "Categories",
"foreignKey": "categoryId"
},
"storeDeatils": {
"type": "belongsTo",
"model": "Users",
"foreignKey": "userId"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
],
"methods": {}
}
my json data object like this :
{
"name": "deara",
"image": ["dsadsa"],
"about": "dear ser",
"categoryId": [
"5d53eaa49b826748f0b72ca5"
],
"userId": "dsdsadsa",
"storeId": "dsdsadsadsad",
"location": {
"lat": 0,
"lng": 0
},
"price": {
"discount": "121",
"actualRate": "321",
"finalRate": "231"
},
"createdAt": "2019-08-09T12:02:54.514Z",
"updatedAt": "2019-08-09T12:02:54.514Z"
}
i don't understand where am i wrong.
thanks in advance
Loopback expects that dsadsad232323 will be an identifier of an object of another model, so categoryId must be array of id.
I'm trying to manipulate data received from an API, it requires grouping according to the category like country, city and state based on their display_priority. Also, sort similar category items based on their display_priority. I did try using JS functions and also with lodash library, but couldn't get the desired output.
API response:
[
{
"category": {
"name": "country",
"display_priority": 1
},
"name": "US",
"display_priority": 2,
"enabled": false
},
{
"category": {
"name": "city",
"display_priority": 3
},
"name": "Greenville",
"display_priority": 1,
"enabled": true
},
{
"category": {
"name": "state",
"display_priority": 2
},
"name": "Alabama",
"display_priority": 2,
"enabled": true
},
{
"category": {
"name": "state",
"display_priority": 2
},
"name": "Arizona",
"display_priority": 1,
"enabled": false
},
{
"category": {
"name": "city",
"display_priority": 3
},
"name": "Houston",
"display_priority": 2,
"enabled": false
},
{
"category": {
"name": "country",
"display_priority": 1
},
"name": "Germany",
"display_priority": 1,
"enabled": true
}
]
Required output:
[
{
"category": "country",
"list": [
{
"name": "Germany",
"enabled": true
},
{
"name": "US",
"enabled": false
}
]
},
{
"category": "state",
"list": [
{
"name": "Arizona",
"enabled": false
},
{
"name": "Alabama",
"enabled": true
}
]
},
{
"category": "city",
"list": [
{
"name": "Greenville",
"enabled": true
},
{
"name": "Houston",
"enabled": false
}
]
}]
You can easily get this result with a single Array.reduce and object destructuring:
const data = [{ "category": { "name": "country", "display_priority": 1 }, "name": "US", "display_priority": 2, "enabled": false }, { "category": { "name": "city", "display_priority": 3 }, "name": "Greenville", "display_priority": 1, "enabled": true }, { "category": { "name": "state", "display_priority": 2 }, "name": "Alabama", "display_priority": 2, "enabled": true }, { "category": { "name": "state", "display_priority": 2 }, "name": "Arizona", "display_priority": 1, "enabled": false }, { "category": { "name": "city", "display_priority": 3 }, "name": "Houston", "display_priority": 2, "enabled": false }, { "category": { "name": "country", "display_priority": 1 }, "name": "Germany", "display_priority": 1, "enabled": true } ]
const result = data.reduce((accumulator, {category, name, enabled}) => {
let key = category.name
accumulator[key] = accumulator[key] || { category: key, list: [] }
accumulator[key].list.push({name, enabled})
return accumulator
}, {})
console.log(Object.values(result))
I am implementing kendo treeview of checkboxes in angular. I get no error, but tree is not rendering. My code is here:
addCtrl.js
$scope.updateTree = function () {
websiteService.getWebsiteFeaturesTree(webName, function (data) {
$scope.treeOptions = {
checkboxes: {
checkChildren: true
}
};
$scope.treeDataSource = data;
});
}
add.html
<div kendo-tree-view k-options="treeOptions" k-data-source="treeDataSource"></div>
app.js
var app = angular.module('app', ['ui.bootstrap', 'ngRoute', 'kendo.directives']);
I get datasource from webservice. here is json
// 20160930204148
// http://localhost/api/Website/GetWebsiteFeaturesTree?webName=MetisEmptyTemplate
[
{
"Name": "Section",
"text": "Admin",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "Matrix Analysis",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "Risk Categories and Questions",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "Risk Colors",
"checked": true,
"items": [
]
},
{
"Name": "Tab",
"text": "SWOT Types",
"checked": false,
"items": [
]
}
]
},
{
"Name": "Section",
"text": "Strategy",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "SWOT Analysis",
"checked": false,
"items": [
{
"Name": "Grid",
"text": "SWOT Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": false,
"items": [
{
"Name": "Output",
"text": "SWOT Box",
"checked": false,
"items": [
]
}
]
}
]
}
]
},
{
"Name": "Section",
"text": "Portfolio",
"checked": true,
"items": [
{
"Name": "Tab",
"text": "Matrix",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Matrix Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Matrix Output",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Strategy",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Strategy Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Investment Level by Objective",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Risk",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Risk Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Portfolio Risk",
"checked": true,
"items": [
]
},
{
"Name": "Output",
"text": "Portfolio Risk Stacked Bar",
"checked": true,
"items": [
]
}
]
}
]
}
]
},
{
"Name": "Section",
"text": "Project",
"checked": false,
"items": [
{
"Name": "Tab",
"text": "Matrix",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Matrix Grid",
"checked": true,
"items": [
]
}
]
},
{
"Name": "Tab",
"text": "Strategy",
"checked": true,
"items": [
{
"Name": "Grid",
"text": "Strategy Grid",
"checked": true,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Project Tactic Linkage",
"checked": true,
"items": [
]
},
{
"Name": "Output",
"text": "OGTM",
"checked": true,
"items": [
]
}
]
}
]
},
{
"Name": "Tab",
"text": "Risk",
"checked": false,
"items": [
{
"Name": "Grid",
"text": "Risk Grid",
"checked": false,
"items": [
]
},
{
"Name": "Outputs",
"text": "Outputs",
"checked": true,
"items": [
{
"Name": "Output",
"text": "Risk Charts",
"checked": true,
"items": [
]
}
]
}
]
}
]
}
]
Although my data source is correct. What thing is missing in it? I am not getting any error. My tree is not rendering in html.
Try placing your data in a new kendo.data.HierarchicalDataSource
I have this collection: https://graph.facebook.com/2playcz/albums
This collection contains 8 id. How can i get the total count id of this collection using javascript? (Total = 8)
Source:
{
"data": [
{
"id": "201936779932071",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tr\u00e9ninky",
"link": "https://www.facebook.com/album.php?fbid=201936779932071&id=190320081093741&aid=41883",
"cover_photo": "201937046598711",
"count": 8,
"type": "normal",
"created_time": "2012-07-02T09:33:43+0000",
"updated_time": "2012-09-15T12:05:44+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "1788805921",
"name": "Edita Nov\u00e1"
},
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/201936779932071/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "205206429605106",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Turnaje a akce",
"link": "https://www.facebook.com/album.php?fbid=205206429605106&id=190320081093741&aid=42900",
"cover_photo": "205208716271544",
"count": 14,
"type": "normal",
"created_time": "2012-07-10T19:36:53+0000",
"updated_time": "2012-09-15T12:04:05+0000",
"can_upload": false
},
{
"id": "221784994613916",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Tenisova \u0161kolka 2play",
"description": "Tenisov\u00e1 \u0161kolka 2play",
"link": "https://www.facebook.com/album.php?fbid=221784994613916&id=190320081093741&aid=49379",
"cover_photo": "221785024613913",
"count": 9,
"type": "normal",
"created_time": "2012-08-31T11:19:59+0000",
"updated_time": "2012-09-14T15:17:53+0000",
"can_upload": false
},
{
"id": "203405996451816",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Webov\u00e9 fotografie - Logo",
"link": "https://www.facebook.com/album.php?fbid=203405996451816&id=190320081093741&aid=42285",
"cover_photo": "203406586451757",
"count": 11,
"type": "normal",
"created_time": "2012-07-05T10:12:40+0000",
"updated_time": "2012-09-14T15:16:40+0000",
"can_upload": false
},
{
"id": "190332361092513",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Cover Photos",
"link": "https://www.facebook.com/album.php?fbid=190332361092513&id=190320081093741&aid=39232",
"cover_photo": "225939404198475",
"count": 2,
"type": "normal",
"created_time": "2012-06-09T13:52:38+0000",
"updated_time": "2012-09-12T18:15:51+0000",
"can_upload": false
},
{
"id": "190802884378794",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Wall Photos",
"link": "https://www.facebook.com/album.php?fbid=190802884378794&id=190320081093741&aid=39324",
"cover_photo": "190802891045460",
"count": 2,
"type": "wall",
"created_time": "2012-06-10T13:19:48+0000",
"updated_time": "2012-07-17T17:16:19+0000",
"can_upload": false
},
{
"id": "205207126271703",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Ostatn\u00ed",
"link": "https://www.facebook.com/album.php?fbid=205207126271703&id=190320081093741&aid=42902",
"cover_photo": "205209679604781",
"count": 4,
"type": "normal",
"created_time": "2012-07-10T19:40:05+0000",
"updated_time": "2012-07-16T14:47:16+0000",
"can_upload": false,
"likes": {
"data": [
{
"id": "100001449904219",
"name": "Mirka Brani\u0161ov\u00e1"
}
],
"paging": {
"next": "https://graph.facebook.com/205207126271703/likes?limit=25&offset=25&__after_id=100001449904219"
}
}
},
{
"id": "190320914426991",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Profile Pictures",
"link": "https://www.facebook.com/album.php?fbid=190320914426991&id=190320081093741&aid=39224",
"cover_photo": "190327474426335",
"count": 1,
"type": "profile",
"created_time": "2012-06-09T13:29:16+0000",
"updated_time": "2012-06-09T13:43:08+0000",
"can_upload": false
},
{
"id": "190322704426812",
"from": {
"name": "2play.cz - Tenisov\u00e1 \u0161kola",
"category": "Sports league",
"id": "190320081093741"
},
"name": "Logo",
"description": "Loga spole\u010dnosti",
"link": "https://www.facebook.com/album.php?fbid=190322704426812&id=190320081093741&aid=39225",
"type": "normal",
"created_time": "2012-06-09T13:34:09+0000",
"updated_time": "2012-07-05T10:16:58+0000",
"can_upload": false
}
]
}
If you mean the count of items in the data array, it would just be data.length.
First of all, there 9 ids. If you want to count the number of elements in array there's a built-in property length. So that if your object is called x you retrieve the length of data by accessing x.data.length.
On the other hand, if you wanted to count the number of unique ids (IDs should by unique anyway, but if you really really wanted to) you have to manually iterate through the array and count them:
var countIds = function (arr) {
var uniqueIds = {}, num = 0;
if (!arr.data) return false;
arr.data.forEach(function (val, i) {
if (typeof uniqueIds[val.id] === "undefined") {
++num;
uniqueIds[val.id] = true;
}
});
return num;
};