ReactJS filter complex Json - json

[enter image description here][1]Please help me as I am struggling to get something to work. I have a JSON that represents a tree view in my app.
{
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
What I want to do is traverse through my JSON and only return the entries which have the chosen property as true.
I tried many things without success so far, would you guys be able to help me?
https://i.stack.imgur.com/r61MO.jpg
onPush(e){
var chosennodes = filters.filterTreeChosen(this.state.data);
this.setState({selectednodes: chosennodes});
}
Then the filter itself:
[Object.filter = (obj, predicate) =>
Object.keys(obj)
.filter( key => predicate(obj\[key\]) )
.reduce( (res, key) => (res\[key\] = obj\[key\], res), {} );
export const isChosen = (chosen) =>{
return chosen == true;
}
export const filterTreeChosen = (nodes) => {
var filtered = Object.filter(nodes, node => node.chosen == true);
console.log(filtered);
};][1]
https://i.stack.imgur.com/IAXZ7.jpg

Check this out. I think it should apply to your task.
const tree = {
name: 'Global', toggled: true, children: [
{
name: 'Region A', nodeid: 0,chosen: true,pushed: false,
children: [
{ name: 'Town A', nodeid: 1, chosen: true, pushed: false,
children: [{
name : "child 1", pushed: false, chosen:false
}]
},
{ name: 'Town B', nodeid: 2, chosen: false, pushed: false,
children: [{
name : "child 2", pushed: false, chosen: false
}]
}
]
}
]
};
function getChosenNodes (nodes) {
let result = [];
nodes.forEach(node => {
if (node.chosen) {
result = result.concat([node.nodeid]);
}
if (node.children) {
result = result.concat(getChosenNodes(node.children));
}
})
return result;
}
console.log(getChosenNodes([tree]))
I've returned only nodeid but you can change it as you need.

Related

Laravel Yajra Datatable Duplicating Data When page number change

Laravel Yajra DataTable Duplicating Data when changing the page number.
page 01 data response
page 02 data response
Some data will appear on all pages (duplicate) ..for example, id 513.
if ($request->ajax()) {
$data = Lead::with('getSource', 'getPreferredProject', 'tasks')
->where('is_verified', 1)
->whereNull('attended_by')
->whereNull('leads_maker_status')
->where(function ($q) {
$q->where('lead_stage_id','!=',6)->orWhereNull('lead_stage_id');
})
->orderBy('verified_at', 'DESC');
return DataTables::of($data)
->addIndexColumn()
->editColumn('lead_date', function ($data) {
return $data->lead_date != '0000-00-00' ?date('d-m-Y', strtotime($data->lead_date)) : '';
})
->editColumn('verified_at', function ($data) {
return $data->verified_at != '0000-00-00' ? date('d-m-Y', strtotime($data->verified_at)) : '';
})
->rawColumns(['lead_date','verified_at'])
->make(true);
}
Request Function
function getLeadsMakerData() {
$('#leadsMakerTable').DataTable({
processing: true,
serverSide: true,
pageLength: 10,
paging: true,
responsive: true,
scrollX: false,
ajax: {
url: "{{ route('leads-maker.index') }}",
},
columns: [
{
data: 'DT_RowIndex',
name: 'DT_RowIndex',
orderable: false,
searchable: false
},
{
data: 'enquirer_name',
name: 'enquirer_name'
},
{
data: 'lead_date',
name: 'lead_date'
},
{
data: 'verified_at',
name: 'verified_at'
},
{
data: 'mobile',
name: 'mobile'
},
{
data: 'email',
name: 'email'
},
{
data: 'source',
name: 'source'
},
{
data: 'preferred_project',
name: 'preferred_project'
},
{
data: 'action',
name: 'action',
searchable: false,
orderable: false,
},
],
"drawCallback": function (settings) {
$('.dropdown-trigger').dropdown();
},
lengthMenu: [5, 10, 25, 50, 75, 100]
});
}
this is the source code I am using to send the request
and I am calling this method inside $( document ).ready().

Grouping after Mongoose aggregation lookup

Actually im new to mongoDB and mongoose, and im tryig to get nested join using three schemas and grouping them.
const company = Schema(
{
title: {type: String, required: true},
}
);
const plans = Schema(
{
companyId: {type: Schema.Types.ObjectId, ref: 'company', required: true},
title: {type: String, required: true},
}
);
const promotions = Schema(
{
planId: {type: Schema.Types.ObjectId, ref: 'plans', required: true},
title: {type: String, required: true},
}
);
I got the below result but separated, and I would like to group it, any help with this point would be appreciated?
[
{
_id: '621c2749ac447abf20a8a263',
title: 'test 1',
plans: {
_id: '621c290ad6bce1084f900b0b',
title: 'test 1',
promotions: {
_id: '621d1187b18de3c35fa3963b',
title: 'test 1',
},
},
},
{
_id: '621c2749ac447abf20a8a263',
title: 'test 2',
plans: {
_id: '621c290ad6bce1084f900b0b',
title: 'test 2',
promotions: {
_id: '621d1187b18de3c35fa3963d',
title: 'test 2',
},
},
},
];
The result that i want to achieve is:
[
{
title: 'company name',
plans: [
{
title:'plan name',
promotions: [
{
title:'promotion name'
}
]
},
...
]
},
...
]
A nested "$lookup" is one way to do it.
db.company.aggregate([
{
// lookup plans matching companies
"$lookup": {
"from": "plans",
"localField": "_id",
"foreignField": "companyId",
"pipeline": [
{
// lookup promotions matching plans
"$lookup": {
"from": "promotions",
"localField": "_id",
"foreignField": "planId",
"as": "promotions"
}
}
],
"as": "plans"
}
},
{
// drop unwanted fields
"$project": {
"_id": 0,
"plans._id": 0,
"plans.companyId": 0,
"plans.promotions._id": 0,
"plans.promotions.planId": 0
}
}
])
Try it on mongoplayground.net.

how to display json in table vuejs

how to display the following json data ?
i have json data like this, and want to display it in table, i use vue-bostrapt .
Previously I tried like this, but it's not perfect.
this my json
[
{
"id":"1",
"name": "KINTIL",
"desc": "Kintil is good",
"location": [
{
"prov": "Jawa Barat",
"city": "Bandung"
},
{
"prov": "Banten",
"city": "Tanggerang"
}
],
"applied": [
{
"item_name": "galian"
},
{
"item_name": "timbunan"
}
],
"exception": [
{
"ex_name": "F001",
"ex_value": "001001"
},
{
"ex_name": "M001",
"ex_value": "002002"
}
]
}
]
and this html
<b-table class="table spacing-table" style="font-size: 13px;" show-empty
:items="inovasi" :fields="fields" :current-page="currentPage" :per-page="0" :filter="filter" >
</b-table>
and this my script
import json from "../static/data.json";
export default {
name: 'tes',
data() {
return {
inovasi:[],
filter: null,
fields: [
{
key: 'id',
label: 'id',
sortable: true
},
{
key: 'name',
label: 'name',
sortable: true
},
{
key: 'location',
label: 'location',
sortable: true
},
{
key: 'applied',
label: 'applied',
sortable: true
},
{ key: 'actions', label: 'Doc' }
],
currentPage: 0,
perPage: 5,
totalItems: 0
}
},
mounted() {
this.inovasi = json;
},
computed:{
},
methods: {
}
}
this result
how to display location and applied , into a single row table ?
thanks in advance for those who have answered :)
thanks
You can do it using formatter like
fields: [
{
key: 'id',
label: 'id',
sortable: true
},
{
key: 'name',
label: 'name',
sortable: true
},
{
key: 'location',
label: 'location',
sortable: true,
formatter: (value, key, item) => {
return value.map(x => 'prov: ' + x.prov + ' city:' + x.city).join(", ")
}
},
{
key: 'applied',
label: 'applied',
sortable: true,
formatter: (value, key, item) => {
return value.map(x => x.item_name).join(", ")
}
},
{ key: 'actions', label: 'Doc' }
],
It will show for the location column this: prov: Jawa Barat city:Bandung, prov: Banten city:Tanggerang and for the applied column this: galian, timbunan

