make a json format with angularjs - javascript

Hi i want to use a treeView in my angularjs, the data recieved from the server is:
[
{
"vehiculeid": 1,
"name": "ggg",
"id": 1,
"group": "TGV"
},
{
"vehiculeid": 5,
"name": "eee",
"id": 5,
"group": "TGV"
},
{
"vehiculeid": 6,
"name": "tru123",
"id": 8,
"group": "TGV"
},
{
"vehiculeid": 2,
"name": "aqs",
"id": 3,
"group": "TCF"
}
]
How can i make the data like the folowing data so that i can use it in treeView Component, this is that format which i want to got:
treedata_avm = [{
group: 'TGV',
children: [{
name: 'ggg',
data: {
vehiculeid: 1
}
}, {
name: 'eee',
data: {
vehiculeid: 5
}
}, {
name: 'tru123',
data: {
vehiculeid: 6
}
}]
},{
group: 'TCF',
children: [{
name: 'aqs',
data: {
vehiculeid: 2
}
}]
}]
How i can do that with javascript or angularjs to get this format?
P.S:
the data recieved from the server is dynamic.

Here's the code:
var test = [
{
"vehiculeid": 1,
"name": "ggg",
"id": 1,
"group": "TGV"
},
{
"vehiculeid": 5,
"name": "eee",
"id": 5,
"group": "TGV"
},
{
"vehiculeid": 6,
"name": "tru123",
"id": 8,
"group": "TGV"
},
{
"vehiculeid": 2,
"name": "aqs",
"id": 3,
"group": "TCF"
}
];
var test2 = {};
for(var i in test) {
if(typeof test2[test[i]['group']] === 'undefined') {
test2[test[i]['group']] = [];
}
test2[test[i]['group']].push({name: test[i]['name'], data: {vehiculeid: test[i]['vehiculeid']}})
}
var treedata_avm = [];
for(var j in test2) {
var final = {};
final['group'] = j;
final['children'] = test2[j];
treedata_avm.push(final)
}
console.log(treedata_avm)

Related

how to extract and mapping object from array(object) with redundant key inside key

