How to split and parse particular value of json array using Angularjs? - angularjs

How can i split and parse particular json data using angularjs.
My json is
[{"id":"1", "stud":" name=Alex, roll_no=12 | class=12,sec=1" , "tech":"Sam"},
{"id":"2", "stud":" name=john, roll_no=13 | class=12,sec=2" , "tech":"Sandy"},
{"id":"3", "stud":" name=Cate, roll_no=14 | class=12,sec=1" , "tech":"Sandy"},
]
I want to parse this json formated data like if ID =1 .
Name= Alex.
Roll No=12.
Class=12.
Section=1.
Teacher=Sam.

Iterate through the collection with reduce.
On every Object in the collection parse the stud attribute with Regular Expressions. Create the new string and add it to the new collection.
// Pseudocode
collection.reduce((memo, item) => {
const values = item.stud.match(" name=Alex, roll_no=12 | class=12,sec=1".match(/^ name=(\w*), roll_no=(\d{2}).../))
// Add it to memo in a format you like
return memo
})

I wrote you some functions...
function getRowFromJson(json, id){
for(var i=0; i<json.length; i++)
if('undefined' == json[i].id) continue;
else if(json[i].id==id) return json[i];
return false;
}
function parseStudFromRow(row){
var stud = {};
var chunks = row.stud.split("|");
for(var i=0; i<chunks.length; i++){
var morechunks = chunks[i].split(",");
for(var n=0; n<morechunks.length; n++){
var chunkage = morechunks[n].split("=");
stud[chunkage[0].trim()] = chunkage[1].trim();
}
}
return stud;
}
You use it like this..
var row = getRowFromJson(json, 1);
var stud = parseStudFromRow(row);
console.log(stud);
Check out the example.. https://jsfiddle.net/o0v6nyzu/2/

Related

Flutter : How to get the value of the other properties from a List?

