Convert JSON.STRINGIFY to json array length is not calculating - angularjs

Hi i have excel upload functionality when i upload the excel that should save in to the mongodb(database). I have taken the excel cell value as json stringify and also i converted that as parse. now i don't how to get that json length value.
here i have attached my code
function to_json(workbook) {
debugger;
console.log(workbook);
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
$scope.Sheetname=sheetName; // here sheet name is excel sheet name
MainSheetName=sheetName;
var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}
var HTMLOUT = document.getElementById('htmlout');
function to_html(workbook) {
HTMLOUT.innerHTML = "";
workbook.SheetNames.forEach(function(sheetName) {
var htmlstr = X.write(workbook, {sheet:sheetName, type:'binary', bookType:'html'});
HTMLOUT.innerHTML += htmlstr;
});
}
var tarea = document.getElementById('b64data');
function b64it() {
if(typeof console !== 'undefined') console.log("onload", new Date());
var wb = X.read(tarea.value, {type: 'base64',WTF:wtf_mode});
process_wb(wb);
}
window.b64it = b64it;
var OUT = document.getElementById('out');
var global_wb;
function process_wb(wb) {
global_wb = wb;
var output = "";
var output1 = "";
switch(get_radio_value("format")) {
case "json":
output = JSON.stringify(to_json(wb), 2, 2);
output1=JSON.parse(output);
break;
case "form":
output = to_formulae(wb);
break;
case "html": return to_html(wb);
default:
output = to_csv(wb);
}
if(OUT.innerText === undefined) OUT.textContent = output;
else OUT.innerText = output;
debugger;
$scope.MainInfo=output1; // this return whole data
console.log(output1);
$scope.jsondata=output1.Sheet1[0].length;// this returns undefined. Here sheet name is excel file sheetName it will taken from excel.
console.log($scope.jsondata);
my output is showing like this
{
"Sheet1": [
{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
},
{
"Name": "yyy",
"Age": "23",
"Salary": "232323"
},
{
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}
]
}
now i want length of Sheet1 how can i get that. can any one tell me

If you were to assign your output to obj getting the length of Sheet1 would be as simple as going obj.Sheet1.length

Try this :
var output = {
"Sheet1": [{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
}, {
"Name": "yyy",
"Age": "23",
"Salary": "232323"
}, {
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}]
};
var newArr = $.grep(output1.Sheet1, function( value, index ) {
if(n.Name == "xxx") {
return n;
}
});
console.log(newArr)

You can use angular.fromJson(yourJson).length functionality.
In your case, it should be angular.fromJson($scope.jsondata).length.

As per my understanding from above code, your are getting response into JSON string format so you can't perform JSON operation over it. try to convert json string to json object using JSON.parse method.Below is sample code in node js.
var parseData = JSON.parse(output1);
var parsedata1 = parseData.Sheet1;
for(var namevalue=0;namevalue<parsedata.length;namevalue++){
console.log("Name value: "+parsedata1[namevalue].Name);
}

Related

How to extract element from JSON array and put into another array

How to convert this
[{"col":"ClientPolicy","st":"false","id":"1"},{"col":"Department","st":"true","id":"2"}]
to
[{"ClientPolicy":false,"Department":true}]
this in react js
Use reduce function. Try like below.
const input = [{"col":"ClientPolicy","st":"false","id":"1"},{"col":"Department","st":"true","id":"2"}];
const output = input.reduce((prevValue, { col, st }) => {
prevValue[col] = typeof st === "string" ? JSON.parse(st) : st
return prevValue;
}, {});
console.log([output]);
using Array.reduce
var array = [{
"col": "ClientPolicy",
"st": "false",
"id": "1"
}, {
"col": "Department",
"st": "true",
"id": "2"
}];
var cols = array.reduce((a, c) => {
a[c.col] = Boolean(c.st);
return a;
}, {});
var output = [];
output.push(cols);
console.log(output);

Conversion of list of JSON array to a single object in Angular

I have an array list which needs to be converted to a single object with few of the values from array list using TypeScript in Angular 8. Below is the array:
"arrayList": [{
"name": "Testname1",
"value": "abc"
},
{
"name": "Testname2",
"value": "xyz"
}
]
This needs to be converted to the below format,
data: {
"Testname1": "abc",
"Testname2": "xyz",
}
No matter how much i try, i end up creating a list instead of a single object. Can you please help on the same?
You can use as follows,
var arr = [
{
"name": "Testname1",
"value": "abc"
},
{
"name": "Testname2",
"value": "xyz"
}
];
var result = {};
for (var i = 0; i < arr.length; i++) {
result[arr[i].name] = arr[i].value;
}
console.log(result);
Try with using .reduce() as the following:
const arrayList = [{ "name": "Testname1", "value": "abc" }, { "name": "Testname2", "value": "xyz" }];
const data = arrayList.reduce((a, {name, value}) => {
a[name] = value;
return a;
}, {});
const result = { data };
console.log(result);
Use Array.map() to get a list of [name, value] entries, then use Object.fromEntries() to convert to an object:
const arrayList = [{ "name": "Testname1", "value": "abc" }, { "name": "Testname2", "value": "xyz" }];
const result = Object.fromEntries(arrayList.map(({ name, value }) => [name, value]));
console.log(result);
Please use the below code
const rawData = {
"arrayList": [{
"name": "Testname1",
"value": "abc"
},
{
"name": "Testname2",
"value": "xyz"
}
]
};
const updatedData = {
data: {}
};
for (const item of rawData["arrayList"]) {
updatedData.data[item.name] = item.value;
}
console.log(updatedData);

pushing values from json into array

JSON:
var res =
{
"response": {
"data": {
"profilesearchsnippet": [
[
{
"profileInfo": {
"firstname": "Sundar",
"lastname": "v",
"gender": "male",
"country": "Afghanistan",
"state": "Badakhshan",
"city": "Eshkashem",
"pincode": "",
"plancode": "T001",
"userid": 13
},
"roleInfo": {
"defaultphotoid": 94
}
}
],
[
{
"profileInfo": {
"firstname": "ghg",
"lastname": "vbhvh",
"gender": "male",
"state": "Badakhshan",
"city": "Eshkashem",
"pincode": "454",
"plancode": "T001",
"userid": 22
},
"roleInfo": {
"defaultphotoid": 171
}
}
]
]
}
}
}
In the above json , I need to move roleInfo.defaultphotoid into var image
JS:
$scope.setimage = res.response.data.profilesearchsnippet[0];
for (var i = 0; i++; i<setimage.length; i++){
var image = [];
image .push(setimage[i].roleinfo.defaultphotoid);
}
I assigned one variable named setimage and from there I am trying to push values of all defaultphotoid in another array image to fetch images of all roleinfos, but I am able to fetch only first value.
This is because var image = [] is inside the for loop. It gets re-initialized after every cycle. You need to put it outside the loop
$scope.setimage = res.response.data.profilesearchsnippet[0];
var image = [];
for (var i = 0; i<$scope.setimage.length; i++){
image.push($scope.setimage[i].roleinfo.defaultphotoid);
}
You could use angular.foreach inorder to iterate the elements and set into the variable.
var log = [];
angular.forEach(res.response.data.profilesearchsnippet[0], function(value, key) {
this.push(setimage[i].roleinfo.defaultphotoid);
}, log);
https://docs.angularjs.org/api/ng/function/angular.forEach
try this:
$scope.setimage = res.response.data.profilesearchsnippet;
var images = [];
for (var i = 0; i<$scope.setimage.length; i++){
image.push($scope.setimage[i][0].roleinfo.defaultphotoid);
}
will work.

Parse Objects and Arrays in Json data in AngularJs

I am trying to build an array of objects using the data I get from backend service using angularjs. Here is how I get the data
"mylist": [
{
"name": "Test1",
"moreInfo": {
"moreInfoText": "More test",
},
"companyInfo": {
"companyNameInfo": "ABC",
"url": "http://www.google.com",
}
},
{
"name": "Test2",
"moreInfo": {
"moreInfoText": "More test2",
},
"companyInfo": {
"companyNameInfo": "ABC2",
"url": "http://www.yahoo.com",
}
},
]
I want to parse it so I can combine it all in one array of objects like
[{"name": "Test1", "moreInfoText": "More test","companyNameInfo": "ABC", "url": "http://www.google.com"},{ "name": "Test2", "moreInfoText": "More test2","companyNameInfo": "ABC2", "url": ""}]
Try this:
var flatten = function(object) {
var newObj = {};
for (var key in object) {
var item = object[key];
if (typeof item !== 'object') {
newObj[key] = item;
}
else {
var flattened = flatten(item);
for (var k in flattened) {
newObj[k] = flattened[k];
}
}
}
return newObj;
};
var newList = [];
myList.forEach(function(object) {
newList.push(flatten(object);
});
console.log(newList) //this should be what you want

AngularJs filter is not working correctly

Please see the link
http://plnkr.co/edit/Cn0sNDGEkPOzV19ye8NW?p=preview
I have changed my JSON, then I can't able to filter the data.
var result = $filter('filter')(foo.results, {id:2426})[0];
if you limit on your JSON data
and you can make sure the data property value is num
maybe you can try this ...
var foo = {
"count": 70,
"language": "en",
"0": {
"id": "2420",
"name": "Medical Center"
},
"1": {
"id": "7840",
"name": "Conference Room"
},
"2": {
"id": "2426",
"name": "Deck 5 Starboard"
}
};
foo.results = [];
for(var property in foo){
if(property == parseInt(property, 10).toString()){
foo.results.push(foo[property]);
}
}
var result = $filter('filter')(foo.results, {id:2426})[0];
$scope.name = result.name;
wish help !!

Resources