How to segregate an array of object by using map? - javascript

I have to form an array of object in another array of object based on id. I was able to group the object based the "applicationId" but was not able to group the inside array of attributes and values array. Also the above code gives me duplicate objects. Please help with this I know its a small fix but I spend whole day still no result.stackblitz. Expected output commented below in stackblitz
data.map((el) => {
el.attribute.map((elm) => {
elm.options.map(em => {
permissions.push({
applicationId: el.application, attributes: [{ name: elm.name, value: em.value, disabled: true
}]
})
})
})
});
Input Object
[
{
application: "abc",
attribute: [
{
description: "abc description1"
name: "audio"
options:[
{name: "Yes", value: "Y"}
{name: "No", value: "N"}
]
},
{
description: "abc description2"
name: "video"
options:[
{name: "true", value: "T"}
{name: "false", value: "F"}
]
}
{
description: "abc description3"
name: "call"
options:[
{name: "Yes", value: "Y"}
{name: "false", value: "F"}
]
}
]
},
{
application: "def",
attribute: [
{
description: "def description1"
name: "audio"
options:[
{name: "Yes", value: "Y"}
{name: "No", value: "N"}
]
},
{
description: "def description2"
name: "video"
options:[
{name: "true", value: "T"}
{name: "false", value: "F"}
]
}
{
description: "def description3"
name: "call"
options:[
{name: "Yes", value: "Y"}
{name: "false", value: "F"}
]
}
]
}
]
Expected Output:
permissions:[
{
applicationId:abc
attributes:
[
{
name:audio
value:["Y","N"]
disabled: true
},
{
name:video,
value:["T","F"]
disabled: true
},
{
name:call,
value:["Y","F"]
disabled: true
}
]
},
{
applicationId: def
attributes:
[
{
name:audio
value:["Y","N"]
disabled: true
},
{
name:video,
value:["T","F"]
disabled: true
},
{
name:call,
value:["Y","F"]
disabled: true
}
]
}
]

You could do so using few array maps.
Try the following
var input = [ { application: "abc", attribute: [ { description: "abc description1", name: "audio", options: [ { name: "Yes", value: "Y" }, { name: "No", value: "N" } ] }, { description: "abc description2", name: "video", options: [ { name: "true", value: "T" }, { name: "false", value: "F" } ] }, { description: "abc description3", name: "call", options: [ { name: "Yes", value: "Y" }, { name: "false", value: "F" } ] } ] }, { application: "def", attribute: [ { description: "def description1", name: "audio", options: [ { name: "Yes", value: "Y" }, { name: "No", value: "N" } ] }, { description: "def description2", name: "video", options: [ { name: "true", value: "T" }, { name: "false", value: "F" } ] }, { description: "def description3", name: "call", options: [ { name: "Yes", value: "Y" }, { name: "false", value: "F" } ] } ] } ];
var output = input.map(item => ({
applicationId: item.application,
attributes: item.attribute.map(attr => ({
name: attr.name,
value: attr.options.map(option => option.value),
disabled: true
}))
}));
console.log(output);
I've modified your Stackblitz.
Sidenote: Please try to post a valid object in the question. It'll make it much easier and quicker to reproduce the issue. Much of my time was spent not in the solution but in fixing the object.

Related

How to merge map result in to one array objects?

