I have this JSON array
{"nodes":[
{"type":"simple-node","left":500,"id":"node-start","content":"Start"},
{"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},
{"type":"simple-node","left":500,"id":"node-end""content":"End"}
],
"connections":[
{"start":"node-start","end":"node-1"},
{"start":"node-4","end":"node-5"}
]
}
I need to dynamically add "top" attribute to each element in nodes array so that it may look like this
{"nodes":[
{"type":"simple-node","left":500,"top":3403.252685546875,"id":"node-start","content":"Start"},
{"type":"simple-node","left":500,"top":3703.252685546875,"id":"node-1","content":"Ironing"},
{"type":"simple-node","left":500,"top":3903.252685546875,"id":"node-end""content":"End"}
],
"connections":[
{"start":"node-start","end":"node-1"},
{"start":"node-4","end":"node-5"}
]
You can use forEach on obj.nodes and add object property
var obj = {"nodes":[{"type":"simple-node","left":500,"id":"node-start","content":"Start"},{"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},{"type":"simple-node","left":500,"id":"node-end","content":"End"}],"connections":[{"start":"node-start","end":"node-1"},{"start":"node-4","end":"node-5"}]}
obj.nodes.forEach((e) => {
e.top = 3403.252685546875;
});
console.log(obj)
Update: First you need to turn your json string to object with JSON.parse(yourjson) if you didn't.
var obj={"nodes":[
{"type":"simple-node","left":500,"id":"node-start","content":"Start"},
{"type":"simple-node","left":500,"id":"node-1","content":"Ironing"},
{"type":"simple-node","left":500,"id":"node-end""content":"End"}
]};
var top_array=[2495,4985,3467];
for(i=0;i<obj.nodes.length;i++){
obj.nodes[i].top=top_array[i];
}
In case you need to copy the keys from an array
Related
I have a JSON data set as follows:
{
"content":[],
"layout":[],
"trail":[
{
"content":[
{
"type":"image",
"media":[
{
"type":"image/jpg",
"width":593,
"height":900,
"url":"https://live.staticflickr.com/65535/48208920877_e6b234d3ea_c_d.jpg",
"flickr":{
"flickr-post":"https://www.flickr.com/photos/riketrs/48208920877",
"flickr-album":"https://www.flickr.com/photos/riketrs/albums/72157709130951466"
}
}
]
},
{
"type":"image",
"media":[
{
"type":"image/jpg",
"width":1600,
"height":900,
"url":"https://live.staticflickr.com/2817/33807326532_91013ef6b1_h_d.jpg",
"flickr":{
"flickr-post":"https://www.flickr.com/photos/146758538#N03/33807326532",
"flickr-album":"https://www.flickr.com/photos/146758538#N03/albums/72157681438471236"
}
}
]
}
],
"colors":{
"c0":"#1e1e1d",
"c1":"#78736f",
"c2":"#b2a89f"
}
}
]
}
I would like to console.log the "url" key for each of the images shown here.
(https://live.staticflickr.com/65535/48208920877_e6b234d3ea_c_d.jpg and https://live.staticflickr.com/2817/33807326532_91013ef6b1_h_d.jpg)
I tried some code but I'm very new to JSON in general, I've looked at some other answers to do with JSON but I'm not quite sure how to achieve what I want.
JSFiddle: https://jsfiddle.net/fj6qveh1/1/
I appreciate all advice, including links to other answers that I potentially missed.
Thank you!
url is a property of an object. There can be many of these in a media array. (This data only shows one object per array.) media itself is an property of objects inside the content array.
Use map, and flatMap.
map to return the URL values from the objects in media, and flatMap to return a flat array of the nested arrays returned by map.
const data={content:[],layout:[],trail:[{content:[{type:"image",media:[{type:"image/jpg",width:593,height:900,url:"https://live.staticflickr.com/65535/48208920877_e6b234d3ea_c_d.jpg",flickr:{"flickr-post":"https://www.flickr.com/photos/riketrs/48208920877","flickr-album":"https://www.flickr.com/photos/riketrs/albums/72157709130951466"}}]},{type:"image",media:[{type:"image/jpg",width:1600,height:900,url:"https://live.staticflickr.com/2817/33807326532_91013ef6b1_h_d.jpg",flickr:{"flickr-post":"https://www.flickr.com/photos/146758538#N03/33807326532","flickr-album":"https://www.flickr.com/photos/146758538#N03/albums/72157681438471236"}},{type:"image/jpg",width:1600,height:900,url:"https://live.dummyimage.com/2817/dummy.jpg",flickr:{"flickr-post":"https://www.flickr.com/photos/146758538#N03/33807326532","flickr-album":"https://www.flickr.com/photos/146758538#N03/albums/72157681438471236"}}]}],colors:{c0:"#1e1e1d",c1:"#78736f",c2:"#b2a89f"}}]};
const content = data.trail[0].content;
const urls = content.flatMap(obj => {
return obj.media.map(inner => inner.url);
});
console.log(urls)
The easiest way is to use map function. Given that you are very new to programming (the solution has little to do with JSON itself, since the first step is to parse JSON string to a JavaScript object), it would be better if you try yourself. But you start with
let urls = trail["content"].map(x => x["media"][0]["url"])
for more about map function look here
There is a table in the table so for each table:
for(let i in trail){
var content = trail[i]["content"];
content.forEach(content => content.media.forEach(media => console.log(media.url)))
}
To access object properties, you can use a dot (.), and to access an array element, you use its index in square brackets ([]). So you just keep repeating these steps as necessary until you get to the content you're looking for.
Here's how that looks on a simplified version of your object, using the forEach method of arrays to apply a custom function to each item in the content array:
const json = getJson();
json.trail[0].content.forEach(item=>console.log(item.media[0].url));
function getJson(){
let obj = {
"trail": [{
"content": [
{ "media": [{ "url":"image #65535/48208920877_e6b234d3ea_c_d.jpg" }]},
{ "media": [{"url":"image #2817/33807326532_91013ef6b1_h_d.jpg"}]}
]
}]
};
return obj;
}
I have a json data with this structure in Nodejs
{
ids:
[
'Deploy:1426ba8e-5d9d-4572-888d-2265ae7474ce',
'Deploy:f899e1e4-d512-469d-979c-835e98ced8b7',
'Deploy:02f530d3-9727-4894-a1be-c2f90c430251',
'Deploy:254f10bd-374a-41c8-a0ca-7a5ba37e7d2e',
'Deploy:511f878b-a9e9-4c6b-bbf8-1024131fcc86'
]
}
How can I get the first value for "Deploy" key? Preferably JS code not extra libraries.
So for example, I want to get this value 1426ba8e-5d9d-4572-888d-2265ae7474ce which is the first entry.
var firstDeployValue = data.ids[0].split(':')[1]; should give you 1426ba8e-5d9d-4572-888d-2265ae7474ce If you want Deploy in there as well remove the split bit.
You can achieve that with split;
const data = {
ids: [
'Deploy:1426ba8e-5d9d-4572-888d-2265ae7474ce',
'Deploy:f899e1e4-d512-469d-979c-835e98ced8b7',
'Deploy:02f530d3-9727-4894-a1be-c2f90c430251',
'Deploy:254f10bd-374a-41c8-a0ca-7a5ba37e7d2e',
'Deploy:511f878b-a9e9-4c6b-bbf8-1024131fcc86'
]
};
const result = data.ids[0].split(':')[1];
console.log(result);
you can just assign it to an object, like
jsonObj = {
ids:
[
'Deploy:1426ba8e-5d9d-4572-888d-2265ae7474ce',
'Deploy:f899e1e4-d512-469d-979c-835e98ced8b7',
'Deploy:02f530d3-9727-4894-a1be-c2f90c430251',
'Deploy:254f10bd-374a-41c8-a0ca-7a5ba37e7d2e',
'Deploy:511f878b-a9e9-4c6b-bbf8-1024131fcc86'
]
}
and then just access it using jsonObj.ids[0].match(regexpattern)
I am using Node js to process a JSON object
The Json structure is like:
{
"data":{
"datadetails":{
"first":"abc",
"second":"1920",
"coordinates":[
{
"X":0,
"Y":3,
"exists":true
},
{
"X":23,
"Y":35,
"exists":true
},
{
"X":42,
"Y":16,
"exists":true
}
]
}
}
}
when I try to get the data in html I can until I try to use the coordinates array
I am using {{data.datadetails.second}} to get each field
But when I use {{data.datadetails.coordinates[0].X}} it shows blank space
When in javascript I try to use the whole array it gives me [object Object], [object Object]... as the result... just a string of "object" words
How can I have the real contents from my nested array?
This is the correct notation to access a particular array index in Mustache:
{{data.datadetails.coordinates.0.X}}
If you want to iterate over the array, use {{#...}}:
{{#data.datadetails.coordinates}}
X: {{X}}
Y: {{Y}}
{{/data.datadetails.coordinates}}
I found the way, thanks #robertklep you guided me...
var data = [];
"{{#data.datadetails.coordinates}}";
var coord = {
X:Number("{{X}}"),
Y:Number("{{Y}}")
};
data.push(coord);
"{{/data.datadetails.coordinates}}";
and it is a json array...
Json:
{
"comments":[
{"id":1,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/1.json"},
{"id":2,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/2.json"},{"id":3,"author_name":"Yerassyl","comment_text":"Hello world!","url":"http://localhost:3000/comments/3.json"},
{"id":4,"author_name":"Yerassyl","comment_text":"hi there","url":"http://localhost:3000/comments/4.json"}
]
}
How to iterate over each comment in comments. I want something like that:
//pseudocode
comments.each(key,value){
// do something
}
I tried map, but map is for arrays.
EDIT:
If i delete root node 'comments' i can use .map:
var commentNodes = this.props.comments.map(function(comment,index){
});
Ignore this.props, it is actually React.js.
console.log(this.props.comments) returns my json objects with root node 'comments'
Assuming you have
var obj = {
"comments":[
{"id":1,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/1.json"},
{"id":2,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/2.json"},{"id":3,"author_name":"Yerassyl","comment_text":"Hello world!","url":"http://localhost:3000/comments/3.json"},
{"id":4,"author_name":"Yerassyl","comment_text":"hi there","url":"http://localhost:3000/comments/4.json"}
]
};
You can just do, for example,
obj.comments.map(function (comment) {
console.log(comment);
});
Assuming you have already JSON.parsed the string, you can use forEach to iterate. Map is only for returning a new array from your existing values.
this.props.comments.comments.forEach(function(value, index) {
console.log(value, index);
});
edit: Sounds like this.props.comments is the root object. Hence the accessor above
Firstly you have to parse your JSON data:
var json = '{
"comments":[
{"id":1,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/1.json"},
{"id":2,"author_name":null,"comment_text":null,"url":"http://localhost:3000/comments/2.json"},{"id":3,"author_name":"Yerassyl","comment_text":"Hello world!","url":"http://localhost:3000/comments/3.json"},
{"id":4,"author_name":"Yerassyl","comment_text":"hi there","url":"http://localhost:3000/comments/4.json"}
]
}';
var data = JSON.parse(json);
And then you can proceed and loop throught comments like this:
data.comments.forEach(function(comment, index) {
console.log("Comments["+index+"]: "+comment);
});
Note:
Once your JSON is parsed you will get an object including an array of comments so you can easily use all the Array.prototype methods with it including forEach and map.
I have a javascript object that contains addresses as follows
var addressList = [
{"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
{"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
]
I want to add another "address" to it so i have somthing like the following
[
{"AddressID":"10011","AddressType":"Delivery","AddressLine1":"4 Caerleon Drive","AddressLine2":"Bittern","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 5LF","Country":"United Kingdom","ContactID":"10011"},
{"AddressID":"10012","AddressType":"Home","AddressLine1":"526 Butts Road","AddressLine2":"Sholing","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"},
{"AddressID":"10013","AddressType":"Home","AddressLine1":"5436 Bfds Road","AddressLine2":"Sherly","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"}
]
i cant figure out how to do this ?
Use Array#push to add a new object to the array:
addressList.push({"AddressID":"10013","AddressType":"Home","AddressLine1":"5436 Bfds Road","AddressLine2":"Sherly","AddressLine3":"","CityTown":"Southampton","County":"Hampshire","PostCode":"SO19 1DJ","Country":"England","ContactID":"10011"});
It's not clear to me what you're encountering, but the probable problem you're running into is that the first snippet you give is a javascript Object, and the second snippet is an Array. You'd need to so something like:
var addressList = [];
addressList.push({"AddressID":"10011",...});
You could then iterate over the list using a forEach:
addressList.forEach(function(address) {
alert(address.AddressID);
doSomethingWithAddress(address);
});
You can read more about Arrays on the MDN
addressList.push({AddressID: 1234, Country: 'Foo', ...});