All of my Json data is already inside in my model. And I have also a list of data inside "selectedBranches[]". What do I want is to get the other value of the other properties which is the BranchCode and BranchId based on the value from the selectedBranches list and put it to the another list.
My selectedBranches List is consist of list of branchFullName properties from my json data.
But dart says The argument type 'List' can't be assigned to the parameter type 'Pattern'.
#Code Snippet#
selectedBranches = [ex1,ex2,ex3,ex4];
selectedAssignBranches = [];
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var u in newArr) {
AssignBranch assignModel = new AssignBranch();
if (u.branchFullName.contains(selectedBranches/*error*/)) {
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
#Model#
class Datum {
String branchCode;
String branchName;
String stat;
String brID;
String branchFullName;
I already solve the problem. You just need to have a nested for loop. Thats it!
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var i in selectedBranches) {
for (var u in newArr) {
if (u.branchFullName == (i)) {
AssignBranch assignModel = new AssignBranch();
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
}

How to grab variable from one API that is within a nested Array response body?

How to grab the contentID and content title add it within an array and have it used in the URL of API 2
if i use the code below for section title and sectionid it is working because it is not in an nested array but for the contentid and contenttitle it is not working as it is in a nested array.
In the API 1 test tab i have:
for (i = 0; i < resultCount; i++) {
var id = jsonData[i].contents[0].contentId;
var modelString = jsonData[i].contents[0].contentTitle;
console.log(id)
console.log(modelString)
if (modelString.includes(“APIAUTOMATIONcontent”) || modelString.includes(“Testcontent”) || modelString.includes("{{POST-NewSlide}}") || modelString.includes(“stringcontent”)) {
hasDelete.push(id);
// (id) - this creates an integer (int)
// "announcementId": id, (creating object)
// "hasDelete": modelString.includes("Delete") || modelString.includes("Test")
// });
} else {
doesntHaveDelete.push(id)
// "announcementId": id
// });
}
}
// Check that each object in response contained keyword and length matches from test
pm.test(Number of Content that has APIAUTOMATIONcontent or Test ready to be deleted = ${hasDelete.length} out of Total ${resultCount} , function() {
console.log(hasDelete);
console.log(doesntHaveDelete);
pm.expect(hasDelete.length);
});
pm.collectionVariables.set(‘deletesections’, JSON.stringify(hasDelete));
Like you're iterating over each section in your response body, you also need to iterate over the contents array, you can do it like below:
for (i = 0; i < resultCount; i++) {
contentCount = jsonData[i].contents.length;
for (j = 0; j < contentCount; j++) {
let content = jsonData[i].contents[j];
console.log(content.contentId);
console.log(content.sectionId);
console.log(content.contentTitle);
console.log(content.pdfLink);
console.log(content.videoLink);
console.log(content.sortOrder);
}
}

How to delete the array with index 2 in local storage?

My local storage looks like this
{"data":[[0,"Post1","Text1","2016-12-16T11:01:00.000Z"],[1,"Post2","Text2","2016-12-20T14:00:00.000Z"]],[3,"Post3","Text3","2016-12-25T13:00:00.000Z"]]}
How can I delete only one item in the array?
I have tried with this, where postid is the array index I want to delete.
var postid = 1
var info = JSON.parse(localStorage.getItem("rp_data"));
var obj = [];
for(var i = 0; i < info.data.length; i++){
var data = info.data[i];
if(data === postid){
info.splice(i, 1);
}
}
localStorage.setItem("rp_data", JSON.stringify(data));
So the if part is wrong I guess!?
Any input appreciated, thanks.
UPDATE
So with this I can remove one of the posts in the array, where the first item in the array is equal to my postid, so if the postid=1 it will remove the second post in the array.
//var postid = $$(this).attr('data-id');
//postid=parseInt(postid)
postid=1
var info = JSON.parse(localStorage.getItem("rp_data"));
//remove object
for(var i = 0; i < info.data.length; i++){
var data = info.data[i][1];
myApp.alert(data);
if(i === postid){
myApp.alert(i);
info.data.splice(i, 1);
}
}
localStorage.removeItem("rp_data");
localStorage.setItem("rp_data", JSON.stringify(info));
So I have 1 more problems.
If I use postid=1 as above it works and it creates a new local storage with the right values. But if get the value from my form and then try to convert the string to a number it stops working.
This does not work, like it is not converting the string to a number?
var postid = $$(this).attr('data-id');
postid=parseInt(postid)
So why is this not converting it to a number?
Since the items in the array are JSON objects, you cannot compare the object to postid. You should use indexOf. You also don't want to splice the index. This should be the object or item in this case. If you want to remove the item in the object, you would have to iterate through the object as well. So this would be a nested loop.
//remove object
for(var i = 0; i < info.data.length; i++){
var data = info.data[i];
if(i === postid){
info.data.splice(data, 1);
}
}
//remove item in object
for(var i = 0; i < info.data.length; i++){
if(i == postid){
for(var j = 0; j < info.data[i].length; j++){
var item = info.data[i][j];
info.data[i].splice(item,1);
}
}
}

Angular parse response from database

I have response from database:
{"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188},
...
{"id":1170,"sku":0,"word_one":"something","description":"","word_two":"some two","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"ever\"},{\"text\":\"never\"}]","UserID":188}]}
Before I make post: angular.toJson($scope.category);
How I can show category something like this
{{category}} = someone, sometwo ?
Because actually I have string :
[{"text":"someone"},{"text":"sometwo"}]
[{"text":"ever"},{"text":"never"}]
...
You can use the below parsing to achieve what you want. Suppose json is the variable received from database.
var json = {"status":"success","message":"Data selected from database","data":[{"id":1171,"sku":0,"word_one":"one word","description":"","word_two":"two word","mrp":0,"lang_one":"en","image":"","lang_two":"en","status":"Active","category":"[{\"text\":\"someone\"},{\"text\":\"sometwo\"}]","UserID":188}]};
var data = json.data[0].category;
var jsonArray = JSON.parse(data);
var category = "";
jsonArray.map(function(obj, i ) {
if(i != 0) {
category += ",";
}
category += obj.text;
});
console.log(category)
At the end , You attach category to $scope.category.
This work:
var parsed = JSON.parse($scope.c.category);
var arr = [];
for(var x in parsed){
arr.push(parsed[x]);
}
$scope.c.category = arr;

Array retrieval in HTML5 with the help of LocalStorage

I have 1 entry in my local storage file
10/10/2014 19:23:34 (date and time) as a key and
{"entries":[{"name":"s","contact":"s","location":"s","email":"sweety#yahoo.com","detail":"s","f_name":"form1"}]}
(form field records) as value
so my question is how to get this array with separated parameters like Name = s, Contact = s, etc...
If you didn't already parse your data you can parse it like this:
var json = localStorage.getItem('10/10/2014 19:23:34');
if (json) {
var data = JSON.parse(json);
if (data !== null) {
// then you can try to do something with the data within it
var entries = data.entries;
for (var i = 0, l = entries.length; i < l; i++) {
var entry = entries[i];
console.log(entry.name, entry.contact, entry.location, entry.email);
}
}
}

Resources