The object result restructuring function works fine at the same time the map result not coming in one array group.
console.clear();
const filterList = {
ageRange: ["ageRange", "picAgeRange"],
goal: ["goal", "motivation"],
hairColor: ["hairColor", "picHairColor"]
};
const questionList = [{
question: "ageRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "18-30",
selected: true
},
{
value: "31-35",
selected: false
},
{
value: "36-45",
selected: false
},
{
value: "46-55",
selected: false
},
{
value: "55+",
selected: false
}
]
},
{
question: "picAgeRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "country 6",
selected: true
},
{
value: "smart casual 4",
selected: false
},
{
value: "dark denim 6",
selected: false
},
{
value: "sporty 6",
selected: false
},
{
value: "adventure 5",
selected: false
},
{
value: "prints 5",
selected: false
},
{
value: "excentric 5",
selected: false
},
{
value: "dont like any private style",
selected: false
}
]
},
{
question: "goal",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "save time",
selected: true
},
{
value: "personal advice",
selected: false
},
{
value: "inspiration",
selected: false
},
{
value: "testing the service",
selected: false
}
]
},
{
question: "hairColor",
lastUpdated: "2018-07-09T18:13:49.567",
info: null,
type: "SELECT",
answers: [{
value: "blond",
selected: true
},
{
value: "brown",
selected: false
},
{
value: "black",
selected: false
},
{
value: "red",
selected: false
},
{
value: "grey",
selected: false
},
{
value: "noHair",
selected: false
}
]
},
{
question: "picHairColor",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "suit 3",
selected: true
},
{
value: "business casual",
selected: false
},
{
value: "casual",
selected: false
},
{
value: "have to wear uniform",
selected: false
},
{
value: "classic",
selected: false
},
{
value: "conservative",
selected: false
},
{
value: "relaxed 2",
selected: false
},
{
value: "dont like any work style",
selected: false
}
]
}
];
const ofQuestionsList = Object.entries(filterList).map(
([questionKey, questionNames]) => {
return {
[questionKey]: questionList.filter((item) =>
questionNames.includes(item.question)
)
};
}
);
console.log(ofQuestionsList);
The existing result is
[
{
"ageRange": [
{
"question": "ageRange",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
},
{
"question": "picAgeRange",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
},
{
"goal": [
{
"question": "goal",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
},
{
"hairColor": [
{
"question": "hairColor",
"lastUpdated": "2018-07-09T18:13:49.567",
"info": null,
"type": "SELECT"
},
{
"question": "picHairColor",
"lastUpdated": "2018-07-09T18:13:42.541",
"info": null,
"type": "SELECT"
}
]
}
]
The expecting result is
{
"ageRange":[
{
"question":"ageRange",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
},
{
"question":"picAgeRange",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
],
"goal":[
{
"question":"goal",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
],
"hairColor":[
{
"question":"hairColor",
"lastUpdated":"2018-07-09T18:13:49.567",
"info":null,
"type":"SELECT"
},
{
"question":"picHairColor",
"lastUpdated":"2018-07-09T18:13:42.541",
"info":null,
"type":"SELECT"
}
]
}
Array.prototype.map() returns an array, for your case if I understood correctly you want to group your elements and return them in one results object.
This seems like something that can be achieved with Array.prototype.reduce(), so you would have:
const ofQuestionsList = Object.entries(filterList).reduce(
(acc, [questionKey, questionNames]) => {
return {
...acc,
[questionKey]: questionList.filter((item) =>
questionNames.includes(item.question)
)
};
}, {});
This reduce takes 2 parameters, a callback taking the accumulator and your current element that being processed in your array. And the accumulator's initialization with an empty object {}.
Then in each iteration we return a merged object with the previous values of the iterator, and the current one
console.clear();
const filterList = {
ageRange: ["ageRange", "picAgeRange"],
goal: ["goal", "motivation"],
hairColor: ["hairColor", "picHairColor"]
};
const questionList = [{
question: "ageRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "18-30",
selected: true
},
{
value: "31-35",
selected: false
},
{
value: "36-45",
selected: false
},
{
value: "46-55",
selected: false
},
{
value: "55+",
selected: false
}
]
},
{
question: "picAgeRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "country 6",
selected: true
},
{
value: "smart casual 4",
selected: false
},
{
value: "dark denim 6",
selected: false
},
{
value: "sporty 6",
selected: false
},
{
value: "adventure 5",
selected: false
},
{
value: "prints 5",
selected: false
},
{
value: "excentric 5",
selected: false
},
{
value: "dont like any private style",
selected: false
}
]
},
{
question: "goal",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "save time",
selected: true
},
{
value: "personal advice",
selected: false
},
{
value: "inspiration",
selected: false
},
{
value: "testing the service",
selected: false
}
]
},
{
question: "hairColor",
lastUpdated: "2018-07-09T18:13:49.567",
info: null,
type: "SELECT",
answers: [{
value: "blond",
selected: true
},
{
value: "brown",
selected: false
},
{
value: "black",
selected: false
},
{
value: "red",
selected: false
},
{
value: "grey",
selected: false
},
{
value: "noHair",
selected: false
}
]
},
{
question: "picHairColor",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT",
answers: [{
value: "suit 3",
selected: true
},
{
value: "business casual",
selected: false
},
{
value: "casual",
selected: false
},
{
value: "have to wear uniform",
selected: false
},
{
value: "classic",
selected: false
},
{
value: "conservative",
selected: false
},
{
value: "relaxed 2",
selected: false
},
{
value: "dont like any work style",
selected: false
}
]
}
];
const ofQuestionsList = Object.entries(filterList).reduce(
(acc, [questionKey, questionNames]) => {
return {
...acc,
[questionKey]: questionList.filter((item) =>
questionNames.includes(item.question)
)
};
}, {});
console.log(ofQuestionsList);
Manage with result
console.clear();
const result = [{
ageRange: [{
question: "ageRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT"
},
{
question: "picAgeRange",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT"
}
]
},
{
goal: [{
question: "goal",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT"
}]
},
{
hairColor: [{
question: "hairColor",
lastUpdated: "2018-07-09T18:13:49.567",
info: null,
type: "SELECT"
},
{
question: "picHairColor",
lastUpdated: "2018-07-09T18:13:42.541",
info: null,
type: "SELECT"
}
]
}
];
var newObj = result.reduce((a, b) => Object.assign(a, b), {})
console.log(newObj)

