I am attempting to get just the smiling array under tags then attributes. I have tried both to search and simply select. Every attempt results in an undefined variable. If you could explain how to select the smiling array that would be excellent!
{
"status": "success",
"photos": [{
"url": "http://tinyurl.com/673cksr",
"pid": "F#019cbdb135cff0880096136c4a0b9bad_3547b9aba738e",
"width": 375,
"height": 406,
"tags": [{
"uids": [],
"label": null,
"confirmed": false,
"manual": false,
"width": 30.67,
"height": 28.33,
"yaw": -16,
"roll": -1,
"pitch": 0,
"attributes": {
"face": {
"value": "true",
"confidence": 84
},
"smiling": {
"value": "false",
"confidence": 46
}
},
"points": null,
"similarities": null,
"tid": "TEMP_F#019cbdb135cff0880096136c00d500a7_3547b9aba738e_56.80_41.13_0_1",
"recognizable": true,
"center": {
"x": 56.8,
"y": 41.13
},
"eye_left": {
"x": 66.67,
"y": 35.71,
"confidence": 51,
"id": 449
},
"eye_right": {
"x": 50.67,
"y": 35.47,
"confidence": 57,
"id": 450
},
"mouth_center": {
"x": 60.8,
"y": 51.23,
"confidence": 53,
"id": 615
},
"nose": {
"x": 62.4,
"y": 42.61,
"confidence": 54,
"id": 403
}
}]
}],
"usage": {
"used": 21,
"remaining": 79,
"limit": 100,
"reset_time": 1390111833,
"reset_time_text": "Sun, 19 January 2014 06:10:33 +0000"
},
"operation_id": "edc2f994cd8c4f45b3bc5632fdb27824"
}
This particular code, will aggregate all the smiling attribute from the given object and return that as an Array.
map function will get smiling attribute from each and every tag
concat function will flatten all the attributes and returns a single array per photo.
reduce function will gather all the attributes all the photos and accumulate the result in result and that will be returned at the end.
var result = data.photos.reduce(function(result, currentPhoto) {
return result.concat(currentPhoto.tags.map(function(currentTag) {
return currentTag.attributes.smiling;
}));
}, []);
console.log(result);
Output
[ { value: 'false', confidence: 46 } ]
JSON.parse(json).photos[0].tags[0].attributes.smiling
obj.photos[0].tags[0].attributes.smiling
The best way would be to loop through the tags, instead of hardcoding 0 in there
obj.photos.forEach(function(photo){
photo.tags.forEach(function(tag){
tag.attributes.smiling; // here it is
});
});
It's a bit tricky since your JSON object is a mixture of objects and arrays, but here's how you would get to the "smiling" object (it's an object since there's no associative arrays in JavaScript):
var smiling_object = obj["photos"][0]["tags"][0]["attributes"]["smiling"];
Then you if you want to do something with it:
var some_var = smiling_object["value"];
var some_other_var = smiling_object["confidence"];
alert("The combined string is " + some_var + " and " + some_other_var);
Related
How can I remove objects which do not have an ext key? I want to take pictures, but there is a problem because some objects do not have a picture. I'm confused about filtering. Can it be done with reduce or filter?
{
"posts": [
{
"filename": "1647706792183",
"ext": ".png",
"w": 300,
"h": 450,
"tn_w": 166,
"tn_h": 250,
"tim": 1664328637690788,
"time": 1664328637,
"md5": "Omk9VtmPOD1U38U1OOAP/w==",
"fsize": 200271,
"resto": 0,
"country": "DK",
"bumplimit": 0,
"imagelimit": 0,
"semantic_url": "f1-relentless-formula-one-general-all-smiles",
"replies": 378,
"images": 155,
"unique_ips": 102,
"tail_size": 50
},
{
"now": "09/27/22(Tue)21:31:17",
"name": "Anonymous",
"resto": 123946553,
}
]
}
You can use .filter to run a function that returns true/false for each item of your data.
const data = { posts: [ .... ] };
const postsWithExt = data.posts.filter(post => post.ext !== undefined);
If you want to filter falsy values for .ext then use
posts.filter(post => !post.ext)
You can just filter your posts array where the ext property exists.
e.g.
const data = {
"posts": [{
"filename": "1647706792183",
"ext": ".png",
"w": 300,
"h": 450,
"tn_w": 166,
"tn_h": 250,
"tim": 1664328637690788,
"time": 1664328637,
"md5": "Omk9VtmPOD1U38U1OOAP/w==",
"fsize": 200271,
"resto": 0,
"country": "DK",
"bumplimit": 0,
"imagelimit": 0,
"semantic_url": "f1-relentless-formula-one-general-all-smiles",
"replies": 378,
"images": 155,
"unique_ips": 102,
"tail_size": 50
},
{
"now": "09/27/22(Tue)21:31:17",
"name": "Anonymous",
"resto": 123946553,
}
]
};
const result = data.posts.filter(x => x.ext);
console.log(result);
I already searched for my issue, however, i did not find something, that matches my needs. I want to merge (sum) multiple series of data into a single array. Any dataset that matches the key, shall be summed in the resulting array.
Please see sample data and expected result:
var power1 = [
{
"time": 10,
"power": 100
},
{
"time": 20,
"type": 200
},
{
"time": 30,
"type": 300
}
]
var power2 = [
{
"time": 20,
"type": 200
},
{
"time": 30,
"type": 300
},
{
"time": 40,
"type": 400
}
]
var result = [
{
"time": 10,
"type": 100
},
{
"time": 20,
"type": 400
},
{
"time": 30,
"type": 600
},
{
"time": 40,
"type": 400
}
]
Since this should happen with thousands of items, it should be reasonable fast. Could a Map as intermediate help here?
Thanks in advance
You can concat the two arrays and then perform a reduce operation over it with an object to store the values for each key.
var power1=[{time:10,type:100},{time:20,type:200},{time:30,type:300}],power2=[{time:20,type:200},{time:30,type:300},{time:40,type:400}];
const res = Object.values(
power1.concat(power2).reduce((acc, {type, time})=>{
(acc[time] ??= {time, type: 0}).type += type;
return acc;
}, {})
);
console.log(res);
In the map function of my view I am trying to change a field of a javascript object. Interestingly i can not change a field and than emit that object.
I expect all the name fields if the irem object would be "test". But i can`t change it. Any help would be great..
Map Function
function(doc) {
doc.movieList.forEach(function(item){
item.name = "test";
emit([doc.companyId,item.movieID],item);
});
}
Result
"rows": [
{
"key": [
"147",
"044a30f24e98660a8a8c12d09b1cafb3"
],
"value": {
"categoryID": 4,
"dataModelVersion": 1,
"forSale": false,
"movieID": "044a30f24e98660a8a8c12d09b1cafb3",
"name": "HULK", //This field shoud be "test"
"orderId": 99,
"term": 0,
"visible": true,
"watchCount": 0
}
},
{
"key": [
"147",
"07c3c1bc4ac5d99286ccc54cde06b86a"
],
"value": {
"categoryID": 2,
"dataModelVersion": 1,
"forSale": false,
"movieID": "07c3c1bc4ac5d99286ccc54cde06b86a",
"name": "KACIS-PLANI", //This field shoud be "test"
"orderId": 99,
"term": 0,
"visible": true,
"watchCount": 0
}
},
{
"key": [
"147",
"0c6f28034e39bc94009be0375e2fba2a"
],
"value": {
"categoryID": 8,
"dataModelVersion": 1,
"forSale": false,
"movieID": "0c6f28034e39bc94009be0375e2fba2a",
"name": "YOLA-GELDIK", //This field shoud be "test"
"orderId": 99,
"term": 0,
"visible": true,
"watchCount": 0
}
},
Checking into CouchDB code, the doc provided to the map function is sealed so no modification is allowed over the original object.
You need to copy the original object into a new one in order to modify its attributes.
I am using the nvd3 library to make a bar graph, and I'm running into some trouble trying to figure out how to use my object properly. If I try and use the object that I created from a JSON file, the graph doesn't behave properly(see the picture that I've provided. the bars are off center). I'm building off of a multi-bar graph example so my guess is that it's still trying to display the graph as if there would be multiple bars for each x value.
function dataFactory(seriesNum, perSeries) {
return new d3.range(0,seriesNum).map(function(d,i) { return { //will return the amount of streams to be seen on the graph
key: 'Stream ' + i,
values: new d3.range(0,perSeries).map( function(f,j) {
return {
y: 10 + Math.random()*100, // a random value is given to y and the range of 0-perSeries is put into j
x: j
}
})
};
});
}
However, using the above function and calling it to be used for data works properly. Does anyone know what I have to do to get my object to behave like the object that comes from this function?
My json file if you need to see it:
{
"Member1": {
"key":"test10",
"values": [
{
"x": "test10",
"y": 20
}
]
},
"Member2":{
"key":"test9",
"values": [
{
"x": "test9",
"y": 10
}
]
},
"Member3":{
"key":"test8",
"values": [
{
"x": "test8",
"y": 4
}
]
},
"Member4":{
"key":"test7",
"values": [
{
"x": "test7",
"y": 12
}
]
},
"Member5":{
"key":"test6",
"values": [
{
"x": "test6",
"y": 30
}
]
},
"Member6":{
"key":"test5",
"values": [
{
"x": "test5",
"y": 8
}
]
}
,
"Member7":{
"key":"test4",
"values": [
{
"x": "test4",
"y": 27
}
]
},
"Member8":{
"key":"test3",
"values": [
{
"x": "test3",
"y": 17
}
]
},
"Member9":{
"key":"test2",
"values": [
{
"x": "test2",
"y": 2
}
]
},
"Member10":{
"key":"test11",
"values": [
{
"x": "test11",
"y": 53
}
]
},
"Member11":{
"key":"test13",
"values": [
{
"x": "test13",
"y": 55
}
]
},
"Member12":{
"key":"test14",
"values": [
{
"x": "test14",
"y": 104
}
]
},
"Member13":{
"key":"test15",
"values": [
{
"x": "test15",
"y": 12
}
]
},
"Member14":{
"key":"test16",
"values": [
{
"x": "test16",
"y": 87
}
]
}
}
I have the next JSON object but I can't get latitude and longitude from it. In JavaScript every time I try I get undefined error:
{
"response": {
"date_ts": 1352514978,
"raw": {
"profile": "realtime",
"battery": -100,
"battery_state": "unknown"
},
"uuid": "b191d0d4-a967-47c9-b58b-3b491c8247db",
"location": {
"position": {
"altitude": 0,
"speed": -4,
"horizontal_accuracy": 5,
"latitude": 37.785834,
"longitude": -122.406417,
"heading": 0,
"vertical_accuracy": 0
},
"type": "point"
},
"date": "2012-11-09T20:36:18-06:00"
},
"type": "onSuccess",
"source": {}
}
Any advice?
You need to parse the JSON before you can manipulate it as a complex object structure.
var parsed = JSON.parse(myjson);
console.log(parsed.response.location.position.longitude);
DEMO: http://jsfiddle.net/VwWvP/