forEach Return for of Loop Equivalent

Just needing to understand better use of new for loops in ES5/ES6.
I've read somewhere about the code above to modify the items array of objects.
var selected = [1, 2, 4, 6],
items = [
{ id: 1, selected: false },
{ id: 2, selected: false },
{ id: 3, selected: false },
{ id: 4, selected: false },
{ id: 5, selected: true },
{ id: 6, selected: false }
]
set = new Set(selected)
items.forEach(a => a.selected = set.has(a.id))
My question is, what is the for of loop equivalent of the forEach statement above?
for(let [index, value] of items.entries()) {
return value.selected = set.has(value.id)
}
What you have posted is very close-- the only problem I see is that you attempt to return, which will exit the entire function (and thus loop) prematurely. This is because the loop isn't calling any new function on each iteration, so the return leaves the function in which the for begins iterating. If you remove the return from your initial attempt, it functions correctly and as expected.
var selected = [1, 2, 4, 6],
items = [
{ id: 1, selected: false },
{ id: 2, selected: false },
{ id: 3, selected: false },
{ id: 4, selected: false },
{ id: 5, selected: true },
{ id: 6, selected: false }
]
set = new Set(selected)
// items.forEach(a => a.selected = set.has(a.id))
for(let [index, value] of items.entries()) {
value.selected = set.has(value.id)
}
console.log(items);

