AngularJS forEach issue - angularjs

I already have this foreach that sets the selected value for the state in statesList correctly for a _searchParam[key] that == the state.id.
However, if the _searchParam[key] contains more than one value in this format" "NH,TX" then the if statement fails and the match never occurs so all items get selected set to false.
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
if(state.id == _searchParams[key]) {
state.selected = true;
} else {
state.selected = false;
}
this.push(state);
if(state.selected)
$scope.states.push(state);
}, _states);
So, I tried to split the _searchParams[key] value and loop through them but it is not working. See error in comment
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
var sp = _searchParams[key].split(',');
for(i = 0; i < sp.length; i++) {
if(state.id == sp[i]) {
state.selected = true;
} else {
state.selected = false;
}
}
}
this.push(state); // get unexpected token this
if(state.selected)
$scope.states.push(state);
}, _states);
So, I moved the this.push(state) line into the if (where it really should be anyway), and get the error below.
var _states = [];
angular.forEach($scope.statesList, function (state, key2) {
if(_searchParams[key].indexOf(state.id) > -1)
var sp = _searchParams[key].split(',');
for(i = 0; i < sp.length; i++) {
if(state.id == sp[i]) {
state.selected = true;
} else {
state.selected = false;
}
}
this.push(state);
}
if(state.selected) // get unexpected token if
$scope.states.push(state);
}, _states);
Now how can I fix this as I'm not sure what is going on in this forEach() that would be causing these errors?
The statesList is defined like this:
$scope.statesList = [
{ "id": "AK", "name": "Alaska", "selected": false},
{ "id": "AL", "name": "Alabama", "selected": false},
// SNIPPED most of states
{ "id": "WY", "name": "Wyoming", "selected": false},
{ "id": "DC", "name": "District of Columbia", "selected": false},
{ "id": "MX", "name": "Mexico", "selected": false}];

