JSON named objects vs array with name property - javascript

This may be a JS question, I'm not sure.
I am using JSON to store data about enemies in a game. I am currently structuring like this:
{
"Area 1": {
"Enemy Name": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
},
"Area 2": {
"Enemy 1": {
"type": "Human",
"lvl": 30
},
"Enemy 2": {
"type": "Human",
"lvl": 30
}
}
}
I was wondering if I should be instead using an array of enemies for each area with a "name" property instead. Like this:
{
"Area 1": [
{
"name": "Enemy Name",
"type": "Human",
"lvl": 10
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
],
"Area 2": [
{
"name": "Enemy 1",
"type": "Human",
"lvl": 30
},
{
"name": "Enemy 2",
"type": "Human",
"lvl": 30
}
]
}
I believe the second is the way I should be doing it but wanted other's feedback. I think with the first way, I'll need to know the names of the enemies in order to read their data whereas, with the array, I can loop through the areas and read the enemies in each area.
Thanks in advance for any help!

The second option feels right. It is properly structured and you can easily loop through any Area to perform operations on Enemies. My suggestion is to go with second one.
var Map = [
Area1: [
{
name: "Enemy 1";
metaData: '...'
},
{
name: "Enemy 2";
metaData: '...'
}
],
Area2: [
{
name: "Enemy 1";
metaData: '...'
}
]
];
Scanning enemies in particular area.
Map[Area1].forEach(function(enemy) {
//Operation ...
});
I hope this helps. :)

Related

Only display objects inside of an array with certain type [GRAPHQL]

So, I'm fairly new to programming and I am trying to only display certain objects from a api response array. Besides that, I am using JSReport and Handlebars. Is there a way to filter out the ones with a special value?
Here's an example code:
{
"data": {
"books": {
"author": {
"name": "Book Bookinson",
"books": [
{
"name": "book 1 ",
"stars": "3"
},
{
"address1": "book 2 ",
"phone1": "1"
},
{
"name": "book 3 ",
"stars": "3"
},
{
"address1": "book 4 ",
"phone1": "3"
},
]
},
}
}
So in the code above, I want to filter out so I only get the object with the "stars": "1", and skip the rest. Is there a clean, easy way to do this that I am yet not aware of? I am aware that I can use the object then index through them, but I get a long list of objects that could really need a filter.
Please go easy on me, freshie here!
Not efficient way to do as below but tried my luck using filter and deep cloning object to not alter the main :
let bookData = {
"data": {
"books": {
"author": {
"name": "Book Bookinson",
"books": [
{
"name": "book 1 ",
"stars": "1"
},
{
"address1": "book 2 ",
"phone1": "1"
},
{
"name": "book 3 ",
"stars": "3"
},
{
"address1": "book 4 ",
"phone1": "3"
},
]
},
},
},
}
let oneStar = JSON.parse(JSON.stringify(bookData)); // deep clone
oneStar.data.books.author.books = oneStar.data.books.author.books.filter(book=>typeof book.stars!=="undefined" && parseInt(book.stars)===1); // filter if book has property stars and equals 1
/* console.log(bookData) */
console.log(oneStar);

Using Map() function to pull out double nested JSON data with ReactJS

I'm trying to pull out the individual images in the "images" object below under "details" section.
Seem to just be getting nothing printing out. Looking for the correct way to pull within the details.images.image1,2, or 3.
Here is the JSON data I'm working with so far:
{
"books": [
{
"title": "title 1",
"image": "/image1.jpg"
},
{
"title": "title 2",
"image": "/image2.jpg"
}
],
"details": [
{
"author": "book author",
"name": "Book name",
"price": 34.99,
"publisher": "Penguin Books",
"images": [
{
"image1": "/image1.jpg",
"image2": "/image2.jpg",
"image3": "/image3.jpg"
}
]
}
]
}
Also here is the JSON call I'm making in a Book component:
{staticdata.details.map(detail => (
<Book
book_name={detail.author}
book_price={detail.price}
image={detail.images.image1}
/>
))}
Here's an example of accessing those nested properties and logging them to the console. It appears your attempt was mostly correct, but images is an array.
const data = {
"books": [
{
"title": "title 1",
"image": "/image1.jpg"
},
{
"title": "title 2",
"image": "/image2.jpg"
}
],
"details": [
{
"author": "book author",
"name": "Book name",
"price": 34.99,
"publisher": "Penguin Books",
"images": [
{
"image1": "/image1.jpg",
"image2": "/image2.jpg",
"image3": "/image3.jpg"
}
]
}
]
}
data.details.map(detail => {
console.log(detail.author, detail.price, detail.images[0].image1);
});

Restructuring JSON before input

Here's an example of object from the JSON output of my database:
{
"id": "http://...",
"type": "example-type",
"title": "Example title",
"container-title": "Example container title",
"page": "1-100",
"issue": "3",
"URL": "http://www.url",
"ISSN": "0123-0123",
"author": [
{
"family": "Smith",
"given": "John"
}
],
"issued": {
"date-parts": [
[
"2000"
]
]
},
"keyword": "Sample Tag"
}
I've had enormous difficulties/bugs referring to the nested fields for author and date when building a data table. What I would like to do is somehow modify/flatten this before using it in the table (using Datatables' dataSrc as described here) and then simply call the restructured data as many times as I need using the datatables API.
So what I now refer to as issued.date-parts.0.0 would be simply year. The structure would be instead:
"authors": "John Smith", "Mark Smith"
"year": "2000"
Use the map function to get the authors
Look at this code snippet
var data = { "id": "http://...", "type": "example-type", "title": "Example title", "container-title": "Example container title", "page": "1-100", "issue": "3", "URL": "http://www.url", "ISSN": "0123-0123", "author": [ { "family": "Smith", "given": "John" }, { "family": "Smith", "given": "Mark" } ], "issued": { "date-parts": [ [ "2000" ] ] }, "keyword": "Sample Tag"};
var result = {
"authors": data.author.map((d) => `${d.given} ${d.family}`),
"year": data.issued['date-parts'][0][0]
}
console.log(result);
.as-console-wrapper {
max-height: 100% !important
}

