I am working on one game application. Intially I made two loaclstorage, one is userCard and another CompCard and I added 3 cricket players cards details to both the local storage.
My 2 local storage and its deatils :
var user_Card = [["Nixon", "McLean", "West Indies",
45, 314, 0, 1, "12.07", "37.58",
46, 3, 21, 8,
"img/cards/7RBKWQPJAG_NixonMcLean.jpg", 1],
["Brian", "McMillan", "South Africa", 78,
841, 1, 0, "23.36", "36.98", 70, 4, 32, 43,
"img/cards/Y9U5UKA60O_BrianMcMillan.jpg", 2],
["Craig", "McMillan", "New Zealand", 197, 4707, 3,
28, "28.18", "35.04", 49, 3, 20, 44,
"img/cards/WE0NUNG80C_CraigMcMillan.jpg", 3]
];
var comp_Card = [["Geoff", "Miller", "England", 25, 136, 0, 0,
"8.5", "32.52", 25, 3, 27, 4,
"img/cards/7ZPIQXC19H_GeoffMiller.jpg", 4],
["Kyle", "Mills", "New Zealand", 165, 1016,
0, 2, "15.87", "26.74", 235, 5, 25, 40,
"img/cards/P43DTA4ZCX_KyleMills.jpg", 5],
["Minhazul", "Adedin", "Bangladesh", 27, 453, 0, 2,
"18.87", "39.3", 13, 2, 39, 2,
"img/cards/CC8ENY3E09_MinhazulAdedin.jpg", 6]
];
userCard =JSON.parse(localStorage['user_Card']);
compCard =JSON.parse(localStorage['comp_Card']);
When I retrieving all cards details are coming, usercard[0] and compcard [0] is the first card, usercard[1] is the second card, usercard[2] is the third card,
My question is when I play the game, the if usercard wins, I want add the loosing compcard to usercard local storgae. And if compcard wins add the loosing usercard to compcard local storage.
Assume userCard[0] is the first card
compcard[0] is the first card
When I playing the game,if userCard[0] wins, I want to add loosing compcard[0] to localstorage userCard and if compCard[0] wins, I want to add loosing usercard[0] to localstorage compCard
Please provide me the the solution for this.
How you can read from Offline Technologies
Web Storage simply provides a key-value mapping, e.g.
localStorage["name"] = username;. Unfortunately, present
implementations only support string-to-string mappings, so you need to
serialise and de-serialise other data structures. You can do so using
JSON.stringify() and JSON.parse().
so you should try something like this:
var userCard =JSON.parse(localStorage.['user_Card']);
userCard.push(compCard[0]);
localStorage.['user_Card'] = JSON.stringify(userCard));
Related
I've built a web-app that where users can upload contacts using CSV files.
Once the CSV file is uploaded, I'd like to map the header fields to the app's fields.
The issue I'm facing is figuring out an efficient and simple way to return a list of CSV headers from a CSV link.
Ideally, I'd like the list to return via an HTTP request; or alternatively, using JavaScript, so I can pass it back to the app.
For example, here is a CSV file containing hurricane counts:
https://people.sc.fsu.edu/~jburkardt/data/csv/hurricanes.csv
The headers are:
Month, "Average", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"
My idea is to run a Python script in AWS Lambda that gets the CSV headers, then sends them to the app using an HTTP request.
Is there another way to get this done client-side or without having to setup a backend infrustructure like AWS Lambda?
There's an option to stream from requests and you can process CSV per line. See: Body Content Workflow
Example:
import requests
url = "https://raw.githubusercontent.com/codeforamerica/ohana-api/master/data/sample-csv/addresses.csv"
r = requests.get(url, stream=True)
for line in r.iter_lines():
header = line.decode("utf-8").split(",")
print(header)
break
This will give you only the header. I've used an example of raw CSV file from github: sample-csv
The "simple" answer is obviously to download the whole file and work with that. But you may be able to save some time. If the file accepts partial downloads, you are in luck. To check that:
import requests
requests.head("https://people.sc.fsu.edu/%7Ejburkardt/data/csv/hurricanes.csv").headers
Yields
{'Date': 'Mon, 27 Dec 2021 14:00:21 GMT',
'Server': 'Apache/2.4.46 (Fedora) OpenSSL/1.1.1g',
'Last-Modified': 'Mon, 27 Jun 2016 12:37:53 GMT',
'ETag': '"1f6-53641c9fb0a40"',
'Accept-Ranges': 'bytes',
'Content-Length': '502',
'X-Frame-Options': 'SAMEORIGIN',
'Keep-Alive': 'timeout=5, max=100',
'Connection': 'Keep-Alive',
'Content-Type': 'text/csv'}
Note the Accept-Ranges: 'bytes'. You can ask for specific portions of the file. Jackpot!
Then it's a question of working with requests and the headers:
resume_headers = {'Range':'bytes=0-2048'}
r = requests.get(url, stream=True, headers=resume_headers)
for chunk in r.iter_content(chunk_size=1024):
print(chunk)
Output:
b'"Month", "Average", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015"\n"May", 0.1, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0 \n"Jun", 0.5, 2, 1, 1, 0, 0, 1, 1, 2, 2, 0, 1\n"Jul", 0.7, 5, 1, 1, 2, 0, 1, 3, 0, 2, 2, 1\n"Aug", 2.3, 6, 3, 2, 4, 4, 4, 7, 8, 2, 2, 3\n"Sep", 3.5, 6, 4, 7, 4, 2, 8, 5, 2, 5, 2, 5\n"Oct", 2.0, 8, 0, 1, 3, 2, 5, 1, 5, 2, 3, 0\n"Nov", 0.5, 3, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1\n"Dec", 0.0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1\n\n'
So we have our header, as well as a couple more lines.
Note that I print, but you can save into a variable, a large string, write to a local CSV file,... whatever suits your need. Also, I selected a range of 2048 bytes, and chunks of 1024 bytes; adapt to the situation!
Given the following arrays:
const x = [2, 14, 54, 109, 129, 136, 165, 312, 320, 330, 335, 348, 399, 440, 450, 461, 482, 501, 546, 547, 549, 559, 582, 584, 615, 620, 647, 682];
const y = [539, 681, 682, 683];
Using node v 7.3.0 I observe the following unexpected behavior:
[> x.find(y.includes, y);
undefined
[> y.find(x.includes, x);
682
Sample Snippet:
const x = [2, 14, 54, 109, 129, 136, 165, 312, 320, 330, 335, 348, 399, 440, 450, 461, 482, 501, 546, 547, 549, 559, 582, 584, 615, 620, 647, 682];
const y = [539, 681, 682, 683];
console.log(x.find(y.includes, y))
console.log(y.find(x.includes, x))
However code like x.find(element => y.includes(element)); always finds the element as expected.
I don't get why the two calls that just use find and includes would ever yield different results and would be delighted if someone knows an explanation.
The reason x.find(y.includes, y); is returning undefined is because of arguments passed in function.
Callback of Array.find expects 3 values viz., item, index, array and callback of Array.includes expects 2 arguments, viz., item, fromIndex.
Basically, your current index will be treated as fromIndex in Array.includes and will skip elements before it.
So after four iterations would look like, Array.includes will look for values after 4th element and y does not have them. Hence it returns undefined.
I know ton of library can prettyprint json just by indenting/newline stuff but here is a line of my heavy json:
"Shape6":{"bounds_start":[0,-6,0],"bounds_end":[3,1,3],"origin":[2,15,-1],"mirror":true,"rotation":[0,0,0.837758],"uv":[15,30]}
All the libraries i found output something like this:
"Shape6": {
"bounds_start": [
0,
-6,
0
],
"bounds_end": [
3,
1,
3
],
"origin": [
2,
15,
-1
],
"mirror": true,
"rotation": [
0,
0,
0.837758
],
"uv": [
15,
30
]
}
But i'm looking for a more human-readable way which not add new lines for small arrays that can fit in a line like:
"Shape6": {
"bounds_start": [0, -6, 0],
"bounds_end": [3, 1, 3],
"origin": [2, 15, -1],
"mirror": true,
"rotation": [0, 0, 0.837758],
"uv": [15, 30]
}
i do want this because my json file is like 6k+ lines on the first example
if you know a js or a php library (for ajax purposes)
i thank you in advance (and sorry for my poor english :))
you can simply do that with using JSON.stringify() function (see the reference https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
the first argument is the object you want to beautify, the second one is the function where we can specify the logic of line indention and new line breakings, the 3rd argument of this function (now 4) is the number for indention for your beautified JSON data
JSON.stringify(obj, function(k,v)
{
if (v instanceof Array)
return JSON.stringify(v);
return v;
}, 4);
I have some JSON that resembles this:
{
"Variable": "Id",
"Stat": 250,
"Value": 2,
"Data": {
"Key1_std": 20,
"Key1_25%": 100,
"Key1_count": 14,
"Key1_75%": 13,
"Key1_mean": 10,
"Key2_std": 20,
"Key2_25%": 100,
"Key2_count": 14,
"Key2_75%": 13,
"Key2_mean": 10,
"Key3_std": 20,
"Key3_25%": 100,
"Key3_count": 14,
"Key3_75%": 13,
"Key3_mean": 10,
},
"Omega": 0.1
}
I need to create a graph that displays only the values for the "*_mean" keys.
How should I go about fetching these specific values from the JSON?
They are randomly spaced throughout the real file. Since JSON is not a regular language, I've avoided regex ... lest my computer be
p͏͔͚̣o͚̤͙̟̟̖ͅss̷̱̣̩̞̟͙e͉̘̩͟s̩͖̹͍s̯͓͍̱͠e̩d̡̯̯̦̣̱ͅͅ b̠̙̗͓y͓̹̳̩̫͎̳͢ ͞C̢͇̹t͎͇h̻͇͜ͅu̻̭͜l͈̝̫u̢̩̹͎̭̫.
Thanks in advance.
You can iterate over keys with object.keys.
Code is like this:
var keys = object.keys(json.Data);
var finalArr = [];
for(i=0;i<keys.length;i++){
if(keys[i].indexOf('mean') > -1){ // mean is part of string
finalArr.push(json.Data[keys[i]]);
}
}
I'm getting values from database in json structure. and i want to display them in table in javascript.Data come in following format.
"free_issue": [
{
"product_id": [14, 15, 16],
"free_product_ids": [15],
"structure": [
{
"req_qty": 10,
"free_qty": 2
},
{
"req_qty": 20,
"free_qty": 5
},
{
"req_qty": 50,
"free_qty": 10
}
]
}
From those data i want to push this data to table row. data in same row with 4 columns.I used array push method to push data. but i'm unable to get it in correct way.
in here all the data should in one row.
Please help me to do this.......
have You tried to do like this?
"free_issue": [
{
"product_id": [14, 15, 16],
"free_product_ids": [null, 15, null],
"structure": [
{
"req_qty": 10,
"free_qty": 2
},
{
"req_qty": 20,
"free_qty": 5
},
{
"req_qty": 50,
"free_qty": 10
}
]
}
I mean You've to generate these arrays before and then attach these arrays to fields of object. Notice: all of arrays must be same length to be able aligned to correct rows.