i have following example array(object):
[
{
"id": 1,
"name": "selling",
"detail": [
{
"id": 11,
"name": "sale-report",
"detail": [
{ "id": 111, "name": "sale-report1", "detail": [] },
{ "id": 112, "name": "sale-report2", "detail": [] }
]
}
]
},
{
"id": 2,
"name": "webstore",
"detail": [
{
"id": 11,
"name": "sale-report",
"detail": [
{ "id": 111, "name": "webstore-report1", "detail": [] },
{ "id": 112, "name": "webstore-report2", "detail": [] }
]
}
]
},
{
"id": 2,
"name": "setting",
"detail": [
{
"id": 11,
"name": "general",
"detail": [
{ "id": 111, "name": "setting-general1", "detail": [] },
{ "id": 112, "name": "setting-general2", "detail": [] }
]
}
]
}
]
how to change the array with new format like this
[
{
"id": 1,
"name": "selling",
},
{
"id": 11,
"name": "sale-report"
},
{ "id": 111, "name": "sale-report1" },
{ "id": 112, "name": "sale-report2" },
{
"id": 2,
"name": "webstore",
},
{
"id": 11,
"name": "sale-report",
},
{ "id": 111, "name": "webstore-report1" },
{ "id": 112, "name": "webstore-report2" },
{
"id": 2,
"name": "setting",
},
{
"id": 11,
"name": "general",
},
{ "id": 111, "name": "setting-general1" },
{ "id": 112, "name": "setting-general2" }
]
with the condition that if there is a key "detail" inside object in the branch, it will be mapped as well (assuming unlimited key "detail" inside object inside array)
note: content of detail will be same as parent, but different value
thanks in advance
i tried mapping mannualy with foreach, but i cant figure out if detail key with array(object) has unlimited nesting
Simply, use recursion to do this.
Here is the working demo-
let input = [
{
id: 1,
name: "selling",
detail: [
{
id: 11,
name: "sale-report",
detail: [
{ id: 111, name: "sale-report1", detail: [] },
{ id: 112, name: "sale-report2", detail: [] }
]
}
]
},
{
id: 2,
name: "webstore",
detail: [
{
id: 11,
name: "sale-report",
detail: [
{ id: 111, name: "webstore-report1", detail: [] },
{ id: 112, name: "webstore-report2", detail: [] }
]
}
]
},
{
id: 2,
name: "setting",
detail: [
{
id: 11,
name: "general",
detail: [
{ id: 111, name: "setting-general1", detail: [] },
{ id: 112, name: "setting-general2", detail: [] }
]
}
]
}
];
let result = [];
function parseData(arr) {
arr.forEach((item) => {
result.push({
id: item.id || null,
name: item.name || null
});
if (item.detail && item.detail.length) {
parseData(item.detail);
}
});
return result;
}
let output = parseData(input);
console.log(output);
You could destructure the objects and take detail out of the object and map the rest object and the results of nested details array as flat array with a recursive function.
const
flat = (array = []) => array.flatMap(({ detail, ...rest }) => [
rest,
...flat(detail)
]),
data = [{ id: 1, name: "selling", detail: [{ id: 11, name: "sale-report", detail: [{ id: 111, name: "sale-report1", detail: [] }, { id: 112, name: "sale-report2", detail: [] }] }] }, { id: 2, name: "webstore", detail: [{ id: 11, name: "sale-report", detail: [{ id: 111, name: "webstore-report1", detail: [] }, { id: 112, name: "webstore-report2", detail: [] }] }] }, { id: 2, name: "setting", detail: [{ id: 11, name: "general", detail: [{ id: 111, name: "setting-general1", detail: [] }, { id: 112, name: "setting-general2", detail: [] }] }] }],
result = flat(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
A non recursive method of doing this could look something like the following:
const data = [{"id":1,"name":"selling","detail":[{"id":11,"name":"sale-report","detail":[{"id":111,"name":"sale-report1","detail":[]},{"id":112,"name":"sale-report2","detail":[]}]}]},{"id":2,"name":"webstore","detail":[{"id":11,"name":"sale-report","detail":[{"id":111,"name":"webstore-report1","detail":[]},{"id":112,"name":"webstore-report2","detail":[]}]}]},{"id":2,"name":"setting","detail":[{"id":11,"name":"general","detail":[{"id":111,"name":"setting-general1","detail":[]},{"id":112,"name":"setting-general2","detail":[]}]}]}];
const extract = (data) => {
const result = [];
const queue = [...data];
while(queue.length > 0) {
const { detail, ...rest } = queue.shift();
result.push(rest);
queue.unshift(...detail);
}
return result;
};
console.log(extract(data));

Filter complex array in react native?

I have array .
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
I tried following the code to filter it. and no output show
arr.map((item,idx) => (
console.log(item.data.games.gamename)
)
))
I want to print all game name eg.
cricket
football
videogames
volleyball
We can use flatMap() to do it
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}]
// multiple flatMap chain invocation seems ugly,waiting for more elegant solution
let result = arr.flatMap(a => a.data).flatMap(a => a.games).flatMap(a => a.gamename)
console.log(result)
Data is a array and so is games:
const arr = [
{
status: "success",
data: [
{
name: "user1",
games: [
{
id: 1,
gamename: "cricket",
},
{
id: 2,
gamename: "football",
},
],
},
{
name: "user1",
games: [
{
id: 1,
gamename: "videogames",
},
{
id: 2,
gamename: "volleyball",
},
],
},
],
},
];
arr.map((item) => {
item.data.map((item) => {
item.games.map((item) => {
console.log(item.gamename);
});
});
});
Try out this code, it will return only game names, you can change the join if don't need comma (,)
Output :
"cricket,football,videogames,volleyball"
const arr = [{
"status": "success",
"data": [{
"name": "user1",
"games": [{
"id": 1,
"gamename": "cricket"
}, {
"id": 2,
"gamename": "football"
}]
},
{
"name": "user1",
"games": [{
"id": 1,
"gamename": "videogames"
}, {
"id": 2,
"gamename": "volleyball"
}]
}
]
}];
console.log(JSON.stringify(arr.filter(e=>e.status=="success").map(e=>e.data.map(f=>f.games.map(g=>g.gamename)).join(",")).join(",")));

