I have a json data in this format and I would like to load data in an array which is an object, without hard coding the object keys. I would like to get items and labels in each category and each category has it name for object key. At the moment I get the data by
...myData.labels.name; or ...items.name; which is not effective because
the name changes depending on the category.
[
[
{
"key": "mykey",
"category": "myCategoryKey",
"category_label": "myCategoryLabel",
"field": "filter",
"items": {
"name": [
"item1",
"item2"
]
},
"labels": {
"name": [
"Item1",
"Item2"
]
}
},
{
"key": "mykey2",
"category": "myCategoryKey2",
"category_label": "myCategoryLabel2",
"field": "filter",
"items": {
"name2": [
"item1",
"item2"
]
},
"labels": {
"name3": [
"Item1",
"Item2"
]
}
}
]
]
Use Object.keys() to get Keys for items present if values change dynamically.
And then use the keys to get corresponding values.
var data = {
"key": "mykey2",
"category": "myCategoryKey2",
"category_label": "myCategoryLabel2",
"field": "filter",
"items": {
"name2": [
"item1",
"item2"
]
},
"labels": {
"name3": [
"Item1",
"Item2"
]
} }
var labelsPresent = Object.keys(data.labels);
console.log(labelsPresent);
var labelValues= labelsPresent[0];
console.log(data.labels[labelValues]);
Related
How to get the data value from the list, the size of the array, the main thing is not through the index, because the order of the arrays can change and I can get specific data from the code === "size". Unfortunately, the structure cannot be changed. It came to mind only through the filter, by index, but it is impossible
The result should be 100 150
https://jsoneditoronline.org/#left=cloud.b10638604c214b189f87747414e06035
[
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
This data structure is terrible, but a quick fix could be something like this
const data = [
[
"color",
{
"name": "Цвет",
"code": "color",
"list": [
{
"value": "Зеленый"
},
{
"value": "Красный"
}
]
}
],
[
"size",
{
"name": "Размер",
"code": "size",
"list": [
{
"value": "100"
},
{
"value": "150"
}
]
}
]
]
const size = data.filter(element => element[1].code === 'size')[0][1].list.map(element => element.value)
console.log(size)
I am having a node network in the neo4j database which includes multilevel nodes something like,
parent -> child -> sub child -> ...and so on
Now what I need is I need to write a query that will give me the response in the below format.
nodes = [
{
id: parent1,
children : [{
id: child1,
children: [
{
id: sub child1,
children: [... So on]
},
{
id: sub child2,
children: [... So on]
}
]
}]
},
{
id = parent2,
children : [...so on]
}
]
You can do something like:
MATCH (p:Person)
WHERE size((p)<--()) = 0
CALL apoc.path.expandConfig(p, {
relationshipFilter: "PARENT>"
})
YIELD path
WITH collect(path) AS paths
CALL apoc.convert.toTree(paths)
YIELD value
RETURN value;
Matching the top parents, which points no one, then using apoc.path.expandConfig to get their paths and then apoc.convert.toTree in order to get the tree-shape structure that you want.
On a sample data:
MERGE (a:Person{key: 1})
MERGE (b:Person{key: 2})
MERGE (c:Person{key: 3})
MERGE (d:Person{key: 4})
MERGE (e:Person{key: 5})
MERGE (f:Person{key: 6})
MERGE (g:Person{key: 7})
MERGE (h:Person{key: 8})
MERGE (i:Person{key: 9})
MERGE (b)-[:PARENT]-(a)
MERGE (d)-[:PARENT]-(c)
MERGE (d)-[:PARENT]-(i)
MERGE (f)-[:PARENT]-(e)
MERGE (g)-[:PARENT]-(b)
MERGE (g)-[:PARENT]-(d)
MERGE (e)-[:PARENT]-(h)
It returns:
[{
"_type": "Person",
"parent": [
{
"_type": "Person",
"parent": [
{
"_type": "Person",
"_id": 1444,
"key": 8
}
],
"_id": 1441,
"key": 5
}
],
"_id": 1442,
"key": 6
},
{
"_type": "Person",
"parent": [
{
"_type": "Person",
"parent": [
{
"_type": "Person",
"_id": 1437,
"key": 1
}
],
"_id": 1438,
"key": 2
},
{
"_type": "Person",
"parent": [
{
"_type": "Person",
"_id": 1439,
"key": 3
},
{
"_type": "Person",
"_id": 1445,
"key": 9
}
],
"_id": 1440,
"key": 4
}
],
"_id": 1443,
"key": 7
}]
i have the following Javascript object.
var data =
{
"type": [
"car",
"bike"
],
"wheels": [
"4",
"2"
],
"open": [
"Jan",
"Jan"
],
"wheel_press": [
"35",
"19"
],
"max-weight": [
"4000",
"8"
],
"transition_plan": [
"",
null
],
"number_of_occurence": [
5696,
976
],
"model": [
"sedan",
"street"
}
I want to filter on any of the object key to get only the corresponding values for that key.
so far after searching stackoverflow still not able to find similiar question.
I have tried using
data.filter(type ==='car')
and the error i am getting is
.filter is not a function
the expected output is
if i filter on type === 'car'
then it should only show
{
"type": [
"car"
],
"wheels": [
"4"
],
"open": [
"Jan"
],
"wheel_press": [
"35"
],
"max-weight": [
"4000"
],
"transition_plan": [
""
],
"number_of_occurence": [
5696
],
"model": [
"sedan"
}
You can write a function that finds the index of the value inside the given property and then uses Object.fromEntries(), Object.keys() and Array.map() to create a new object as followings
var data={type:["car","bike"],wheels:["4","2"],open:["Jan","Jan"],wheel_press:["35","19"],"max-weight":["4000","8"],transition_plan:["",null],number_of_occurence:[5696,976],model:["sedan","street"]};
const getFilteredObject = (property, value) => {
const index = data[property].indexOf(value)
return Object.fromEntries(Object.keys(data).map(key => ([key, data[key][index]])))
}
const filteredObject = getFilteredObject('type','car');
console.log(filteredObject)
I need to add an array of objects to an another object whose structure has been shown below.
Here arr is the array of objects that needs to be added to "dest" object.
Help would be appreciated
structure:
var arr = [
{ field: "state", value: "value2"},
{ field: "city", value: "value3"}
];
This array of objects needs to added to an object of following structure:
var dest = {
"topic": "test",
"filter": {
"sequence": [
{
"field": "country",
"value": "value1",
}
]
},
"order": [
{
"field": "field1",
"order": "backward"
}
]
}
I would want add the fields of "arr" inside "sequence" so that result would something like this:
var dest = {
"topic": "test",
"filter": {
"sequence": [
{
"field": "country",
"value": "value1",
},
{
"field": "state",
"value": "value2",
},
{
"field": "city",
"value": "value3",
}
]
},
"order": [
{
"field": "field1",
"order": "backward"
}
]
}
Using forEach() you can do it.
var arr = [
{ field: "state", value: "value2"},
{ field: "city", value: "value3"}
];
var dest = {
"topic": "test",
"filter": {
"sequence": [
{
"field": "country",
"value": "value1",
}
]
},
"order": [
{
"field": "field1",
"order": "backward"
}
]
}
arr.forEach(function (item) {
dest.filter.sequence.push(item)
});
console.log(dest)
Also ES6 spread operator can be used to merge
var arr = [
{ field: "state", value: "value2"},
{ field: "city", value: "value3"}
];
var dest = {
"topic": "test",
"filter": {
"sequence": [
{
"field": "country",
"value": "value1",
}
]
},
"order": [
{
"field": "field1",
"order": "backward"
}
]
}
dest.filter.sequence.push(...arr)
console.log(dest)
You just need to use below code to insert array into object.
arr.forEach( function(value, index, array) {
dest.filter.sequence.push(value);
});
I have an array of objects with children. The goal is to remove every item from items arrays.
Is it possible to do without using forEach and map loops? How to use reduce in this case?
The problem is some arrays have items on one level and others have children array with items inside. Sample here:
{
"label": "child1",
"children": [
{
"label": "child2",
"items": [
"item1",
"item2"
]
},
{
"label": "child3",
"items": [
"item1",
"item2",
"item3"
]
}
]
}
As a result, I want to see a mutated array of objects with empty items arrays.
Here`s an object to be mutated:
[
{
"label": "parent",
"children": [
{
"label": "child1",
"children": [
{
"label": "child2",
"items": [
"item1",
"item2"
]
},
{
"label": "child3",
"items": [
"item1",
"item2",
"item3"
]
}
]
},
{
"label": "child4",
"items": []
},
{
"label": "child5",
"items": ["item1","item2"]
}
]
}
]
And here is my incomplete solution:
function flattenDeep(arr) {
return arr.reduce(
(acc, val) =>
Array.isArray(val)
? acc.concat(flattenDeep(val.children))
: acc.concat(val.children),
[]
);
}
Here's a way to empty all items arrays.
The idea is to use a predefined reducer method that can you can use recursively.
const reducer = (reduced, element) => {
// empty items array
if (element.items) {
element.items.length = 0;
}
// if element has children, recursively empty items array from it
if (element.children) {
element.children = element.children.reduce(reducer, []);
}
return reduced.concat(element); // or: [...reduced, element]
};
document.querySelector("pre").textContent =
JSON.stringify(getObj().reduce(reducer, []), null, " ");
// to keep relevant code on top of the snippet
function getObj() {
return [
{
"label": "parent",
"children": [
{
"label": "child1",
"children": [
{
"label": "child2",
"items": [
"item1",
"item2"
]
},
{
"label": "child3",
"items": [
"item1",
"item2",
"item3"
]
}
]
},
{
"label": "child4",
"items": []
},
{
"label": "child5",
"items": ["item1","item2"]
}
]
}
];
}
<pre></pre>