Remove an object from an array - javascript

What is the way to remove an property named "itemType"from the below given object
?
{
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"itemType": {
"id": 16,
"name": "PC 350",
"description": "PC 350"
},
"typeid": 12
}
So that the final array should look like
{
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"typeid": 12
}

This is object not array. You can use delete like this
var obj = {
"id": 19,
"cost": 10,
"items": 10,
"numbers": 10,
"status": false,
"hours": 10,
"itemType": {
"id": 16,
"name": "PC 350",
"description": "PC 350"
},
"typeid": 12
}
delete obj.itemType;

Whether it is an object and you want to delete a property from it, or you have an array and want to delete a value, the way to delete is -
delete obj.itemType //object.
delete array[3] //delete 4th item from the array.
Note - the structure you have provided is not an array, its an object.

Related

Vuejs How to assign array object?

I have a response like below from an API call,
{
"1-2021": [
{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [
{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
}
Then, I have to repeat the whole arrays inside the arrays. How do I do that in VueJS?
I have searched for using forEach.. nowhere I found forEach usage.
Can anyone help me on how to fetch the values from that arrays by using either forEach or any else(VueJS)?
I expected the outcome:
chartData: [
['Month', 'New', 'Verified', 'Regitered ID', 'On Playing', 'Finished', 'Active', 'Inactive'],
['January', 7, 4, 18, 15, 9, 10, 0],
['February', 16, 22, 23, 30, 16, 9, 8]
]
Thanks & Regards,
Try this
let a = {
"1-2021": [{
"id": 1,
"status": "New",
"player_count": 7
},
{
"id": 2,
"status": "Verified",
"player_count": 4
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 18
},
{
"id": 4,
"status": "On Playing",
"player_count": 15
},
{
"id": 5,
"status": "Finished",
"player_count": 9
},
{
"id": 6,
"status": "Active",
"player_count": 10
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
],
"2-2021": [{
"id": 1,
"status": "New",
"player_count": 3
},
{
"id": 2,
"status": "Verified",
"player_count": 8
},
{
"id": 3,
"status": "Regitered ID",
"player_count": 17
},
{
"id": 4,
"status": "On Playing",
"player_count": 11
},
{
"id": 5,
"status": "Finished",
"player_count": 7
},
{
"id": 6,
"status": "Active",
"player_count": 6
},
{
"id": 7,
"status": "Inactive",
"player_count": 0
}
]
};
let ar = [];
let b = Object.keys(a).forEach((e, index) => {
if (index == 0) {
let b = a[e].map(r => r.status)
b.unshift("Month")
ar.push(b)
}
let a1 = [e]
a[e].forEach(c => {
a1.push(c.player_count)
c.status
})
ar.push(a1)
})
console.log(ar)

force directed graph filter nodes and links

I am currently doing data visualization using force-directed graph in D3.js. I have a use case in which I have to filter the nodes and links(by filtering I mean should not be displayed in the data visualization.) based on the threshold value of a score.
following is a piece of data json
"links": [
{
"source": 17,
"target": 9,
"score": 0.428
},
{
"source": 3,
"target": 9,
"score": 0.198
},
{
"source": 17,
"target": 13,
"score": 0.336
},
{
"source": 11,
"target": 13,
"score": 0.178
},
{
"source": 17,
"target": 13,
"score": 0.336
}]
"nodes": [
{
"size": 8,
"score": 0.5,
"id": "Node1",
"name": "Node1",
"type": "triangle-up"
},
{
"size": 10,
"score": 0.1,
"id": "Node2",
"name": "Node2",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node3",
"name": "Node3",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node4",
"name": "Node4",
"type": "circle"
},
{
"size": 10,
"score": 0.1,
"id": "Node5",
"name": "Node5",
"type": "circle"
}]
so what I did is based on the score parameter in the links, I have deleted it an item from the links array. and when I try to delete the node from the nodes array my graph gives multiple errors. So I want to know is there any way through which I can find the nodes which are not linked with any other nodes so that I can delete or not display them in my data visualization.
I do it in this way:
var links=data.links.filter(function(d) {
return d.score > 0.5;
});
var nodes=data.nodes.filter(function(d) {
if (d3.set(links.map(function(v) { return v.source_id })).values().includes(d.id) | d3.set(links.map(function(m){ return m.target_id })).values().includes(d.id)) {
return d.id
};
});
Then I have a function like drawNetwork(links,nodes){...}
Hope this helps!

How can I sort data with Firebase?

I am creating a little React application about Pokemons. I have in DB informations about all of them (about 900+).
They all contain an id field, which is an integer from 1 to 900+.
But the problem is when I do a request like this :
firebase.database().ref(`mydb`).orderByChild('id').startAt(1).limitToFirst(limit).once('value')
The results are not correct: I have an id array like this: [9,1, 10, 6, 4]
Am I doing something wrong ?
Edit:
I add the result I got when I perform the request I wrote above, I added a custom_id field containing ids as strings, but I stille have an unordered result:
[
{
"base_experience": 239,
"custom_id": "009",
"height": 16,
"id": 9,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/9/encounters",
"name": "blastoise",
"order": 12,
"weight": 855
},
{
"base_experience": 64,
"custom_id": "001",
"height": 7,
"id": 1,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/1/encounters",
"name": "bulbasaur",
"order": 1,
"weight": 69
},
{
"base_experience": 39,
"custom_id": "010",
"height": 3,
"id": 10,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/10/encounters",
"name": "caterpie",
"order": 14,
"weight": 29
},
{
"base_experience": 240,
"custom_id": "006",
"height": 17,
"id": 6,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/6/encounters",
"name": "charizard",
"order": 7,
"weight": 905
},
{
"base_experience": 62,
"custom_id": "004",
"height": 6,
"id": 4,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/4/encounters",
"name": "charmander",
"order": 5,
"weight": 85
},
{
"base_experience": 142,
"custom_id": "005",
"height": 11,
"id": 5,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/5/encounters",
"name": "charmeleon",
"order": 6,
"weight": 190
},
{
"base_experience": 142,
"custom_id": "002",
"height": 10,
"id": 2,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/2/encounters",
"name": "ivysaur",
"order": 2,
"weight": 130
},
{
"base_experience": 63,
"custom_id": "007",
"height": 5,
"id": 7,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/7/encounters",
"name": "squirtle",
"order": 10,
"weight": 90
},
{
"base_experience": 236,
"custom_id": "003",
"height": 20,
"id": 3,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/3/encounters",
"name": "venusaur",
"order": 3,
"weight": 1000
},
{
"base_experience": 142,
"custom_id": "008",
"height": 10,
"id": 8,
"is_default": true,
"location_area_encounters": "https://pokeapi.co/api/v2/pokemon/8/encounters",
"name": "wartortle",
"order": 11,
"weight": 225
}
]
You've not attached your database image, but with the order one thing is sure that these keys in your database are strings.
And when you order string data, it is ordered lexicographically.
So for numbers, this is the normal order:
1
9
10
But for strings, this is the normal order:
"9"
"1"
"10"
I don't think there is any operator in Firebase (nor in most other databases) to change this behaviour.
Instead, you will have to modify the data to get the behavior you want. So: store values that are in the order you need them when sorted lexicographically.
For numbers you can accomplish that by padding them with zeroes:
"001"
"009"
"010"
EDIT:
Now after Json, these values are stored in id field so the above thing does not apply to this.
You may however use a code like this, to do what you're trying:
mDatabase
.child("main_child")
.orderByChild("id")
.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
System.out.println(child.getKey());
}
}
...

how to get inner most child of a JSON data

I have a JSON data which is something like this, And my requirement to fetch the data of inner most children i.e hierarchyLevel: 4. And this JSON data is not static, the hierarchyLevel can go any thing like 5, 6, 7 any thing.
Please help to find solution in javascript.
{
"hierarchylist": [
{
"hierarchyId": 10,
"hierarchyLevel": 0,
"name": "ABC",
"parentId": 0,
"children": [
{
"hierarchyId": 12,
"hierarchyLevel": 1,
"name": "ABC-Child1",
"parentId": 10,
"children": [
{
"hierarchyId": 2,
"hierarchyLevel": 2,
"name": "People Management & Development1 ",
"parentId": 12,
"children": [
{
"hierarchyId": 5,
"hierarchyLevel": 3,
"name": "Resourcing2_1",
"parentId": 2,
"children": [
{
"hierarchyId": 19,
"hierarchyLevel": 4,
"name": "Resource Request ",
"parentId": 5,
"children": [],
"docId": 19,
"docstatusid": 20
}
]
}
]
}
]
}
]
}
]
}
Thanks.
This should get you the inner-most one:
let data = {
"hierarchylist": [
{
"hierarchyId": 10,
"hierarchyLevel": 0,
"name": "ABC",
"parentId": 0,
"children": [
{
"hierarchyId": 12,
"hierarchyLevel": 1,
"name": "ABC-Child1",
"parentId": 10,
"children": [
{
"hierarchyId": 2,
"hierarchyLevel": 2,
"name": "People Management & Development1 ",
"parentId": 12,
"children": [
{
"hierarchyId": 5,
"hierarchyLevel": 3,
"name": "Resourcing2_1",
"parentId": 2,
"children": [
{
"hierarchyId": 19,
"hierarchyLevel": 4,
"name": "Resource Request ",
"parentId": 5,
"children": [],
"docId": 19,
"docstatusid": 20
}
]
}
]
}
]
}
]
}
]
}
let children = data.hierarchylist[0].children;
while(children[0] && children[0].children && children[0].children.length) {
children = children[0].children;
}
console.log(children);
You can try the while loop, here's the example
var children = json['hierarchylist']['children'][0];
while(typeof children !== 'undefined') {
children = children['children'][0];
}
console.log(children);

How to find all unique paths in tree structure

root1
child1
child2
grandchild1
grandchild2
child3
root2
child1
child2
grandchild1
greatgrandchild1
I have an object array like tree structure like above, I want to get all unique paths in like this
Food->Dry Food Items->Local Dry Food Items
Food->Dry Food Items->Thai Dry Food Items
Food->Dry Food Items->Others
Food->Fruits
------
------
This is my object
[
{
"id": 1,
"name": "Food",
"parent_id": 0,
"children": [
{
"id": 5,
"name": "Dry Food Items",
"parent_id": 1,
"children": [
{
"id": 11,
"name": "Local Dry Food Items",
"parent_id": 5
},
{
"id": 12,
"name": "Thai Dry Food Items",
"parent_id": 5
},
{
"id": 60,
"name": "Others",
"parent_id": 5
}
]
},
{
"id": 6,
"name": "Fruits",
"parent_id": 1
},
{
"id": 7,
"name": "LG Branded",
"parent_id": 1
},
{
"id": 8,
"name": "Meat",
"parent_id": 1
},
{
"id": 9,
"name": "Sea food",
"parent_id": 1
},
{
"id": 10,
"name": "Vegetables",
"parent_id": 1,
"children": [
{
"id": 14,
"name": "Local Vegetables",
"parent_id": 10
},
{
"id": 15,
"name": "Thai Vegetables",
"parent_id": 10
}
]
},
{
"id": 38,
"name": "Frozen",
"parent_id": 1
},
{
"id": 39,
"name": "IP Kitchen",
"parent_id": 1,
"children": [
{
"id": 40,
"name": "IP Meat",
"parent_id": 39
},
{
"id": 41,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 42,
"name": "IP Ingredients",
"parent_id": 39
},
{
"id": 43,
"name": "IP Sauce",
"parent_id": 39
},
{
"id": 44,
"name": "IP Seafood",
"parent_id": 39
},
{
"id": 45,
"name": "IP Starter",
"parent_id": 39
},
{
"id": 46,
"name": "IP Desert",
"parent_id": 39
}
]
}
]
},
{
"id": 2,
"name": "Beverage",
"parent_id": 0,
"children": [
{
"id": 16,
"name": "Bar",
"parent_id": 2
},
{
"id": 17,
"name": "Coffee & Tea",
"parent_id": 2
},
{
"id": 18,
"name": "In Can",
"parent_id": 2
},
{
"id": 19,
"name": "Water",
"parent_id": 2
},
{
"id": 47,
"name": "IP Bar",
"parent_id": 2
}
]
},
{
"id": 3,
"name": "Disposable",
"parent_id": 0,
"children": [
{
"id": 21,
"name": "Disposable",
"parent_id": 3
}
]
},
{
"id": 4,
"name": "SOE",
"parent_id": 0,
"children": [
{
"id": 20,
"name": "Cleaning Materials",
"parent_id": 4
},
{
"id": 22,
"name": "Chinaware",
"parent_id": 4
}
]
}
];
I get to all the nodes in the tree
function traverse(categories) {
categories.forEach(function (category) {
if (category.children && category.children.length) {
traverse(category.children);
}
else {
}
}, this);
}
You can use recursion and create a function using forEach loop.
var arr = [{"id":1,"name":"Food","parent_id":0,"children":[{"id":5,"name":"Dry Food Items","parent_id":1,"children":[{"id":11,"name":"Local Dry Food Items","parent_id":5},{"id":12,"name":"Thai Dry Food Items","parent_id":5},{"id":60,"name":"Others","parent_id":5}]},{"id":6,"name":"Fruits","parent_id":1},{"id":7,"name":"LG Branded","parent_id":1},{"id":8,"name":"Meat","parent_id":1},{"id":9,"name":"Sea food","parent_id":1},{"id":10,"name":"Vegetables","parent_id":1,"children":[{"id":14,"name":"Local Vegetables","parent_id":10},{"id":15,"name":"Thai Vegetables","parent_id":10}]},{"id":38,"name":"Frozen","parent_id":1},{"id":39,"name":"IP Kitchen","parent_id":1,"children":[{"id":40,"name":"IP Meat","parent_id":39},{"id":41,"name":"IP Starter","parent_id":39},{"id":42,"name":"IP Ingredients","parent_id":39},{"id":43,"name":"IP Sauce","parent_id":39},{"id":44,"name":"IP Seafood","parent_id":39},{"id":45,"name":"IP Starter","parent_id":39},{"id":46,"name":"IP Desert","parent_id":39}]}]},{"id":2,"name":"Beverage","parent_id":0,"children":[{"id":16,"name":"Bar","parent_id":2},{"id":17,"name":"Coffee & Tea","parent_id":2},{"id":18,"name":"In Can","parent_id":2},{"id":19,"name":"Water","parent_id":2},{"id":47,"name":"IP Bar","parent_id":2}]},{"id":3,"name":"Disposable","parent_id":0,"children":[{"id":21,"name":"Disposable","parent_id":3}]},{"id":4,"name":"SOE","parent_id":0,"children":[{"id":20,"name":"Cleaning Materials","parent_id":4},{"id":22,"name":"Chinaware","parent_id":4}]}]
function getNames(data) {
var result = [];
function loop(data, c) {
data.forEach(function (e) {
var name = !c.length ? e.name : c + '->' + e.name;
if (e.children) { loop(e.children, name); }
else {
result.push({ name: name });
}
});
}
loop(data, '');
return result;
}
console.log(getNames(arr))

Categories

Resources