Loading JSON with D3 when columns are defined separately

I'm using d3.js and I'm having hard time understanding how can I load a JSON which represents a table (columns and rows), which the columns are defined separately.
A normal JSON, which I have no problem loading may look like this:
[
{
"id": 1,
"name": "A green door",
"price": 12.50
},
{
"id": 2,
"name": "A red door",
"price": 12.50
},
{
"id": 3,
"name": "A blue door",
"price": 12.50
}
]
The corresponding JSON with separated columns will look like this:
{
"columns": [
{
"ColumnName":"id",
"DataType":"number"
},
{
"ColumnName":"name",
"DataType":"string"
},
{
"ColumnName":"price",
"DataType":"number"
}
],
"rows": [
[
"1",
"A green door",
"12.50"
],
[
"2",
"A red door",
"12.50"
],
[
"3",
"A blue door",
"12.50"
]
]
}
Is it possible to make d3.js load this kind of JSON without reconstructing a new JSON?
The original structure of the JSON that I receive is not changeable.
Thank you for helping.
There won't be any problem in loading the JSON with separated columns(Format 2) with d3. You will just need to convert the JSON format after loading it, to match the required format of your d3 layout. For that, you may try the code as shown below.
d3.json("path/to/column_json_file_name.json", function(error, data) {
if (error) return console.warn(error);
var columns = data.columns.map(function(d){ return d.ColumnName });
var jsonInRquiredFormat = data.rows.map(function(row,i){
var ob = {};
ob[columns[0]] = parseInt(row[0]);
ob[columns[1]] = row[1];
ob[columns[2]] = parseFloat(row[2]);
return ob;
});
console.log(jsonInRquiredFormat);
});
Output obtained for sample input:
[{
"id": 1,
"name": "A green door",
"price": 12.5
}, {
"id": 2,
"name": "A red door",
"price": 12.5
}, {
"id": 3,
"name": "A blue door",
"price": 12.5
}]

What is wrong with this JSON file?

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"
}
]

Categories