How to fill an object into specific properties of another object? - javascript

I have an object a and i want to map the values of a into another object andoridConfig such that splashLdpi will have value of what 120x120 has i mean the url,splashMdpi will have value of what 160x160 has,splashHdpi will have a value of what 240x240 has and so on.
a = {
"120x120": [
"https://storage.googleapis.com/zopping-staging-uploads/120x120/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
],
"160x160": [
"https://storage.googleapis.com/zopping-staging-uploads/160x160/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
],
"240x240": [
"https://storage.googleapis.com/zopping-staging-uploads/240x240/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
],
"320x320": [
"https://storage.googleapis.com/zopping-staging-uploads/320x320/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
],
"480x480": [
"https://storage.googleapis.com/zopping-staging-uploads/480x480/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
],
"640x640": [
"https://storage.googleapis.com/zopping-staging-uploads/640x640/20211221/wallpaperflarecomwallpaper-20211221-104553.jpg"
]
}
"androidAppConfig": {
"playStoreServiceAccount": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112456.png",
"fbServiceAccount": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112456.png",
"icon512x512": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112456.png",
"banner1024x500": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112456.png",
"iconMdpiCircle": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112456.png",
"iconMdpiSquare": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchersq-20211020-112533.png",
"iconHdpiCircle": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112842.png",
"iconHdpiSquare": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchersq-20211020-112946.png",
"iconXhdpiCircle": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-112201.png",
"iconXhdpiSquare": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchersq-20211020-112251.png",
"iconXxhdpiCircle": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-111829.png",
"iconXxhdpiSquare": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchersq-20211020-111956.png",
"iconXxxhdpiCircle": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchercircle-20211020-111431.png",
"iconXxxhdpiSquare": "https://storage.googleapis.com/zopping-uploads/originals/20211020/iclaunchersq-20211020-111631.png",
"splashLdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-112750.webp",
"splashMdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-112645.webp",
"splashHdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-113200.webp",
"splashXhdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-112353.webp",
"splashXxhdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-112053.webp",
"splashXxxhdpi": "https://storage.googleapis.com/zopping-uploads/originals/20211020/splash-20211020-111724.webp"
},
How do i acheive it?
I am thinking of extracting all the urls from object a into an array first and then somehow use that to fill androidAppConfig.

Related

Sort internal array timestamps

I have an array with multiple elements that looks like this:
{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
},
This is the exact format I use to send data into high charts, however, the problem is that the chart breaks if the timestamps inside each environment (DR, SIT) are not in order.
How can I sort the 'data' inside each environment by timestamp?
This JSON is generated in PHP and sent through to JavaScript. So I would like to know how to sort the data inside either PHP or JavaScript.
Thanks.
This is actually rather trivial, once you know about the usort function. It allows you to define your own sorting function, so you can sort based on any factor of whatever the 2 objects it is passed.
Note that from your example json I had to add a set of square brackets around the whole thing to get PHP to parse it with json_decode .
<?php
// note I had to add square brackets to your
// demo json ....
$json='[{
"name": "DR",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
]
],
"type": "areaspline"
},
{
"name": "SIT",
"data": [
[
"1508112000000",
4
],
[
"1534204800000",
1
],
[
"1506384000000",
3
],
[
"1534204800000",
1
],
[
"1531094400000",
1
],
[
"1528502400000",
1
]
],
"type": "areaspline"
}]';
$json_obj_arr=json_decode($json,true);
print_r($json_obj_arr);
print("\n\n");
// a function to handle the sorting itself
// usort will pass it an array(timestamp,datavalue) for both
// $a and $b so we compare the [0] element of each
function cmp($a, $b)
{
if ($a[0] == $b[0]) {
return 0;
}
return ($a[0] < $b[0]) ? -1 : 1;
}
// loop through the array, first is indexed
for($i=0;$i<count($json_obj_arr);$i++){
// then each one of those contains an
// associtive array with an element named ['data'] -
// this is an indexed array that you want sorted
// on the [0] element
usort($json_obj_arr[$i]['data'],"cmp");
}
print_r($json_obj_arr);
print("\n\n");
?>