Nested Array Filter/Mapping

Probably a basic question, but I've been blocked for a day now on this.
I am trying to get the correct map/filter from the following array:
[{
"id": 1,
"description": "Electric",
"subUtilityTypes": [{
"id": 5,
"description": "Grid"
},
{
"id": 6,
"description": "Solar"
}
]
},
{
"id": 2,
"description": "Gas",
"subUtilityTypes": [{
"id": 7,
"description": "Heating Oil"
},
{
"id": 8,
"description": "Natural Gas"
},
{
"id": 11,
"description": "Propane"
}
]
}
]
I want to get the id and description inside all subUtilityTypes.
This is what I've been trying:
this.options = arr1.map((parent) => {
return {
id: parent.id,
name: parent.subUtilityTypes.flatMap((child) => {
return child.description;
})
};
});
My problem with what I am trying is that instead of creating separate objects I am getting parent id and list of child names like this:
[{
"id": 1,
"name": [
"Grid",
"Solar"
]
},
{
"id": 2,
"name": [
"Heating Oil",
"Natural Gas",
"Propane"
]
}
]
Expected result should look like this:
[{
"id": 5,
"name": [
"Grid"
]
},
{
"id": 6,
"name": [
"Solar"
]
},
{
"id": 7,
"name": [
"Heating Oil"
]
}
]
Firstly use flatMap to get subUtilityTypes, then map entries:
const input = [{id:1,description:"Electric",subUtilityTypes:[{id:5,description:"Grid"},{id:6,description:"Solar"}]},{id:2,description:"Gas",subUtilityTypes:[{id:7,description:"Heating Oil"},{id:8,description:"Natural Gas"},{id:11,description:"Propane"}]}];
const res = input.flatMap(e => e.subUtilityTypes)
.map(e => ({ id: e.id, name: [e.description] }))
console.log(res)
.as-console-wrapper { max-height: 100% !important; top: 0; } /* ignore this */
Map only arr1 by returning only subUtilityTypes and then map it to get the desired result :
arr1.flatMap(item=>item.subUtilityTypes)
.map(item=>({id:item.id,name:[item.description]}))
let arr1=[
{
"id": 1,
"description": "Electric",
"subUtilityTypes": [
{
"id": 5,
"description": "Grid"
},
{
"id": 6,
"description": "Solar"
}
]
},
{
"id": 2,
"description": "Gas",
"subUtilityTypes": [
{
"id": 7,
"description": "Heating Oil"
},
{
"id": 8,
"description": "Natural Gas"
},
{
"id": 11,
"description": "Propane"
}
]
}
]
let options=arr1.flatMap(item=>item.subUtilityTypes).map(item=>({id:item.id,name:[item.description]}))
console.log(options)
You can also use reduce to achieve the same result.
arr.reduce((acc, curr) => [...acc,...curr.subUtilityTypes.map((o) => ({ id: o.id, name: [o.description] })),],[])
const arr = [
{
id: 1,
description: "Electric",
subUtilityTypes: [
{
id: 5,
description: "Grid",
},
{
id: 6,
description: "Solar",
},
],
},
{
id: 2,
description: "Gas",
subUtilityTypes: [
{
id: 7,
description: "Heating Oil",
},
{
id: 8,
description: "Natural Gas",
},
{
id: 11,
description: "Propane",
},
],
},
];
const result = arr.reduce((acc, curr) => [...acc,...curr.subUtilityTypes.map((o) => ({ id: o.id, name: [o.description] })),],[]);
console.log(result);

