I have an object with an array of images (just the ids).
var listOfImages = {
"number-items": 30,
"pageCount": 0,
"ids": {
0: "image1",
1: "image2"
}
};
I also have a collection of images:
var images = {
"image1": {
"author": "Marc",
"size": "40kb"
},
"image2": {
"author": "Anthony",
"size": "60kb"
},
"image3": {
"author": "Anthony",
"size": "60kb"
}
}; image2,
]
I would like to combine them all so having something like this:
var extendedListOfImages = {
"number-items": 30,
"pageCount": 0,
"ids": {
0: {"id": "image1", "author": "Marc", "size": "40kb"},
1: {"id": "image2", "author": "Anthony", "size": "60kb"}
}
};
This is my code:
https://jsbin.com/pamofudisa/edit?html,js,console,output
New to lodash so I would appreciate a better solution if possible.
My solution right now:
var listOfImages = {
"number-items": 30,
"pageCount": 0,
"ids": {
0: "image1",
1: "image2"
}
};
var images = {
"image1": {
"author": "Marc",
"size": "40kb"
},
"image2": {
"author": "Anthony",
"size": "60kb"
},
"image3": {
"author": "Anthony",
"size": "60kb"
}
};
myIds = _.keyBy(listOfImages.ids);
_.map(myIds, function(key, value){
myIds[key]= images[key];
});
listOfImages.ids=myIds;
console.log(listOfImages);
The code in jsbin:
https://jsbin.com/qesuyewuza/1/edit?html,js,console,output
Related
I'm pretty sure the below can be done, I'm struggling to understand how to do it in MongoDB.
My data is structured like this (demo data):
db={
"recipes": [
{
"id": 1,
"name": "flatbread pizza",
"ingredients": {
"1010": 1,
"1020": 2,
"1030": 200
}
},
{
"id": 2,
"name": "cheese sandwich",
"ingredients": {
"1040": 1,
"1050": 2
}
}
],
"ingredients": [
{
"id": 1010,
"name": "flatbread",
"unit": "pieces"
},
{
"id": 1020,
"name": "garlic",
"unit": "clove"
},
{
"id": 1030,
"name": "tomato sauce",
"unit": "ml"
},
{
"id": 1040,
"name": "bread",
"unit": "slices"
},
{
"id": 1050,
"name": "cheese",
"unit": "slices"
}
]
}
The output I'm trying to achieve would look like this:
[
{
"id": 1,
"name": "flatbread pizza",
“flatbread”: “1 pieces”,
“garlic”: “2 cloves”,
“tomato sauce”: “200 ml”
},
{
"id": 2,
"name": "cheese sandwich",
“bread”: “1 slices”,
“cheese”: “2 slices”
}
]
I've tried several approaches, and I get stuck at the bit where I need to do a lookup based on the ingredient name (which actually is the id). I tried using $objectToArray to turn it into a k-v document, but then I get stuck in how to construct the lookup pipeline.
This is not a simple solution, and probably can be improved:
db.recipes.aggregate([
{
"$addFields": {
ingredientsParts: {
"$objectToArray": "$ingredients"
}
}
},
{
$unwind: "$ingredientsParts"
},
{
"$group": {
_id: "$id",
name: {
$first: "$name"
},
ingredientsParts: {
$push: {
v: "$ingredientsParts.v",
id: {
$toInt: "$ingredientsParts.k"
}
}
}
}
},
{
"$lookup": {
"from": "ingredients",
"localField": "ingredientsParts.id",
"foreignField": "id",
"as": "ingredients"
}
},
{
$unwind: "$ingredients"
},
{
"$addFields": {
"ingredientsPart": {
"$filter": {
input: "$ingredientsParts",
as: "item",
cond: {
$eq: [
"$$item.id",
"$ingredients.id"
]
}
}
}
}
},
{
$project: {
ingredients: 1,
ingredientsPart: {
"$arrayElemAt": [
"$ingredientsPart",
0
]
},
name: 1
}
},
{
"$addFields": {
units: {
k: "$ingredients.name",
v: {
"$concat": [
{
$toString: "$ingredientsPart.v"
},
" ",
"$ingredients.unit"
]
}
}
}
},
{
$group: {
_id: "$_id",
name: {
$first: "$name"
},
units: {
$push: "$units"
}
}
},
{
"$addFields": {
"data": {
"$arrayToObject": "$units"
}
}
},
{
"$addFields": {
"data.id": "$_id",
"data.name": "$name"
}
},
{
"$replaceRoot": {
"newRoot": "$data"
}
}
])
You can see it works here
As rickhg12hs said, it can be modeled better.
I've been able to make a script to save a file using the new fileCreate but when I try to delete a file with fileDelete, I can't seems to get the right ID.
mutation fileDelete($fileIds: [ID!]!) {
fileDelete(fileIds: $fileIds) {
userErrors {
field
message
}
deletedFileIds
}
}
I pass the ID from my last image: "gid://shopify/ImageSource/20805776113730", called with a new private app key (which should use the v2021-10)
If anybody got that mutation to work, I would appreciate any help.
Response from Shopify:
{
"data": {
"fileDelete": null
},
"errors": [{
"message": "invalid id",
"locations": [{
"line": 3,
"column": 4
}],
"path": ["fileDelete"]
}],
"extensions": {
"cost": {
"requestedQueryCost": 10,
"actualQueryCost": 1,
"throttleStatus": {
"maximumAvailable": 1000,
"currentlyAvailable": 999,
"restoreRate": 50
}
}
}
}
Here is what's send:
{
"query": "mutation fileDelete($fileIds: [ID!]!) {\r\n\t\t\tfileDelete(fileIds: $fileIds) {\r\n\t\t\t userErrors {\r\n\t\t\t\tfield\r\n\t\t\t\tmessage\r\n\t\t\t }\r\n\t\t\t deletedFileIds\r\n\t\t\t}\r\n\t\t }",
"variables": {
"fileIds": "gid:\/\/shopify\/ImageSource\/20825330909250"
}
}
You need to send the ImageMedia ID:
{
"fileIds": "gid://shopify/MediaImage/20835931816073"
}
To get that ID, you need to send your request like this:
{
files(first: 99, reverse:true){
edges{
cursor
node{
... on MediaImage {
id
mimeType
image {
originalSrc
}
}
__typename
createdAt
fileStatus
preview{
image{
altText
id
transformedSrc
originalSrc
}
status
}
}
}
}
}
Response:
{
"data": {
"files": {
"edges": [
{
"cursor": "xxxxxx",
"node": {
"id": "gid:\/\/shopify\/MediaImage\/xxxx",
"mimeType": "image\/png",
"image": {
"originalSrc": "xxxx"
},
"__typename": "MediaImage",
"createdAt": "2021-10-21T17:39:58Z",
"fileStatus": "READY",
"preview": {
"image": {
"altText": "xxxx",
"id": "gid:\/\/shopify\/ImageSource\/xxxx",
"transformedSrc": "xxxx",
"originalSrc": "xxxx"
},
"status": "READY"
}
}
},
]
}
},
"extensions": {
"cost": {
"requestedQueryCost": 398,
"actualQueryCost": 18,
"throttleStatus": {
"maximumAvailable": 1000.0,
"currentlyAvailable": 982,
"restoreRate": 50.0
}
}
}
}
We are currently using GA4 data API and faced the issue when custom dimensions returns value "(not set)".
We were using the following article to set custom dimension for the session count, but we still receiving "(not set)" values.
Example of request:
{
"dateRanges": [
{
"startDate": "2021-09-01",
"endDate": "2021-09-05"
}
],
"offset": 0,
"limit": 100,
"dimensionFilter": {
"filter": {
"fieldName": "eventName",
"stringFilter": {
"matchType": 1,
"value": "screen_view",
"caseSensitive": true
}
}
},
"dimensions": [
{
"name": "customUser:applicationID"
},
{
"name": "customEvent:ga_session_number"
},
{
"name": "dateHour"
},
{
"name": "platform"
},
{
"name": "sessionSource"
},
{
"name": "sessionMedium"
},
{
"name": "sessionCampaignName"
},
{
"name": "deviceCategory"
}
],
"metrics": [
{
"name": "userEngagementDuration"
}
]
}
Does anybody have any idea why it may happen?
"data": {
"offset": 0,
"limit": 20,
"total": 1,
"count": 1,
"results": [
{
"id": 1009144,
"name": "A.I.M.",
"modified": "2013-10-17T14:41:30-0400",
"thumbnail": {
"path": "i.annihil.us/u/prod/marvel/i/mg/6/20/52602f21f29ec",
"extension": "jpg"
},
"resourceURI": "gateway.marvel.com/v1/public/characters/1009144",
"comics": {
"available": 33,
"collectionURI": "gateway.marvel.com/v1/public/characters/1009144/comics",
"items": [
{
"resourceURI": "gateway.marvel.com/v1/public/comics/36763",
"name": "Ant-Man & the Wasp (2010) #3"
},
{
"resourceURI": "gateway.marvel.com/v1/public/comics/17553",
"name": "Avengers (1998) #67"
}
]
}
}
]
}
I am using axios to fetch datas from an api inside a React component. I would like to access to the key items in my json response in order to setState but I can't.
export default class Hero extends React.Component {
constructor(props) {
super(props);
this.state = {
details : [],
comics :[]
};
}
componentDidMount() {
axios.get(infoUrl).then((res) => {
this.setState({details : res.data.data.results,
comics : res.data.data.results.results[6].items});
})
}
render() {
(<div>
</div>)
}
}
I can access to my state details but not the comics one.
items being present in comics is not the 6th item in the result array but the 6th item in the first object of the result array and hence you need to access it like.
res.data.data.results[0].comics.items
Change you componentDidMount function to
componentDidMount() {
axios.get(infoUrl).then((res) => {
this.setState({details : res.data.data.results,
comics : res.data.data.results[0].items});
})
}
Results array contains only one item, so you need to use index 0 instead of 6. Another thing is items is present inside comics, so first access comics then access items, use this:
componentDidMount() {
axios.get(infoUrl).then((res) => {
this.setState({
details : res.data.data.results,
comics : res.data.data.results[0].comics.items});
})
}
Run this snippet:
let data = {"data": {
"offset": 0,
"limit": 20,
"total": 1,
"count": 1,
"results": [
{
"id": 1009144,
"name": "A.I.M.",
"modified": "2013-10-17T14:41:30-0400",
"thumbnail": {
"path": "i.annihil.us/u/prod/marvel/i/mg/6/20/52602f21f29ec",
"extension": "jpg"
},
"resourceURI": "gateway.marvel.com/v1/public/characters/1009144",
"comics": {
"available": 33,
"collectionURI": "gateway.marvel.com/v1/public/characters/1009144/comics",
"items": [
{
"resourceURI": "gateway.marvel.com/v1/public/comics/36763",
"name": "Ant-Man & the Wasp (2010) #3"
},
{
"resourceURI": "gateway.marvel.com/v1/public/comics/17553",
"name": "Avengers (1998) #67"
}
]
}
}
]
}
}
console.log('items', data.data.results[0].comics.items)
Now suppose I have a json data formation like this following:
{
"ServiceName": "cacheWebApi",
"Description": "This is a CacheWebApiService",
"IsActive": true,
"Urls": [{ "ServiceAddress": "http://192.168.111.210:8200", "Weight": 5, "IsAvailable": true },
{ "ServiceAddress": ",http://192.168.111.210:8200", "Weight": 3, "IsAvailable": true }]
}
Now what worries me is that the "Urls" is another nested json formation. So how to bind this value to the datatables? And have you got any good ideas (e.g:something like I only wanna show all the ServiceAddress)...
This should do what you need:
var data = [{
"ServiceName": "cacheWebApi",
"Description": "This is a CacheWebApiService",
"IsActive": true,
"Urls": [
{
"ServiceAddress": "http://192.168.111.210:8200",
"Weight": 5,
"IsAvailable": true
},
{
"ServiceAddress": ",http://192.168.111.210:8200",
"Weight": 3,
"IsAvailable": true
}
]
}];
$(function() {
var table = $('#example').dataTable({
"data": data,
"columns": [
{
"data": "ServiceName"
}, {
"data": "Description"
}, {
"data": "IsActive"
}, {
"data": "Urls[0].ServiceAddress"
}, {
"data": "Urls[0].Weight"
}, {
"data": "Urls[0].IsAvailable"
}, {
"data": "Urls[1].ServiceAddress"
}, {
"data": "Urls[1].Weight"
}, {
"data": "Urls[1].IsAvailable"
}
],
});
});
You should put your data in an array though. Working JSFiddle
EDIT
IF the number of Urls isn't defined then you could do something like this:
var table = $('#example').dataTable({
"data": data,
"columns": [
{
"data": "ServiceName"
}, {
"data": "Description"
}, {
"data": "IsActive"
}, {
"data": "Urls",
"render": function(d){
return JSON.stringify(d);
}
}
],
});
I guess that that isn't brilliant but you could do almost anything to that function, for instance:
var table = $('#example').dataTable({
"data": data,
"columns": [
{
"data": "ServiceName"
}, {
"data": "Description"
}, {
"data": "IsActive"
}, {
"data": "Urls",
"render": function(d){
return d.map(function(c){
return c.ServiceAddress
}).join(", ");
}
}
],
});