Javascript/json keep in database and special chars

I have layout builder in React to keep data structure and text I use ImmutableJS objects.
Such structure with attributes as text or css styles is saved into database as JSON.
To make it JSON I using json-immutable library: serialize, deserialize functions.
After save in database I provide configuration for react components as javascript variables. For example my backend generate js file with variables or small part is printing directly in html code using script tag.
Data are JSON or decoded javascript.
The biggest problem I have with save special chars.
For example if someone set ' single quote in some attribute it is saved directly.
But when I print it in html code as
var myConfig = '{anyjson}';
when inside JSON is single quote parser throw error. The same with double quotes, & (ampersant) or any chars used in html code like <,/>
Single quote I replace to \' when I print it in html code.
But I think does exists any way to save keep all data in JSON and still they will easy to decode by deserialize function to parse JSON to ImmutableJS objects.
Code example
https://jsfiddle.net/jaroapp/2yzud6ua/2/
var structure = {
"__iterable":"Map",
"data":[
[
"entityMap",
{
"__iterable":"Map",
"data":[
[
"html_el_qb7tyhi",
{
"__iterable":"Map",
"data":[
[
"imported",
false
],
[
"path",
"html_el_qb7tyhi"
],
[
"componentData",
{
"__iterable":"Map",
"data":[
]
}
],
[
"draftjsObject",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"draftjs",
true
],
[
"data",
{
"__iterable":"OrderedMap",
"data":[
[
"text",
"B&B is the best company. It's my hope for new markets."
]
]
}
],
[
"chunk",
null
],
[
"style",
{
"__iterable":"OrderedMap",
"data":[
[
"background-image",
"url(\"/path/to/image.jpg\")"
]
]
}
],
[
"attr",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"runEditor",
false
],
[
"entityMap",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"type",
"div"
],
[
"key",
"html_el_qb7tyhi"
]
]
}
],
[
"html_el_2dgupn7",
{
"__iterable":"Map",
"data":[
[
"imported",
false
],
[
"path",
"html_el_2dgupn7"
],
[
"componentData",
{
"__iterable":"Map",
"data":[
]
}
],
[
"draftjsObject",
{
"entityMap":{
},
"blocks":[
{
"key":"3ia22",
"text":"Text saved with html inside",
"type":"unstyled",
"depth":0,
"inlineStyleRanges":[
],
"entityRanges":[
],
"data":{
}
}
]
}
],
[
"draftjs",
true
],
[
"data",
{
"__iterable":"OrderedMap",
"data":[
[
"text",
null
],
[
"html",
"<p class=\"md-block-unstyled\">Text saved with html inside</p>"
]
]
}
],
[
"chunk",
null
],
[
"style",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"attr",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"runEditor",
false
],
[
"entityMap",
{
"__iterable":"OrderedMap",
"data":[
]
}
],
[
"type",
"div"
],
[
"key",
"html_el_2dgupn7"
]
]
}
]
]
}
],
[
"containersMap",
{
"__iterable":"Map",
"data":[
]
}
],
[
"componentsMap",
{
"__iterable":"Map",
"data":[
[
"entityMap",
{
"__iterable":"OrderedMap",
"data":[
]
}
]
]
}
]
]
};
Such structure I set as parameter into ReactJS component.
If I set it as JSON and wrap in quotes then the browser throws an error. If I set it as JavaScript object into the React component I can't make ImmutableJS from this one, because this structure is read by this
https://www.npmjs.com/package/json-immutable library (I use the same to make JSON from Immutable JS to save it in database);
Thanks in advance for any hints.
I solved this problem. Short description, maybe will helpful for someone.
I get file with deserialize function form json-immutable package. And I modified function deserialize to
export function deserialize(json){
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if(typeof json==='string'){
return JSON.parse(json, function(key, value, options){
return revive(key, value, options);
});
}else{
return JSON.parse(JSON.stringify(json), function(key, value, options){
return revive(key, value, options);
});
}
}
Function in package use JSON.parse for param. Maybe it's not the most elegant solution but I don't have time to find way to don't "re-json" object. Standard Object.keys and map return origin object.