Group elements of array by sub string JavaScript

I have a list of elements like this
[{
"id": 1,
"id_team": "1.",
"name": "Name",
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
}, {
"id": 5,
"id_team": "5.",
"name": "Name",
}, {
"id": 6,
"id_team": "2.1.",
"name": "Name",
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
}, {
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
}]
a shown in the example, i have some string in the column id_team that are inside other string in the same column in the array
For example, the main team would be 1. then 1.1. and 1.2. and so on would be part of 1.
so the result must be like this
[{
"id": 1,
"id_team": "1.",
"name": "Name",
"subteams": [{
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
},],
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
"subitems": [{
"id": 6,
"id_team": "2.1.",
"name": "Name",
},]
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
"subitems": [],
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
"subitems": [],
}, {
"id": 5,
"id_team": "5.",
"name": "Name",
"subitems": [],
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
"subitems": [],
},]
it is doable? i use lodash if it would make it easier.
EDIT
I have something like this
teams.filter(item => item.id_team.length <= size).map((item) => {
const subteams = teams.filter((team) =>
team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);
console.log(subteams);
return {
...item,
subteams,
};
}) || [];
this kinda work, as it will insert in the sub teams array, but the problem is that this must work for many levels, take this example
[{
"id": 1,
"id_team": "1.",
"name": "Team Name",
"subteams": [{
"id": 8,
"id_team": "1.1.",
"name": "Team Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Team Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Team Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Team Name",
}, {
"id": 12,
"id_team": "1.1.1",
"name": "Team Name",
}]
}, {
"id": 2,
"id_team": "2.",
"name": "Team Name",
"subteams": [{
"id": 6,
"id_team": "2.1.",
"name": "Team Name",
}]
}, {
"id": 3,
"id_team": "3.",
"name": "Team Name",
"subteams": []
}, {
"id": 4,
"id_team": "4.",
"name": "Team Name",
"subteams": []
}, {
"id": 5,
"id_team": "5.",
"name": "Team Name",
"subteams": []
}, {
"id": 7,
"id_team": "6.",
"name": "Team Name",
"subteams": []
}]
in this example there is a team with id_team = 1.1.1., how can i add that team to a sub team of the team with id_team 1.1. with the same logic, i tried to use the same code recursively but it didnt work.
const items = data.reduce((acc, item, index) => {
const id_team = item.id_team.split('.')[0] + '.';
const current = acc.find(record => record.id_team === id_team);
if(current) current.subteams.push(item);
else acc.push({
id: index + 1,
id_team,
subteams: [item]
});
return acc;
}, []);
let data = [{
"id": 1,
"id_team": "1.",
"name": "Name",
}, {
"id": 2,
"id_team": "2.",
"name": "Name",
}, {
"id": 3,
"id_team": "3.",
"name": "Name",
}, {
"id": 4,
"id_team": "4.",
"name": "Name",
}, {
"id": 5,
"id_team": "1.2.5.",
"name": "Name",
}, {
"id": 6,
"id_team": "2.1.",
"name": "Name",
}, {
"id": 7,
"id_team": "6.",
"name": "Name",
}, {
"id": 8,
"id_team": "1.1.",
"name": "Name",
}, {
"id": 9,
"id_team": "1.2.",
"name": "Name",
}, {
"id": 10,
"id_team": "1.3.",
"name": "Name",
}, {
"id": 11,
"id_team": "1.4.",
"name": "Name",
}];
data = data.map(node=>{
let t = node.id_team.split('.').filter(n=>n);
let parent = t.splice(0,t.length-1).join('.');
return {
...node,
parent: !!parent ? parent+'.' : null
}
});
let tree = (items, id = null) =>
items
.filter(item => item.parent === id)
.map(item => ({ ...item, subTeams: tree(items, item.id_team) }));
console.log(JSON.stringify(tree(data),null,2));
From the code of the example, i just checked if the subteams array has data, if it does, i use an auxiliar with the recursive function and assigned it to the subteams.
getSubTeams(teams, size) {
const filtered = teams.filter(item => item.id_team.length <= size);
return filtered.map((item) => {
let subteams = teams.filter((team) => team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);
if (subteams.length > 0) {
let aux = this.getSubTeams(subteams, size + 2);
subteams = [
...aux,
];
}
return {
...item,
subteams,
};
}) || [];
},

