I am trying to get a json object like the below
{
"Name":"UnitedStates",
"Info":[
{"StateName":"Washington",
"Commands":[
{"Type":"Men","Parameter":"10000"},
{"Type":"Women","Parameter":"30000"}
]},
{"StateName":"California",
"Commands":[
{"Type":"Kids","Parameter":"20000"}
]}
]}
I am trying the below
Fiddle
How can I add array of values to existing blank object.
var CountryName = 'UnitedStates';
var Data = {
Name: CountryName,
Info: [
commands :[]
]
}
Data.Info.push({'StateName': 'washington'});
Data.Info.commands.push(
{'Type': 'Men'},
{'Parameter': '1000'},
);
$('.displayJsonBlock').empty();
$('.displayJsonBlock').append('<pre><code>' + JSON.stringify(TemplateData) + '</pre></code>')
If you look at the error console output, you'll see it's giving you a syntax error because Info is trying to be both an object and an array at the same time:
Info: [
commands :[]
]
If you want to push into Info it needs to be an array, but that means you can't define properties in the literal like this. If you want to define properties like this it needs to be a an object like:
Info: {
commands :[]
}
But then you can't push.
I think what you're looking for is:
var CountryName = 'UnitedStates';
var Data = {
Name: CountryName,
Info: [] // info is just an array
}
Data.Info.push({
'StateName': 'washington',
commands: [] // each element of Info is an object with a commands array
});
Data.Info[0].commands.push( // you need to define which element of info. Here it's the first
{'Type': 'Men'},
{'Parameter': '1000'},
);
console.log(Data)
Of course you don't need to do two pushes you can push the whole object including commands:
Data.Info.push({
'StateName': 'washington',
commands: [
{'Type': 'Men'},
{'Parameter': '1000'}
]
})
Well, the structure of JSON data is incorrect, I think you do something like this
var CountryName = 'UnitedStates';
var Data = {
Name: CountryName,
Info: [
{commands :[]}
]
}
After
Data.Info.push({'StateName': 'washington'});
Data.Info['commands'] = [{'Type': 'Men'}, {'Parameter': '1000'}];
There is a good way, but I do not know if it is what you want.
Related
I am working on some trains' open data feed and getting some JSON as a response from a server. I parse this JSON into a data variable and display it as seen below. However, I cannot find a way to iterate over the response to be able to manipulate each message. I want to iterate over each message and then use the data for a record in a SQL database. I cannot get to the point of accessing any individual message data.
How can I create a loop to iterate over each message and extract it's data?
[
{
SF_MSG: {
time: '1666370311000',
area_id: 'TD',
address: '0C',
msg_type: 'SF',
data: '1F'
}
},
{
CA_MSG: {
to: '4333',
time: '1666370311000',
area_id: 'WO',
msg_type: 'CA',
from: '4331',
descr: '2K60'
}
}, ...
]
Edit: using data.forEach(function(message) produces an output of the structure:
{ CA_MSG: { to: '6021', time: '1666372120000', area_id: 'CY', msg_type: 'CA', from: 'STIN', descr: '2Y73' } }
, however, how do I query this inner object, the names of the objects will differ depending on message type if this matters?
try this:
data = JSON.parse(yourJSONdata)
data.map((o, i)=>{
//o is the object, i is the index
// do your processing here
then at the end do
data[i]=processedobject
})
Hello I would like to know if there would be a method to recover the uuid when I push data in my table I put you in the screenshot below
the code
push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
The push function returns a Reference object, from which you can get the key with something like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
console.log(newRef.key);
If you want to use that key in the write operation, you can also separate the creating of the new ID from the writing of the data like this:
const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`));
console.log(newRef.key);
set(newRef, {
name: registerInformation.nameBox,
id: registerInformation.idBox,
categories: registerInformation.categories,
routine: registerInformation.routine,
});
In this snippet, the first line is a pure client-side operation that doesn't actually write anything to the database yet.
this is my JSON output (see below).
I need to consume it using handlebars so I can turn it into HTML. "insects" and "birds" are just placeholders. it can be anything.
{
"insects":
[
{"name":"wasp"}
,{"name":"spider"}
]
, "birds":
[
{"name":"eagle"}
,{"name":"pigeon"}
]
}
This is the desired output.
Use Object.keys to iterate over the keys of an object.
Example:
const input = {
"insects":
[
{"name":"wasp"}
,{"name":"spider"}
],
"birds":
[
{"name":"eagle"}
,{"name":"pigeon"}
]
};
const categories = Object.keys(input); // categories now contains ["insect", "birds"]
Now you can iterate over the categories to do whatever you want to do with them.
My page object is structured so that I have all of the elements in an object and then an array of objects containing data about the fields that can be looped over to test max char length and error texts.
I would like the locator to reference a property that is outside the array so that the value does not need to be updated twice if the element changed.
Snippet from page object as an example...
module.exports = {
siteName: element(by.id('P662_NAME')),
fields: [
{
name: 'site name',
max: 45,
locator: element(by.id('P662_NAME'))
}
]
}
I have tried using the following with no luck...
this.siteName, this.siteName, module.exports.siteName
Is there a way to do this?
Your exporting looks pretty good. Import it correctly.
What you could do is set siteName as another variable and reference that in your fields object like this:
let siteName = "foo"; // now, updating this variable will also update the one in fields
let fields = [{
// other props
locator: siteName
}];
console.log(fields[0].locator); // expects "foo"
// module.exports = { siteName, fields };
Try this :
Export from a file like this
Sandbox: https://codesandbox.io/s/compassionate-bas-fg1c2
var siteName = "dsdsd";
var fields = [
{
name: "site name",
max: 45,
locator: "dsdsd"
}
];
module.exports = {
siteName,
fields
};;
Get it imported like this:
import { siteName } from "./test.js";
console.log(siteName);
I'm having a puzzler here. I have the following collection:
var TagsCollection = Backbone.Collection.extend({
model: TagModel,
parse : function(response) {
console.log(response);
// code to go
return response;
}
});
Now that fetches the following sample JSON object:
{
id: 10149682,
published: "2014-01-13 08:23:00",
title: "Title",
tags: "tag1,tag2,tag3,tag4"
}
now what I want is to insert some code that will re-map the existing response into the following format:
[{name: "tag1"}, {name: "tag2"}, {name: "tag3"}, {name: "tag4"}]
and load it into the collection. (An important note - using Backbone / Underscore methods - for ex. _.chain / _.reduce etc.).
You can split you tags key:
var tags = response.tags.split(',');
And map the resulting array
return _.map(tags, function(tag) {
return {name: tag};
});
You can also try something like:
var tags= "tag1,tag2,tag3,tag4";
var newArr = tags.split(',').map(function(tag){ return {name: tag} });
Important: This works in IE9+ if you need this to run in IE8 or older you need to polufill. You can fine the instructions here