I use this eample link to customize.There are json array and create bar charts according to array.
Please guide me, this is my json format:
{ "server1": [
{
"mount": "test1",
"value": 207
},
{
"mount": "test2",
"value": 20.07
}
],
"server2": [
{
"mount": "test1",
"value": 45
},
{
"mount": "test2",
"value": 0.04
}
]
}
I want to create 2 separate bar chart.
you can use loop for this
$.getJSON("./data/new.json", function(data){
for(i in data){
alert(i);
data1 = data[i];
after looping using your example this way link
pass value like data array and loop it!
d3.json("./data/new.json", function(error,data1){
for(r in data1){
like this you can implement this
Convert this json to two list using javascript or jquery whichever you like
t1 = [207, 45]
t2 = [20.07, 0.04]
now replace data (d) in the http://bl.ocks.org/mbostock/3885304 with t1 for graph 1 and with t2 for graph 2
it will work for sure
Related
i want fetch 2 data and push array, then merge this two data, who can help me?
My first data example;
{
"experts": [
{
"id": 1,
"name": "XXXXX",
"integration_id": "1",
},
{
"id": 243,
"name": "YYYY",
"integration_id": "2",
},] }
My Second data example https://xxx/api/?staff={integration_id}
{
"uzmanlar": [
{
"id": "1",
"ad_soyad": "xxx",
"pic": "117-k-xxx.jpg",
}
],
}
i want foreach first data array to second data merge. i want print picture screen
const arr1 = res.exprts;
const arr2 = res.uzmanlar;
Array.prototype.push.apply(arr1, arr2);
console.log(arr1) // final merge result would be in arr1
you can try this...
You can achieve this using the ... Spread Syntax:
const array = [...obj1["experts"], ...obj1["uzmanlar"]]
This creates a new array including all elements of the first and second array.
I work on a project where the output of one of our APIs is a JSON array. I'd like to encapsulate this array inside an object.
I try to use a JOLT transformation (this is the first time I use this tool) to achieve this. I've already searched through a lot of example, but I still can't figure out what my JOLT specification has to be to perform the transformation. I can't find what I am looking for.
For example, if my input is like this:
[
{
"id": 1,
"name": "foo"
},
{
"id": 2,
"name": "bar"
}
]
I'd like the output to be:
{
"list":
[
{
"id": 1,
"name": "foo"
},
{
"id": 2,
"name": "bar"
}
]
}
In short, I just want to put my array inside a field of another object.
You can use a shift transformation spec such as
[
{
"operation": "shift",
"spec": {
"*": "list[]"
}
}
]
where "*" wildcard represents indices of the current wrapper array of objects
the demo on the site http://jolt-demo.appspot.com/ is
I am having trouble with querying a MongoDB collection with an array inside.
Here is the structure of my collection that I am querying. This is one record:
{
"_id": "abc123def4567890",
"profile_id": "abc123def4567890",
"image_count": 2,
"images": [
{
"image_id": "ABC123456789",
"image_url": "images/something.jpg",
"geo_loc": "-0.1234,11.234567890",
"title": "A Title",
"shot_time": "01:23:33",
"shot_date": "11/22/2222",
"shot_type": "scenery",
"conditions": "cloudy",
"iso": 16,
"f": 2.4,
"ss": "1/545",
"focal": 6.0,
"equipment": "",
"instructions": "",
"upload_date": 1234567890,
"update_date": 1234567890
},
{
"image_id": "ABC123456789",
"image_url": "images/something.jpg",
"geo_loc": "-0.1234,11.234567890",
"title": "A Title",
"shot_time": "01:23:33",
"shot_date": "11/22/2222",
"shot_type": "portrait",
"conditions": "cloudy",
"iso": "16",
"f": "2.4",
"ss": "1/545",
"focal": "6.0",
"equipment": "",
"instructions": "",
"upload_date": 1234567890,
"update_date": 1234567890
}
]
}
Forgive the formatting, I didn't know how else to show this.
As you can see, it's a profile with a series of images within an array called 'images' and there are 2 images. Each of the 'images' array items contain an object of attributes for the image (url, title, type, etc).
All I want to do is to return the object element whose attributes match certain criteria:
Select object from images which has shot_type = "scenery"
I tried to make it as simple as possible so i started with:
find( { "images.shot_type": "scenery" } )
This returns the entire record and both the images within. So I tried projection but I could not isolate the single object within the array (in this case object at position 0) and return it.
I think the answer lies with projection but I am unsure.
I have gone through the MongoDB documents for hours now and can't find inspiration. I have read about $elemMatch, $, and the other array operators, nothing seems to allow you to single out an array item based on data within. I have been through this page too https://docs.mongodb.com/manual/tutorial/query-arrays/ Still can't work it out.
Can anyone provide help?
Have I made an error by using '$push' to populate my images field (making it an array) instead of using '$set' which would have made it into an embedded document? Would this have made a difference?
Using aggregation:
db.collection.aggregate({
$project: {
_id: 0,
"result": {
$filter: {
input: "$images",
as: "img",
cond: {
$eq: [
"$$img.shot_type",
"scenery"
]
}
}
}
}
})
Playground
You can use $elemMatch in this way (simplified query):
db.collection.find({
"profile_id": "1",
},
{
"images": {
"$elemMatch": {
"shot_type": 1
}
}
})
You can use two objects into find query. The first will filter all document and will only get those whose profile_id is 1. You can omit this stage and use only { } if you wnat to search into the entire collection.
Then, the other object uses $elemMatch to get only the element whose shot_type is 1.
Check an example here
I have an array of > 1000 objects, each with a nested array that looks something like that:
data = [{
"id": 0,
"location": "A",
"basket": [
"milk",
"bread",
"sugar",
"water"
],
}, {
"id": 1,
"location": "B",
"basket": [
"chocolate",
"cereal",
"sugar",
"sauce"
],
}, {
"id": 2,
"location": "C",
"basket": [
"milk",
"cereal",
"soda",
"flour"
],
}]
I have a multi-select dropdown menu that has the list of all items in the "basket" nested array. When I select "sugar", it should be able to return the objects with id=0 and id=1 or if I select both "water" and "milk" should return objects with id=0 and id=2. I have tried using a combination of _.map _.find _.filter, but it doesn't work. Also tried looking for similar questions here, but didn't find one. prefer to use lodash if possible.
You can use this:
var result = _.filter(data, { basket: ['sugar', 'milk'] });
Replace the array of products with whatever you are looking for. They must all occur in the same item for it to be retained in the result.
Although you clearly indicate you prefer a lodash-based solution, I want to add the vanilla JS way as well:
var filtered = data.filter(function(item){
return ['sugar', 'milk'].every(product => item.basket.includes(product));
});
When you want the logic to be that only some of the selected products need to occur in the basket for it to get selected, then also in the lodash version you'll need a callback:
var result = _.filter(data, function(item) {
return _.intersection(item.basket, ['sugar', 'milk']).length;
});
In the vanilla JS version, replace every by some.
My JSON looks as follows:
{
"records": [
{
"_id": "5106f97bdcb713b818d7f1f1",
"cn": "lsacco",
"favorites": [
{
"fullName": "Friend One",
"uid": "friend1"
},
{
"fullName": "Friend Two",
"uid": "friend2"
}
]
}
]
}
When I try to use records.favorites as the root for my JSON reader, I do not get any results populated to my model. Is there a way to do this without having to resort to using an association? Note that in my case, records will only have one element despite it showing an array.
records.favorites isn't valid because the property doesn't exist.
You want:
records[0].favorites
records has been declared as an array so records.favorites will point to nothing in the json data file.
using the index in records should solve the problem.