Remove elements inside nested JSON based on condition

Below is my JSON object in which I want to remove the groupType from the group that dosen't have trips.ie.,trips.length===0.And also want to remove the entire client object if all the trips array is empty for that particular client object..
let fruitsArray= [
{
"fruit": {
"id": 1,
"name": "Mango"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": []
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 1,
"name": "Apple"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
}
]
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 95,
"name": "Banana"
},
"group": [
{
"groupType": {
"id": 4,
"name": "A1"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 63,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 65,
"deliverySlotId": 900000000000001,
"orderId": 22
}
}
]
}
]
}
]
The code that I have tried returns all the elements with 0 groupType..
let finalArray = fruitsArray.map((group) => {
return group.group.filter((trip) => {
return trip.trips.length > 0;
})
})
You can use map combined with filter.
Alternatively you can also use the reduce method.
I wrote both examples beneath.
let fruitsArray= [
{
"fruit": {
"id": 1,
"name": "Mango"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": []
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 1,
"name": "Apple"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
}
]
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 95,
"name": "Banana"
},
"group": [
{
"groupType": {
"id": 4,
"name": "A1"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 63,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 65,
"deliverySlotId": 900000000000001,
"orderId": 22
}
}
]
}
]
}
];
var filteredFruits = fruitsArray.map((fruit) => {
fruit.group = fruit.group.filter((group) => group.trips.length);
return fruit;
}).filter((fruit) => fruit.group.length);
console.log(filteredFruits);
// alternative example with reduce
var reducedFruits = fruitsArray.reduce((prev, next) => {
next.group = next.group.filter((group) => group.trips.length);
return next.group.length ? prev.concat([next]) : prev;
}, []);
console.log(reducedFruits);
var fruitsArray= [
{
"fruit": {
"id": 1,
"name": "Mango"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": []
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 1,
"name": "Apple"
},
"group": [
{
"groupType": {
"id": 1,
"name": "A"
},
"trips": []
},
{
"groupType": {
"id": 2,
"name": "B"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
}
]
},
{
"groupType": {
"id": 3,
"name": "C"
},
"trips": []
},
{
"groupType": {
"id": 4,
"name": "D"
},
"trips": []
}
]
},
{
"fruit": {
"id": 95,
"name": "Banana"
},
"group": [
{
"groupType": {
"id": 4,
"name": "A1"
},
"trips": [
{
"trip": {
"id": 62,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 63,
"deliverySlotId": 900000000000001
}
},
{
"trip": {
"id": 65,
"deliverySlotId": 900000000000001,
"orderId": 22
}
}
]
}
]
}
]
for(var i=0;i<fruitsArray.length;i++){
var currentFruit = fruitsArray[i];
var currentFruitGroups = currentFruit.group;
var foundTrip =false;
for(var j=0;j<currentFruitGroups.length;j++){
var currentTrips = currentFruitGroups[j].trips;
if(!currentTrips.length){
currentFruitGroups.splice(j,1);
j--;
}else{
foundTrip = true;
}
}
if(!foundTrip){
fruitsArray.splice(i,1);
i--;
}
}
console.log(fruitsArray);
https://jsfiddle.net/485ue5zs/1/
let finalArray = fruitsArray.reduce((p, n) => {
const gt = n.group.filter(g => g.trips.length);
return gt.length ? p.concat({fruit: n.fruit, group: gt}) : p;
}, []);

Categories