I have the following Json, in the browser's localstorage, I want to know if there is a way to take it and modify it with JavaScript.
The json you are seeing is modified to make it easier to understand the information, but it grows more than 22 thousand lines and exponentially due to the values of the variable called pricelist.
What I want to do is :
grab the json from localstorage.
make a method that goes through all the json and when it finds the variable "pricelist" delete the content that has "items_ids" and "items"
reload the json to the same key
ANNOTATIONS: -the variable lines can be empty.
-the variable pricelist can be false.
Thank you very much for your attention and your comments.
[
{
"id":"00027-008-0001",
"data":{
"lines":[
[
0,
0,
{
"product_id":23603,
"pricelist":{
"id":10,
"item_ids":[
27069,
26894
],
"items":[
{
"id":20044,
"product_tmpl_id":[
13142,
"[DNCM1LT] DESENGRASANTE NARANJA CITRUSMAX 1 LT"
]
}
]
},
"uom_id":[
1
]
}
],
[
0,
0,
{
"product_id":23666,
"pricelist":false,
"uom_id":[
1
]
}
]
],
"pos_session_id":27
}
},
{
"id":"00027-008-0002",
"data":{
"lines":[
[
0,
0,
{
"product_id":23655,
"pricelist":false,
"uom_id":[
1
]
}
]
],
"pos_session_id":27
}
},
{
"id":"00027-008-0003",
"data":{
"lines":[
],
"pos_session_id":27
}
}]
You can do it like the following:
let obj = JSON.parse(localStorage.get("item"));
obj.key = "value";
localStorage.set("item", JSON.stringify(obj));
Checkout the window.localStorage
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
Related
I want to update progress of the particular course by finding userId and coursename. I have seen all the MongoDB queries for that but still not getting the desired output. Sometimes got an empty array while data exist in DB. I am not getting if I have to add value inside the progress array then how I will apply the MongoDB query on that:
{
userId: "218u092ue029ie",
ABC:{
"courseName": "course1",
"progress": [
1,2,3
]
},
XYZ:{
"courseName": "course2",
"progress": [
1,2
]
},
pqr:{
"courseName": "course3",
"progress": [
1,2,3,4,5
]
}
}
Try like this:
db.yourCollection.updateOne(
{ userId: "218u092ue029ie", "ABC.courseName": "course1" },
{ $set: { "ABC.progress.$": [1, 2, 3, 4] } }
);
I need to traverse a pretty deep object and I need to extract 12 values in different depths. My plan was to extract with the for of the values for each depth but I have a problem in the first depth.
I am a little confused about the for of behavior.
I thought this:
for (const key of Object.keys(jsonData)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData[key];
}
console.log(key, jsonData[key]);
}
for of loop would run a cycle for each key element that it finds inside the object but the loop is only running once. It prints out all necessary keys with values in the lower console.log function but it calls isWantedValue function only once.
Can please somebody explain that to me?
Object looks like this:
{ searchNGResponse:
{ totalCount: [ '420' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ [Object] ],
data: [ [Object] ] } }
console output:
1
called with searchNGResponse
searchNGResponse { totalCount: [ '416' ],
from: [ '-1' ],
count: [ '-1' ],
tooManyResults: [ 'false' ],
collapsed: [ 'false' ],
repoDetails: [ { 'xxx': [Object] } ],
data: [ { artifact: [Object] } ] }
Edit: updated
Your jsonData object has only one key, as the console output shows: searchNGResponse.
What you want is the keys of that object, the searchNGResponse object, specifically jsonData.searchNGResponse like this
for (const key of Object.keys(jsonData.searchNGResponse)){
console.log(i+1);
if (isWantedValue(key))
{
artifactColl[key] = jsonData.searchNGResponse[key];
}
console.log(key, jsonData.searchNGResponse[key]);
}
I need to know the best way to get following results
courseFrequency : [
{
'courses': [
'a.i'
],
'count' : 1
},
{
'courses': [
'robotics'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i'
],
'count' : 2
},
{
'courses': [
'software engineering', 'a.i','robotics'
],
'count' : 1
}
]
from following json data.
arr = [
{
'courses': [
'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
]
},
{
'courses': [
'robotics'
]
},
{
'courses': [
'software engineering', 'a.i'
],
'courses': [
'software engineering', 'a.i','robotics'
]
}];
Basically i need to find out the unique courses and their frequency. What is the most optimal way to do that ?
const hash = {}, result = [];
for(const {courses} of arr){
const k = courses.join("$");
if(hash[k]){
hash[k].count++;
} else {
result.push(hash[k] = { courses, count : 1 });
}
}
Simply use a hashmap to find duplicates. As arrays are compared by reference, we need to join it to a string for referencing ( note that this will fail if a coursename contains the joining symbol ($))
There both of them are best for area relates to them.These concepts are heaving their own property and methods to accomplish a certain task like JSON used for data transfer and cross browsing aspect as the common type data value.Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
JSON is a representation of the data structure, it's not an object or an array.
JSON can be used to send data from the server to the browser, for example, because it is easy for JavaScript to parse into a normal JavaScript data structure.for doing an action on JSON data you need to convert it into an object which is also seamed some property like ARRAY.
Arrays are really good at storing ordered lists and ordering things while the cost of removing/splicing elements is a bit higher.
Relative link
Relative link
I am very new to JSON, stuck in parsing multi level JSON array, I want to parse it using javascript or jquery. From the JSON I want to get application id, application description & Product description
[
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh"
}
]
}
]
},
{
"roadMapData": [
{
"applicationDetail": [
{
"applicationDescr": "R25updated-R25updated",
"applicationId": 352
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 271,
"productSubGroupDes": "TEST123-TEST1234"
}
]
},
{
"productSubGrupDetail": [
{
"productGroupId": 278,
"productSubGroupDes": "ggg-hhhh1"
}
]
}
]
}
]
Thanks in advance :)
Here is the Demo
Check jQuery.parseJSON
var jsonObj = jQuery.parseJSON(jsonString);
for (i = 0; i < jsonObj.length; i++) {
var roadMapData = jsonObj[i].roadMapData;
var applicationDetail = roadMapData[0].applicationDetail; //First Object
var productSubGrupDetail1 = roadMapData[1].productSubGrupDetail; //Second Object
var productSubGrupDetail2 = roadMapData[2].productSubGrupDetail; //Third Object
console.log(applicationDetail[0].applicationDescr); //applicationDetail's First Object
console.log(productSubGrupDetail1[0].productGroupId); //productSubGrupDetail1's First Object
console.log(productSubGrupDetail2[0].productSubGroupDes); //productSubGrupDetail2's First Object
}
If data initially presented in JSON (as a string), you need first parse it into JavaScript object with JSON.parse(json). Then you can access any property with object dot notation. If you are not familiar with objects in JavaScript, check this article.
Im new to JSON and I have to deal with a complex one.
Please see image below:
It has an error:
I don't know how to properly separate the 2 json arrays. I even tried to use : instead of , on line 18 but I still get errors. BTW, I use http://jsonlint.com/ to validate.
On line 2 you gave a key, but failed to do so on line 19. You have to keep the structure.
Remove the key on line 2, they shouldn't be used for arrays in that way.
Edit: In addition, you are trying to put arrays right in objects, switch the opening and ending object marks ({}) with ([]) for arrays on your first and last line.
[
[
{...},
{...},
...
{...}
],
[
{...},
{...},
...
{...}
],
...
[
{...},
{...},
...
{...}
]
]
I believe the correct way to build this JSON should be:
{
"glEntries": [
{
"generalLedgerId":1,
"accountId": 34,
"amount" : 32334.23,
"descripction": "desc1",
"debit" : "Yes"
},
{
"generalLedgerId":2,
"accountId": 35,
"amount" : 323.23,
"descripction": "desc",
"debit" : "Yes"
},
...
]
}
There are many ways to construct JSON data, but it depends on your data and the way you want to present it. Here are a couple examples - hope it helps:
{
"glEntries": [
{
"object1-prop1": "one"
},
{
"object2-prop1": 1,
"object2-prop2": "two"
},
{
"object3-prop1": [
"a",
"r",
"r",
"a",
"y"
],
"object3-prop1.1": "string"
}
],
"otherEntries": [
{
"objectx": "x"
},
{
"objecty": "y"
},
{
"objectz": [
1,
2,
3,
4
]
}
],
"oneEntry": "json"
}
Other Example:
[
{
"obj1-prop": 222
},
{
"obj2-prop": "object2"
},
{
"obj3-prop": "Object3"
},
[
"a",
"r",
"r",
"a",
"y",
777,
888
],
"string",
178,
{
"objectProp": "testing123"
}
]
You have more {} than needed and will make parsing your JSON more difficult:
Structure will work a lot better like this:
{"glentries":[
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"},
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"},
{ "property1":"value", "property2" : "value",..... "lastProperty": "value"}
]
}
Now glentries is an array of objects that have multiple properties to them.
alert( glentries[0].property2 )
The parent structure is an Object, so it is expecting a string Key for the second array. It it's supposed to be an array of arrays, you should be using an array and not an Object.