How to loop through an array of objects and add a new field using react and javascript?

i want to loop through array of objects and check for a particular property and add a new property "disabled" to the array.
below is the array of objects
const arr_obj = [
{
id: "1",
name: "name1",
type: "type2",
children: [
{
id: "2",
name: "name2",
type: "type4",
children: [
{
id: "3",
name: "name3",
type: "type5",
},
},
{
id: "4",
name: "name4",
type: "type3",
children: [
{
id: "5",
name: "name5",
type: "type4",
children: [],
},
],
},
id: "6",
name: "name6",
type: "type3",
children: [
{
id: "7",
name: "name7",
type: "type4",
children: [],
},
],
}
},
.....//similar objects
];
so from above array of objects i want to check for each object if type === type2 and if type2 then add property disabled: false if not disabled: true.
below is what i have tried
const new_obj = React.useMemo(() => {
return arr_obj.map((arr) => ({
...arr,
disabled: arr?.type !== "type2" ? true : false,
}));
}, [arr_obj]);
this adds disabled property only to outer object it doesnt add to children object.
output with above snippet is like below,
const new_arr = [
{
id: "1",
name: "name1",
type: "type2",
disabled: false,
children: [
{
id: "2",
name: "name2",
type: "type4",
children: [
{
id: "3",
name: "name3",
type: "type5",
},
},
{
id: "4",
name: "name4",
type: "type3",
children: [
{
id: "5",
name: "name5",
type: "type4",
children: [],
},
],
},
id: "6",
name: "name6",
type: "type3",
children: [
{
id: "7",
name: "name7",
type: "type4",
children: [],
},
],
}
},
.....//similar objects
];
expected output is like below,
const new_arr = [
{
id: "1",
name: "name1",
type: "type2",
disabled: false,
children: [
{
id: "2",
name: "name2",
type: "type4",
disabled: true,
children: [
{
id: "3",
name: "name3",
type: "type5",
disabled: true,
},
},
{
id: "4",
name: "name4",
type: "type3",
disabled: true,
children: [
{
id: "5",
name: "name5",
type: "type4",
disabled: true,
children: [],
},
],
},
id: "6",
name: "name6",
type: "type3",
disabled: true
children: [
{
id: "7",
name: "name7",
type: "type4",
disabled: true,
children: [],
},
],
}
},
.....//similar objects
];
How can i fix the above snippet such that it adds disabled property to children too. could someone help me with this. thanks.
EDIT:
tried answer is like below,
function loop_children(children) {
if (!children || children.lengh <=0) {
return;
} else {
return children.map((child) => {
...child,
disabled: child?.type !== "type2" ? true : false,
children: loop_children(children)
})
};
}
}
return arr_obj.map((arr) => ({
...arr,
disabled: arr?.type !== "type2" ? true : false,
children: loop_children(arr.children) //seems like a problem here in adding children field again
}));
but this adds children array under children again.
This code doesnt work. it adds field disabled to children but also adds children within children.
could someone help me with this. thanks.
Not sure why all the others are mapping, just alter the object with a simple recursive call when it has a children property.
const arr_obj = [{
id: "1",
name: "name1",
type: "type2",
children: [{
id: "2",
name: "name2",
type: "type4",
children: [{
id: "3",
name: "name3",
type: "type5",
}, ]
}, ]
}];
const disableEnableObj = (arr, type) => {
arr.forEach(obj => {
obj.disabled = obj.type !== type;
obj.children && disableEnableObj(obj.children, type);
});
}
disableEnableObj(arr_obj, 'type2');
console.log(arr_obj);
You have to loop through the children too. It should look something like this:
function loop_children(children) {
return children.map((child) => {
...child,
disabled: child?.type !== "type2" ? true : false,
children: loop_children(children)
})
}
return arr_obj.map((arr) => ({
...arr,
disabled: arr?.type !== "type2" ? true : false,
children: loop_children(children)
}));

How to return data if value does not exist in the nested array of objects using Higher order functions

Suppose I have a Nested array of objects like below:
let a = [{
title: "A123",
book: "A",
tags: [{
key: "Romantic",
ID: 1
}, {
key: "Sad",
ID: 2
},{
key: "Strange",
ID: 3
}]
}, {
title: "B123",
book: "B",
tags: [{
key: "Parody",
ID: 1
}, {
key: "Romantic",
ID: 2
},{
key: "Happy",
ID: 3
}]
}, {
title: "C123",
book: "C",
tags: [{
key: "Dark",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}, {
title: "D123",
book: "D",
tags: [{
key: "New Life",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}]
Now I am trying to get the output of those objects which does not contain the tags as 'Romantic'.
** Expected Output:**
{
title: "C123",
book: "C",
tags: [{
key: "Dark",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}, {
title: "D123",
book: "D",
tags: [{
key: "New Life",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}
I have tried the below from my end but it is returning all the elements. Is there a way to achieve the expected output?
a.filter( (ele) => ele.tags.filter( (eachTags) => eachTags.key !== 'Romantic'))
You can use every instead of the 2nd filter:
a.filter(book => book.tags.every(tag => tag.key !== "Romantic"));
Which is saying filter the array and exclude a book where any tag is Romantic.
Example:
let a = [{
title: "A123",
book: "A",
tags: [{
key: "Romantic",
ID: 1
}, {
key: "Sad",
ID: 2
},{
key: "Strange",
ID: 3
}]
}, {
title: "B123",
book: "B",
tags: [{
key: "Parody",
ID: 1
}, {
key: "Romantic",
ID: 2
},{
key: "Happy",
ID: 3
}]
}, {
title: "C123",
book: "C",
tags: [{
key: "Dark",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}, {
title: "D123",
book: "D",
tags: [{
key: "New Life",
ID: 1
}, {
key: "Science Fiction",
ID: 2
}]
}];
let notRomantic = a.filter(book => book.tags.every(tag => tag.key !== "Romantic"));
console.log(notRomantic);
Alternatively you could use Array.prototype.some():
let a = [
{title: "A123",book: "A",tags: [{key: "Romantic",ID: 1}, {key: "Sad",ID: 2},{key: "Strange",ID: 3}]},
{title: "B123",book: "B",tags: [{key: "Parody",ID: 1}, {key: "Romantic",ID: 2},{key: "Happy",ID: 3}]},
{title: "C123",book: "C",tags: [{key: "Dark",ID: 1}, {key: "Science Fiction",ID: 2}]},
{title: "D123",book: "D",tags: [{key: "New Life",ID: 1}, {key: "Science Fiction",ID: 2}]}]
console.log(a.filter(o=>!o.tags.some(t=>t.key==="Romantic")))
you can achieve this result using filter and some
const result = a.filter((obj) => !obj.tags.some((o) => o.key === "Romantic"));
let a = [
{
title: "A123",
book: "A",
tags: [
{
key: "Romantic",
ID: 1,
},
{
key: "Sad",
ID: 2,
},
{
key: "Strange",
ID: 3,
},
],
},
{
title: "B123",
book: "B",
tags: [
{
key: "Parody",
ID: 1,
},
{
key: "Romantic",
ID: 2,
},
{
key: "Happy",
ID: 3,
},
],
},
{
title: "C123",
book: "C",
tags: [
{
key: "Dark",
ID: 1,
},
{
key: "Science Fiction",
ID: 2,
},
],
},
{
title: "D123",
book: "D",
tags: [
{
key: "New Life",
ID: 1,
},
{
key: "Science Fiction",
ID: 2,
},
],
},
];
const result = a.filter((obj) => !obj.tags.some((o) => o.key === "Romantic"));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Structuring an array the correct way with lodash groupBy

I have an object that looks like this:
{
data: [
{ id: "1", state: "accepted", estimate_date: "2019-12-17" },
{ id: "2", state: "rejected", estimate_date: "2019-12-17" },
{ id: "3", state: "open", estimate_date: "2019-12-17" },
{ id: "4", state: "open", estimate_date: "2019-12-18" },
{ id: "5", state: "rejected", estimate_date: "2019-12-18" },
{ id: "6", state: "accepted", estimate_date: "2019-12-18" },
]
}
When I use lodash groupBy on the object like this:
const key = 'data';
const groupedEstimates = groupBy(estimateData[key], 'estimate_date');
It returns:
[
[
"2019-12-17"
],
[
[ { id: "1", state: "accepted" } ],
[ { id: "2", state: "rejected" } ],
[ { id: "3", state: "open" } ]
]
],
[
[
"2019-12-18"
],
[
[ { id: "4", state: "open" } ],
[ { id: "5", state: "rejected" } ],
[ { id: "6", state: "accepted" } ]
]
]
But now I'm trying to achieve something like this:
[
{
date: "2019-12-17",
items: [
{ id: "1", state: "accepted" },
{ id: "2", state: "rejected" },
{ id: "3", state: "open" },
]
},
{
date: "2019-12-18",
items: [
{ id: "4", state: "open" },
{ id: "5", state: "rejected" },
{ id: "6", state: "accepted" },
]
}
]
Except I don't know how to achieve that using lodash. It doesn't have to use lodash but I only used that at the start as it seemed an easy solution to my problem. Now that I'm trying to achieve a more sensible data structure I would like some insights on how to achieve it.
After grouping by the estimate_date property, iterate the groups object with _.map(). Generate the group's object by taking the key (2nd param) for the date property, and mapping the items to omit the estimate_date:
const estimateData = {"data":[{"id":"1","state":"accepted","estimate_date":"2019-12-17"},{"id":"2","state":"rejected","estimate_date":"2019-12-17"},{"id":"3","state":"open","estimate_date":"2019-12-17"},{"id":"4","state":"open","estimate_date":"2019-12-18"},{"id":"5","state":"rejected","estimate_date":"2019-12-18"},{"id":"6","state":"accepted","estimate_date":"2019-12-18"}]}
const groupedEstimates = _.map(
_.groupBy(estimateData.data, 'estimate_date'),
(items, date) => ({
date,
items: items.map(o => _.omit(o, 'estimate_date'))
})
)
console.log(groupedEstimates)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

validate json array schema

guys i have this json
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook'
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: Tablets
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android'
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones'
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital'
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR'
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
it says it is not valid json ... so how to make it valid json ??
any help would be appreciated ... thanks a lot
btw i am beginner so please help me
any suggestions ?? thanks again :)
You have some missing commas, where noted and a suposed to be a string without delimiters in your object literal.
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook', // missing ,
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: 'Tablets', // missing string delimiter and comma
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android', // missing ,
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones', // missing ,
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital', // missing ,
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR', // missing ,
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
console.log(menu);
.as-console-wrapper { max-height: 100% !important; top: 0; }
There are two problems
After name property values, comma is missing (at various places)
Tablets is not in quotes
Correct syntax would be.
var menu = [{
name: 'Computers',
children: [{
name: 'Notebook',
children: [{
name: 'Apple'
}, {
name: 'Windows'
}]
}, {
name: 'Tablets',
children: [{
name: 'Apple'
}, {
name: 'Android'
}, {
name: 'Windows'
}]
}]
}, {
name: 'Phones',
children: [{
name: 'Android',
children: [{
name: 'Samsung'
}, {
name: 'Nokia'
}, {
name: 'Lenovo'
}]
}, {
name: 'Windows Phones',
children: [{
name: 'Microsoft'
}, {
name: 'Nokia'
}]
}]
}, {
name: 'Cameras',
children: [{
name: 'Digital',
children: [{
name: 'Nikon'
}, {
name: 'Fuji'
}]
}, {
name: 'DSLR',
children: [{
name: 'Canon'
}, {
name: 'Nikon'
}]
}]
}];
You can copy your code is chrome's console to see where the error is.
you need to put double quotes on key an value like this "key" : "value"
[{
"name": "Computers",
"children": [{
"name": "Notebook",
"children": [{
"name": "Apple"
}, {
"name": "Windows"
}]
}, {
"name": "Tablets",
"children": [{
"name": "Apple"
}, {
"name": "Android"
}, {
"name": "Windows"
}]
}]
}]
And after name property values, comma is missing (at various places)
key and value should be wrapped by double quotes
copy paste the code validate here https://jsonformatter.curiousconcept.com/
[
{
"name":"Computers",
"children":[
{
"name":"Notebook",
"children":[
{
"name":"Apple"
},
{
"name":"Windows"
}
]
},
{
"name":"Tablets",
"children":[
{
"name":"Apple"
},
{
"name":"Android"
},
{
"name":"Windows"
}
]
}
]
},
{
"name":"Phones",
"children":[
{
"name":"Android",
"children":[
{
"name":"Samsung"
},
{
"name":"Nokia"
},
{
"name":"Lenovo"
}
]
},
{
"name":"Windows Phones",
"children":[
{
"name":"Microsoft"
},
{
"name":"Nokia"
}
]
}
]
},
{
"name":"Cameras",
"children":[
{
"name":"Digital",
"children":[
{
"name":"Nikon"
},
{
"name":"Fuji"
}
]
},
{
"name":"DSLR",
"children":[
{
"name":"Canon"
},
{
"name":"Nikon"
}
]
}
]
}
]

Categories

Resources