Extract child objects from json object

I am struggling to convert xml to json object and then extracting nodes from the converted object. I am using busyboy to read from the file uploaded on the server. And after that I am using inspect to convert xml to json and then printing the json object. The final output seems as
{ declaration: { attributes: { version: '1.0', encoding: 'utf-8' } },
root:
{ name: 'order',
attributes:
{ orderid: '123456',
xmlns: 'http://www.someRandomNameSpace.com' },
children:
[ { name: 'orderperson',
attributes: {},
children: [],
content: 'str1234' },
{ name: 'shipto',
attributes: {},
children:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' },
I want to read the 'name'='Adnan Ali' from this object how will that be done in nodejs ? I mean how can i reach to the object which has name='name' and content='Adnan Ali'.
The print command is
console.log(inspect(order, {colors: true, depth: Infinity}));
Since you are using NodeJS perhaps giving JSONPath a try would be a good idea. Then you can do something like this:
var jp = require("JSONPath");
var tobj = { "declaration": { "attributes": { "version": '1.0', "encoding": 'utf-8' } },
"root":
{ "name": 'order',
"attributes":
{ "orderid": '123456',
"xmlns": 'http://www.someRandomNameSpace.com' },
"children":
[ { "name": 'orderperson',
"attributes": {},
"children": [],
"content": 'str1234' },
{ "name": 'shipto',
"attributes": {},
"children":
[ { "name": 'name',
"attributes": {},
"children": [],
"content": 'Adnan Ali'
}
]
}
]
}};
var result = jp.eval(tobj, "$..children[?(#.name === 'name' && #.content === 'Adnan Ali')]");
console.log(result);
Example output:
[ { name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali' } ]
(Don't forget to install JSONPath ;-))
Sources:
https://www.npmjs.com/package/JSONPath
http://goessner.net/articles/JsonPath/
You need to search the arrays of objects for the objects you are interested in. There are various ways to do that, including Array.prototype.find (not sure if it is available in all Node.js versions) and lodash _.find.
Using Array.prototype.filter a solution could look like this (not tested):
function findObject(array, key, value) {
var filtered = array.filter(obj => (obj[key] === value));
if (filtered.length !== 1) throw new Error('Found ' + filtered.length + ' objects with `' + key + '`=`' + value + '`, expected to find 1.');
return filtered[0];
}
var shipto = findObject(input.root.children, 'name', 'shipto');
var name = findObject(shipto.children, 'name', 'name').content;
console.log(name);
You should be able to reach the object with content: 'Adnan Ali' with this path data.root.children[1].children[0]:
const data = {
declaration: {
attributes: {
version: '1.0',
encoding: 'utf-8'
}
},
root: {
name: 'order',
attributes: {
orderid: '123456',
xmlns: 'http://www.someRandomNameSpace.com'
},
children: [{
name: 'orderperson',
attributes: {},
children: [],
content: 'str1234'
}, {
name: 'shipto',
attributes: {},
children: [{
name: 'name',
attributes: {},
children: [],
content: 'Adnan Ali'
}]
}]
}
};
console.log(data.root.children[1].children[0])
Explanation:
data is an object that contains a root object. root is an object that contains a children array. The second element in root.children (index 1) is an object that contains another children array that contains the object you're looking for at the first index (0).
Consider using object-scan. It's very powerful once you wrap your head around it.
// const objectScan = require('object-scan');
const find = (input) => objectScan(['**'], {
abort: true,
rtn: 'value',
filterFn: ({ value }) => value.content === 'Adnan Ali' && value.name === 'name'
})(input);
const tobj = { declaration: { attributes: { version: '1.0', encoding: 'utf-8' } }, root: { name: 'order', attributes: { orderid: '123456', xmlns: 'http://www.someRandomNameSpace.com' }, children: [{ name: 'orderperson', attributes: {}, children: [], content: 'str1234' }, { name: 'shipto', attributes: {}, children: [{ name: 'name', attributes: {}, children: [], content: 'Adnan Ali' }] }] } };
console.log(find(tobj));
// => { name: 'name', attributes: {}, children: [], content: 'Adnan Ali' }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.8.0"></script>
Disclaimer: I'm the author of object-scan