It appears that you are missing an opening curley brace {. Add it, like this:
if(_searchParams[key].indexOf(state.id) > -1) {
And then take out the split part you added. Otherwise, in some cases you'd set state.selected to true then to false.

Related

how to add 2 element in nested array from one root element using JOLT specification?

Input:
{
"k1": "v1",
"k2": "v2",
"event": "SUMMARY"
}
Expected output:
{
"k1": "v1",
"k2": "v2",
"event": "SUMMARY",
"arr": [
{
"k1": "v1",
"key": "first"
},
{
"k2": "v2",
"key": "second"
},
{
"summary1": "s1",
"key": "SUMMARY"
},
{
"summary1": "s2",
"key": "SUMMARY"
}
]
}
For each 'k1' and 'k2', respective array element should be added as
{
"k2": "v2",
"key": "second"
}
For when event = "SUMMARY", respective 2 elements should be added as
{
"summary1": "s1",
"key": "SUMMARY"
},
{
"summary2": "s2",
"key": "SUMMARY"
}
Please help with JOLT specification
Since you gave input and output based on that i written
one function in javascript.
{
"k1": "v1",
"k2": "v2",
"event": "SUMMARY"
}
Well that function will work only for above input
and also you have to mention that whether u want output in which language...
//Input
var obj = {
"k1": "v1",
"k2": "v2",
"event": "SUMMARY"
}
//funciton calling
obj["arr"] = checkThis(obj);
//output
console.log(obj)
function checkThis(obj) {
var arr = [];
Object.keys(obj)
.forEach(function eachKey(key) {
if (key == "k1") {
var tempObj = {}
tempObj["k1"] = obj[key];
tempObj["key"] = "first";
arr.push(tempObj)
} else if (key == "k2") {
var tempObj = {}
tempObj["k2"] = obj[key];
tempObj["key"] = "second";
arr.push(tempObj)
} else if (key == "event") {
var tempObj1 = {}
var tempObj2 = {}
tempObj1["summary1"] = "s1";
tempObj1["key"] = obj[key];
tempObj2["summary1"] = "s2";
tempObj2["key"] = obj[key];
arr.push(tempObj1)
arr.push(tempObj2)
}
});
return arr;
}

Highlight values angularjs

I would like to get the results like the following manner,
Expected work flow chart,
Here the default view,
Scenario 1: When click on the number "1" all the left to right and children need to highlight like the below,
Scenario 2: Considering the scenario 1 results, click on the number "3" all the left to right and children need to remove highlight like the below since 3 we consider as parent/root,
Scenario 3: Considering the default view, By default there is no selection When click on the number "18" all the parent values need to highlight like the below,
Scenario 4: Considering the Scenario 3 results , When click on the number "18" only for 18 the highlight need to be removed and like the below, Right to left parent level deselection not required for any value.
Note: While clicking on any value Right to left parent level deselection not required. In this case only clicked value need to remove highlight.
Here is the code: JSFiddle
$scope.setActiveSelectedItem = function() {
return $scope.groupOfCheckboxes[i].RowValues[j].isActive = !$scope.groupOfCheckboxes[i].RowValues[j].isActive;
}
$scope.setActivePrevNext = function(arr, id) {
let keys = Object.keys(arr);
let nextIndex = keys.indexOf(id) +1;
let nextItem = keys[nextIndex];
return $scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive = !$scope.groupOfCheckboxes[i].RowValues[nextIndex].isActive;
}
$scope.setBinary = function (id) {
for(i=0; i<$scope.groupOfCheckboxes.length; i++) {
for(j=0; j<$scope.groupOfCheckboxes[i].RowValues.length; j++) {
if($scope.groupOfCheckboxes[i].RowValues[j].td == id) {
$scope.setActiveSelectedItem();
$scope.setActivePrevNext($scope.groupOfCheckboxes, id);
}
}
}
}
});
Html
<table>
<tr ng-repeat="checkbox in groupOfCheckboxes track by $index" ng-init="rowId = $index">
<td ng-repeat="data in checkbox.RowValues track by $index" ng-init="vId = $index">
<span ng-if="data.show" ng-click="setBinary(rowId,vId,data)"
ng-class="data.isActive ? 'active' : ''">{{data.td}}</span>
</td>
</tr>
</table>
Controller
$scope.groupOfCheckboxes = [
{
Row: "1",
RowValues: [
{ td: "1", show: true },
{ td: "2", show: true },
{ td: "3", show: true },
{ td: "4", show: true },
{ td: "5", show: true }
]
},
{
Row: "2",
RowValues: [
{ td: "6", show: false },
{ td: "7", show: false },
{ td: "8", show: false },
{ td: "9", show: true },
{ td: "10", show: false }
]
},
{
Row: "3",
RowValues: [
{ td: "11", show: false },
{ td: "12", show: false },
{ td: "13", show: true },
{ td: "14", show: true }
]
},
{
Row: "4",
RowValues: [
{ td: "15", show: false },
{ td: "16", show: false },
{ td: "17", show: false },
{ td: "18", show: true }
]
}
];
// Setting Parents
for (let rowIndex = $scope.groupOfCheckboxes.length - 1; rowIndex >= 0; rowIndex--) {
for (let valIndex = $scope.groupOfCheckboxes[rowIndex].RowValues.length - 1; valIndex >= 0; valIndex--) {
$scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].isActive = false;
if ($scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].show == true) {
loop4:
for (let rx = rowIndex; rx >= 0; rx--) {
for (let vx = valIndex - 1; vx >= 0; vx--) {
if ($scope.groupOfCheckboxes[rx].RowValues[vx].show == true) {
$scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pri = rx;
$scope.groupOfCheckboxes[rowIndex].RowValues[valIndex].pvi = vx;
break loop4;
};
}
}
};
}
}
$scope.setBinary = function (rowId, vId, data) {
data.isActive = !data.isActive
bool = data.isActive;
loopx:
for (let row = rowId; row < $scope.groupOfCheckboxes.length; row++) {
loop1:
for (let v = vId; v < $scope.groupOfCheckboxes[row].RowValues.length; v++) {
if (row == rowId) {
if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
$scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
} else {
break;
};
} else {
if (v == vId) {
if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
break loopx;
}
} else {
if ($scope.groupOfCheckboxes[row].RowValues[v].show == true) {
$scope.groupOfCheckboxes[row].RowValues[v].isActive = bool;
}
}
}
}
}
function rec(pri, pvi) {
if ($scope.groupOfCheckboxes[pri]) {
$scope.groupOfCheckboxes[pri].RowValues[pvi].isActive = true;
x = $scope.groupOfCheckboxes[pri].RowValues[pvi]
rec(x.pri, x.pvi)
}
}
rec(data.pri, data.pvi)
}

Get data from complex Json structure using NodeJs

