I am having json objects with parent and child combination. I need to filter only the keys which matches the condition in child key/value pair.
response json is below,
I tried by using lodash filter and pickup and some more thing and result is only getting the child objects. But i need the output with including parent objects as well
"custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
"pineapple": {
"path": "www.pineapple.com",
"version": "4.4"
},
"jackfruit": {
"path": "www.jackfruit.com",
"version": "4.3"
}
Here, i need to filter only the objects which has version=4.5 and version="".
i need to filter the above json like below,
"custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
Please let me know how to make this possible.
Your question is ambiguity. According to your input/output sample, I guess code below is what you need:
let data = { "custom": {
"url": "",
"version": ""
},
"apple": {
"path": "www.testing.com",
"version": "4.5"
},
"mango": {
"path": "www.mango.com",
"version": "4.5"
},
"pineapple": {
"path": "www.pineapple.com",
"version": "4.4"
},
"jackfruit": {
"path": "www.jackfruit.com",
"version": "4.3"
}}
let result = _.pickBy(data,(v,k)=>v.version === "4.5" || v.version ==="" )
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Related
Strapi v4, postgres.
I have two collections Sprites and Tags and I want to link those collections throught REST API when I creating a new Sprite entry. I have a simple API endpoint which is firing strapi.controller('api::sprite.sprite').create(), but I cant understand how can I specify relation to Tags.
I tried many various variants but nothing not worked for me.
For example:
{
"data": {
"tags": [1, 2],
}
}
As I understand right I need to specify Tag's ID, as said at documentation but its not worked for me.
If I create a new entry from Content Manager and specify relation manually its works good.
Also I tried to open all permissions to Tag collection
Sprites:
{
"kind": "collectionType",
"collectionName": "sprites",
"info": {
"singularName": "sprite",
"pluralName": "sprites",
"displayName": "Sprite",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
...
"tags": {
"type": "relation",
"relation": "manyToMany",
"target": "api::tag.tag",
"inversedBy": "sprites"
}
}
}
And Tags:
{
"kind": "collectionType",
"collectionName": "tags",
"info": {
"singularName": "tag",
"pluralName": "tags",
"displayName": "tag"
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"name": {
"type": "string"
},
"sprites": {
"type": "relation",
"relation": "manyToMany",
"target": "api::sprite.sprite",
"inversedBy": "tags"
}
}
}
I am new to Front-end dev, and I want to display below REST API response using Vue.js
Where the domains names will be a DDL, once u select one of them it will show u the list of subdomain in it with their version.
[
{
"subdomains": [
{
"name": "subdomain-1",
"version": "0.4.12"
},
{
"name": "subdomain-2",
"version": "0.4.25"
}
],
"name": "RB"
},
{
"subdomains": [
{
"name": "subdomain-3",
"version": "0.4.2"
},
{
"name": "subdomain-4",
"version": "0.4.5"
}
],
"name": "OB"
}
]
So I have the following object from my controller, which has a name, a list of beans and a list of operations:
{
"name": "Charge",
"beans": [
],
"operations": [
{
"name": "getSize",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
]
},
{
"name": "truncate",
"returnType": "java.lang.Void",
"description": "empty description",
"parameters": [
]
},
{
"name": "count",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "update",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaSelectCode",
"type": "java.lang.String",
"value": null
},
{
"name": "javaUpdateCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "delete",
"returnType": "java.lang.Integer",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "dump",
"returnType": "java.lang.Void",
"description": "empty description",
"parameters": [
{
"name": "javaSelectCode",
"type": "java.lang.String",
"value": null
},
{
"name": "destinationPath",
"type": "java.lang.String",
"value": null
}
]
},
{
"name": "select",
"returnType": "java.lang.String",
"description": "empty description",
"parameters": [
{
"name": "javaCode",
"type": "java.lang.String",
"value": null
}
]
}
],
"$$hashKey": "object:620"
}
Basically I want to display all the operations from this object in a dropdown menu.
So I was thinking of having something like:
<div ng-repeat="operation in object.operations">
{{operation.name}}
</div>
Except the code above doesn't display anything on the screen, no errors in the console, nothing.
Any help would be much appreciated!
EDIT:
Javascript service:
app.controller('selectAll', ['$http', '$scope' , '$rootScope', function ($http, $scope, $rootScope) {
$scope.response;
$scope.operations;
$rootScope.$on("invokeSelectAll", function(){
$scope.invokeSelectAll();
});
$scope.invokeSelectAll = function(){
$scope.response = $http.post('/invoke/selectAll/', $rootScope.dataObj);
$scope.object = JSON.stringify($rootScope.object);
console.log(" object operation from selectAll " + $scope.object);
$scope.response.then(function(data) {
$scope.responses = data.data ? data.data : "Select Operation not supported on this bean";
});
}
}]);
Screenshot of dev console:
https://imgur.com/a/8WAAL
Use JSON.stringify() to create a JSON string from a JavaScript object.
Use JSON.parse() to parse a JSON string to a JavaScript object.
In your case, you need to use JSON.parse() because you get a JSON string from the server and want to parse it to a JavaScript object.
$scope.object = JSON.parse($rootScope.object);
you are using JSON.stringify which is used to change javascript object to string and store it as a string only.
You should Parse the data with JSON.parse(), and the data becomes a JavaScript object. and you can easily use that in ng-repeat.
Try it ,It will work fine
i got the following transform template:
var transform = {'<>':'li','html':'${name} - ${version} - ${licenseSources.package.sources[0].license}'};
now i want to transform a JSON into a HTML with json2html:
var html = json2html.transform(data,transform);
JSON looks like this:
{
"id": "xxx",
"name": "xxx",
"version": "0.0.14-SNAPSHOT",
"repository": "xxx",
"directory": "./",
"type": "(none)",
"licenseSources": {
"package": {
"sources": [{
"license": "BSD",
"url": "(none)"
}
]
},
"license": {
"sources": [{
"filePath": "xxx",
"text": "xxx"
}, {
"filePath": "xxx",
"text": "xxx"
}
]
},
"readme": {
"sources": [{
"filePath": "xxx",
"text": "xxx"
}
]
}
}
}
I want to access licenseSouce.package.sources.license = "bsd" over the transform variable. But i can't figure out the notation.
Can someone help?
Thx in advance
I figured it out:
${licenseSources.package.sources.0.license}
I am a newbie to NodeJS and Sails.js.
I want create a REST API that allows me to expand a resource based on query parameter. For eg
HTTP GET /snippets
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "htep://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": "http://localhost:8000/users/2/",
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
HTTP GET /snippets?expand=owner
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://localhost:8000/snippets/1/",
"highlight": "http://localhost:8000/snippets/1/highlight/",
"title": "test",
"code": "def test():\r\n pass",
"linenos": false,
"language": "Clipper",
"style": "autumn",
"owner": {
"url": "http://localhost:8000/users/2/",
"username": "test",
"email": "test#test.com"
},
"extra": "http://localhost:8000/snippetextras/1/"
}
]}
Wondering how can I do that in Sails.js or NodeJS?
You should use assocations.
Here is how you would create a one-to-many association between your User model and your Snippet model:
// User.js
module.exports = {
// ...
attributes: {
// ...
snippets: {
collection: 'Snippet',
via: 'owner'
}
}
};
// Snippet.js
module.exports = {
// ...
attributes: {
// ...
owner: {
model: 'User'
}
}
};
Then you can hit /snippets if you want a list of snippets, and hit /snippets?populate=[owner] if you need details about the owners of the snippets.