I need to parse json api response, with response.data equals:
[
{
"name": "Programming",
"subcategories": [
{"name": "web-development"},
{"name": "backend-development"},
{"name": "data-scince"}
]
},{
"name": "Design",
"subcategories": [
{"name": "Graphic-design"},
{"name": "Motion-design"},
{"name": "3D modeling"}
]
and I need to return one array[String] with all subcategories, ex ["web-development", "backend-development" ... "3D modeling"]
All that I did is:
let subs = categories.data.map(function(category) {
return category.subcategories.map(function(subcategory) {
return subcategory.name
})
})
and it returns Array of arrays with categories. Im sure, that there near a better and easier way. Thanks!
let data = [{
"name": "Programming",
"subcategories": [{
"name": "web-development"
},
{
"name": "backend-development"
},
{
"name": "data-scince"
}
]
}, {
"name": "Design",
"subcategories": [{
"name": "Graphic-design"
},
{
"name": "Motion-design"
},
{
"name": "3D modeling"
}
]
}]
let subs = data.flatMap(function(category) {
return category.subcategories.map(function(subcategory) {
return subcategory.name
})
})
console.log(subs)
Try using Array flatMap
You can get a flat array by replacing:
categories.data.map
with
categories.data.flatMap
Related
Right now I have an array of object with children that also have an array of objects.
[
{
"code": "mock-code",
"name": "mock-name",
"children": [
{
"code": "mock-child-code",
"name": "mock-child-name",
},
{
"code": "mock-child-code",
"name": "mock-child-name",
},
],
},
{
"code": "mock-code",
"name": "mock-name",
"children": [],
},
{
"code": "mock-code",
"name": "mock-name",
"children": [
{
"code": "mock-code",
"name": "mock-name",
}
],
}
]
I want to extract the children array and concat them to the parent array like below.
[
{
"code": "m1",
"name": "mock-name",
"children": [
{
"code": "mc-1",
"name": "mn-1",
},
{
"code": "mc-2",
"name": "mn-2",
},
],
},
{
"code": "m2",
"name": "mock-name",
"children": [],
},
{
"code": "mm3",
"name": "mock-name",
"children": [
{
"code": "mc-3",
"name": "mn-3",
}
],
}
{
"code": "mc-1",
"name": "mn-1",
},
{
"code": "mc-2",
"name": "mn-2",
},
{
"code": "mc-3",
"name": "mn-3",
}
]
What are someways to do this. I'm currently looping though the child array creating a new array checking if it's not empty. It all seems a bit messy. Is there a clean way to do this?
let fullList = New Array()
parentData.forEach(element => {
if (!!element.children.length) {
fullList.push(element.children);
}
});
return parentData.concat(fullList);
This isn't giving me the desired results since it's adding another array to the parent object but this is where I am at.
const newArray = originalArray.flatMap(element => [element, ...element.children])
This should do it, and as a bonus will preserve the order (parent1, parent1's children, parent2, parent2's children etc.)
Of course, this works if you have only one level of nesting. If you have greater depth level, that would be a bit more complex, probably using Array.prototype.reduce().
I am trying to flatten an array with multiple objects inside my array. It keeps flatting it into one single array. I want it to have multiple objects inside the array but also want everything flatten with the everything removed except the keys and the values.
This is the current array named "livedata".
[
{
"earningsFileId": {
"value": 1234
},
"paymentType": {
"value": "Session",
"errors": [
{
"id": 802462,
"message": "Invalid Combination",
"status": "Processing"
}
]
},
"detailStatus": {
"value": "Processing"
}
},
{
"earningsFileId": {
"value": 5678
},
"paymentType": {
"value": "Session",
"errors": [
{
"id": 802462,
"message": "Invalid Combination",
"status": "Processing"
}
]
},
"detailStatus": {
"value": "Processing"
}
}
]
This is the output I am trying to achieve.
[
{
"earningsFileId": 1234,
"paymentType": "Session",
"detailStatus": "Processing"
},
{
"earningsFileId": 1234,
"paymentType": "Session",
"detailStatus": "Processing"
}
]
data = [];
Object.values(livedata).map((value, keys) => {
Object.keys(value).forEach((key) => {
data[key] = livedata[keys][key]['value']
})
});
You need to create a new "flattened" object from each object in livedata. You can get each key-value pair using Object.entries and reduce that array to your desired object:
const livedata = [{
"earningsFileId": {
"value": 1234
},
"paymentType": {
"value": "Session",
"errors": [{
"id": 802462,
"message": "Invalid Combination",
"status": "Processing"
}]
},
"detailStatus": {
"value": "Processing"
}
},
{
"earningsFileId": {
"value": 5678
},
"paymentType": {
"value": "Session",
"errors": [{
"id": 802462,
"message": "Invalid Combination",
"status": "Processing"
}]
},
"detailStatus": {
"value": "Processing"
}
}
];
const result = livedata.map(d =>
Object.entries(d).reduce((acc, [k, v]) => {
acc[k] = v.value;
return acc;
}, {}))
console.log(result);
i have this data structure:
tree = [
{
"name": "Men Section",
"categories": [
{
"name": "Clothings",
"Subcategories": [
{
"name": "Jackets",
"products": [
{
"name": "jacket 01",
"price": 100
},
{
"name": "jacket 02",
"price": 140
},
// ..and so on
]
]
},
// ..and so on
]
} // ..and so on
]
how can i add new property in products item isSelected: false in javascript (ES5 or ES6 is fine) so the object will be
{
"name": "jacket 01",
"price": 100,
"isSelected": false
}
?
It could be useful, it works for me
tree.forEach(base => {
base.categories.forEach(categories => {
categories.Subcategories.forEach(subCategory =>{
subCategory.products.forEach(product => product.isSelected = false)
})
});
});
so yeah, i should traverse deep to the object with nested foreach. i came up with this:
tree.categories.forEach(function (category) {
category.subcategories.forEach(function (subcategory) {
subcategory.products.forEach(function (product) {
product.isSelected = false;
})
})
})
thanks for the enlightment
I have been trying to delete a nested object from a JavaScript object, with no success and have not been able to find the correct answer through searching previous posts.
Here is what I have been trying to do.
<code id='code'></code>
var myobj = {
"children": [
{
"name": "albuterol ",
"children": [
{
"name": "albuterol - fluticasone ",
"children": [
{
"name": "prednisone ",
"children": [
{
"name": "dexamethasone ",
"children": [],
"size": 1,
"colname": "CONCEPT_NAME.4"
}
],
"size": 3,
"colname": "CONCEPT_NAME.3"
}
],
"size": 4,
"colname": "CONCEPT_NAME.2"
}]}]}
function deleteObject(myobj) {
var x = delete myobj.colname
return (myobj.name, myobj.children)
}
document.getElementById('code').innerText = JSON.stringify(deleteObject(myobj))
I want to delete the object colname. Am I missing something or is the code completely incorrect?
You need a recursive function to delete the property.
var myobj = {
"children": [
{
"name": "albuterol ",
"children": [
{
"name": "albuterol - fluticasone ",
"children": [
{
"name": "prednisone ",
"children": [
{
"name": "dexamethasone ",
"children": [],
"size": 1,
"colname": "CONCEPT_NAME.4"
}
],
"size": 3,
"colname": "CONCEPT_NAME.3"
}
],
"size": 4,
"colname": "CONCEPT_NAME.2"
}]}]}
function deleteColnameRecursive(obj){
delete obj.colname
if(obj.children){
for(var i=0;i<obj.children.length;i++)
deleteColnameRecursive(obj.children[i]);
}
}
deleteColnameRecursive(myobj);
console.log(myobj);
MyObj does not directly have a property of colname.
MyObj has an array named Children.
To delete the proper attribute, select the proper object. For example myObj.children[0].colname
I put it in a parser and all it gives me is "expecting string on line 19". I have no idea what that means.
{
"name": "Rajeev",
"children": [
{
"name": "Joe",
"children": [
{
"name": "Kevin",
"children": [
{
"name": "George"
}
]
},
{
"name": "John",
"children": [
{
"name": "Barb",
}{
"name": "Michael",
}{
"name": "Charles"
}
]{
"name": "Ravinder"
]
},
Your commas are in the wrong place, e.g.
"children": [
{
"name": "Barb"
},{
"name": "Michael"
},{
"name": "Charles"
}
]
The left one is the right one. see for yourself. you had many extra , and unclosed { and [
http://i.stack.imgur.com/9yKNN.jpg
You have a property / value:
"name": "Barb",
… with a trailing comma so the next thing must be another property / value (the string mentioned in the error message is the property name).
However you have:
}{
Either remove the comma or add more details about Barb.
Then you will need to put a comma between the two objects:
}, {
It seems likely that you intended to place the comma causing teh error between the two objects, so you can just move them.
(You have similar errors throughout the rest of the file)
Sorry for the first answer, I saw a missing comma and automatically assumed that was it, but there were many other errors in there. I think this is what you're trying to do
[
{
"name": "Rajeev",
"children": [
{
"name": "Joe",
"children": [
{
"name": "Kevin",
"children": [
{
"name": "George"
}
]
},
{
"name": "John",
"children": [
{
"name": "Barb"
},
{
"name": "Michael"
},
{
"name": "Charles"
}
]
}
]
}
]
},
{
"name": "Ravinder"
}
]