Syphoning out irregularities in a large array using a loop. JS

I have this huge array that is representing opening and closing times. (this stretches for an entire week). I also created a new feature in which it allows users to have holiday hours in which they can change their hours....but also close their doors during a specific day. The problem that I've run across is when a customer chooses to close for a day, the date for the large array becomes a blank array.
Is there an effective way to trigger something if the elements inside one of those arrays return nothing?
[
[],
[
"2015-10-16T13:00:00Z",
"2015-10-16T21:00:00Z"
],
[],
[
"2015-10-18T13:00:00Z",
"2015-10-18T21:00:00Z"
],
[
"2015-10-19T05:00:00Z",
"2015-10-20T05:00:00Z"
],
[
"2015-10-20T13:00:00Z",
"2015-10-20T21:00:00Z"
],
[
"2015-10-21T13:00:00Z",
"2015-10-21T21:00:00Z"
]
]
So up here ^^ is what my large array looks like when a customer chooses to be closed for the day.
You can use .map and check for x.length === 0 condition.
(function() {
var data = [
[],
[
"2015-10-16T13:00:00Z",
"2015-10-16T21:00:00Z"
],
[],
[
"2015-10-18T13:00:00Z",
"2015-10-18T21:00:00Z"
],
[
"2015-10-19T05:00:00Z",
"2015-10-20T05:00:00Z"
],
[
"2015-10-20T13:00:00Z",
"2015-10-20T21:00:00Z"
],
[
"2015-10-21T13:00:00Z",
"2015-10-21T21:00:00Z"
]
];
data.map(function(x, y) {
if (x.length === 0) {
console.log("There is no data in the position: " + y + ".");
}
});
})();

How to store City, State and Country data related json in single area

I like to know how to construct or store json data for City, State and Country in a single variable ?
I have seen one but it only deal with single kind of data means CAR
{
"cars": {
"Nissan": [
{"model":"Sentra", "doors":4},
{"model":"Maxima", "doors":4}
],
"Ford": [
{"model":"Taurus", "doors":4},
{"model":"Escort", "doors":4}
]
}
}
My intention is to store different kind of json data in single area or in single variable.
What about this ?
{
"countries": [
{...}
],
"states" [
{...}
],
"cities": [
{...}
]
}
maybe this is a direct answer.
{
"Countries":[{
"CountryName":"Indonesia",
"States":[{
"StateName":"Bali",
"Cities":["Denpasar",
"Kuta",
"Tuban"
]},
{
"StateName":"Jakarta",
"Cities":[
"Bandung",
"Tanggerang"
]
}
]
}]}
check the file here
jsonforcountriesstatecities

Access anonymous array using NgRepeat

I have a multi-level array containing some objects at its deepest level.
[
[
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
]
]
I would like to use ngRepeat to display all the items in "FUND" or "DAILY" but I'm unsure how to access objects this deep without names for each of the arrays above.
Sorry if this is a basic question but I wasn't able to find an answer elsewhere.
You'll want to get the first element of your two outer arrays.
$scope.obj = [
[
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
]
]
<ng-repeat el in obj[0][0]>
<span>totassets: {{el[0].FUND.totalAssets}}</span>
<span>navdate: {{el[0].DAILY.navdate}}</span>
</ng-repeat>
An issue you have with the array is that even when you ignore the outer arrays, you're still left with two individual arrays a la:
[
"FUND",
{
"totassets":10.9,
"totdate":"2015-03-23",
"expratiogross":1.35,
"exprationet":1.08
}
],
And:
[
"DAILY",
{
"navdate":"2015-03-23",
"nav":10.05,
"chgamt":0,
"chgpct":0,
"pop":10.05,
"ytdreturn":2.03,
"curr7dayyield":0,
"eff7dayyield":0,
"unsub7dayyield":0,
"30dayyield":0,
"30dayloadyield":0
}
]
So you will need two ngRepeat blocks to achieve what I assume you want to achieve as well as going one level deeper to actually access the values you want.
Here's a quick plnkr to demonstrate what I mean: http://plnkr.co/edit/ArCh8q8w2JoXsg107XwP?p=preview

Categories

Resources