json Structure:
{
"id" : "1",
"Data" : [
{
"name" : "abc",
},
{
"name" : "option1",
"position" : [
{
"name" : "option1",
"status" : [
{
"code" : "0",
"value" : "OFF"
},
{
"code" : "1",
"value" : "ON"
}
]
}]
} ]
}
Here,I want to get the data from above complex Json structure.How to do that,
Have tried below code but giving error like;
error: uncaughtException: Cannot read property 'status' of undefined
function myfunc(req,res){
var collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
if(doc != null)
{
var fdata = [];
for(var i =0;i<doc.Data.length;i++){
fdata.push(doc.Data[i].position.status);
}
console.log("fdata............",fdata);
}
});
}
Please help with the same.
You can use foreach for prevent length undefined.
function myfunc(req,res)
{
let collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc)
{
if(doc != null)
{
let fdata = [];
for(let i in doc.Data)
{
for(let j in doc.Data[i].position)
{
fdata.push(doc.Data[i].position[j].status);
}
}
console.log("fdata............", fdata);
}
});
}
#MikaelLennholm have right, for(let i in doc.Data) works but not recommanded, be careful not to use it in prototyped or object-built arrays.
EDIT:
function myfunc(req,res)
{
db.collection('col1').find({}).each(function(err, doc)
{
if(err)
{
console.log('[INFOS] Request fail, more details:\n', err);
}
else if(doc)
{
let fdata = [];
if(doc.Data && doc.Data.length)
{
for(let i = doc.Data.length-1; i >= 0; i--)
{
if(doc.Data[i].position && doc.Data[i].position.length)
{
for(let j = doc.Data[i].position.length-1; j >= 0; j--)
{
if(doc.Data[i].position[j].status)
{
fdata = fdata.concat(doc.Data[i].position[j].status);
}
}
}
}
}
console.log("[INFOS] Datas:\n", fdata);
}
});
}
im a newbie at nodejs, hope this's correctly
//I assume this is a object or you can convert from string to object
var data = {
"id": "1",
"Data": [
{
"name": "option1",
"position": [
{
"name": "option1",
"status": [
{
"code": "0",
"value": "OFF"
},
{
"code": "1",
"value": "ON"
}
]
}]
}]
}
var statusArr = data.Data[0].position[0].status;
console.log(...statusArr);
Result: { code: '0', value: 'OFF' } { code: '1', value: 'ON' }
Parse through position array as well
function myfunc(req,res){
var collectionname = db.collection("col1");
collectionname.find({}).each(function(err, doc) {
if(doc != null)
{
var fdata = [];
for(var i =0;i<doc.Data.length;i++){
for(var j =0;j<doc.Data[i].position.length;j++){
fdata.push(doc.Data[i].position[j].status);
}
}
console.log("fdata............",fdata);
}
});
}

Convert JSON.STRINGIFY to json array length is not calculating

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);
}

Protractor test not executing

I have a protractor test. I want to provide some data for a test so i can generate tests automatically.
My function is as below. The problem is that i can console log something after the opening of describe. But this is'nt the case after the it function.
The code:
bigTestFunction = function(testElements) {
testElements = JSON.parse(testElements);
for (i = 0; i < testElements.length; i++) {
var title = testElements[i].title;
var shouldText = testElements[i].should
var url = testElements[i].url;
var actions = testElements[i].action;
describe(title, function() {
it(shouldText, function() {
goToUrl(url);
for (x = 0; x < actions.length; x++) {
var action = actions[x].action;
var value = actions[x].value;
var element = actions[x].element;
var notEmpty = actions[x].notEmpty;
var nested = actions[x].nested;
if (action === 'sendKeys') {
sendKey(element, value);
}
if (action === 'click') {
click(element, notEmpty);
if (nested) {
for (x = 0; x < nested.length; x++) {
if (nested[x]['action'] === 'sendKeys') {
sendKey(nested[x]['element'], nested[x]['value']);
}
if (nested[x]['action'] === 'click') {
click(nested[x]['element'], nested[x]['notEmpty']);
}
}
}
}
}
});
});
}
}
testElements = JSON.parse(testElements);
the json:
[
{
"id": 1,
"title": "Small test one",
"should": "should start training",
"url": "https://ledmagazine.nl/home",
"actions": [
{
"id": 1,
"test_id": 1,
"element": "/html/body/div[1]/div/div/header/div/div[2]/div[2]/div/div/div/div/nav/section/ul/li[3]/a",
"action": "click",
"status": "notEmpty",
"value": "/html/body/div[1]/div/div/div[2]/div/div/div[1]/div/section",
"nested": {
"id": 1,
"action_id": 1,
"action": "sendKeys",
"element": "//*[#id=\"mce-EMAIL\"]",
"value": "dennisageffen#hotmail.com",
"created_at": null,
"updated_at": null
}
}
]
}
]
I think i'm really close but the function stops after 'describe(title, function() {...'
Propably you are missing beforeEach(function() {...} to get data.

Resources