AngularJS: Read array with variables with dot - arrays

I have the following result from database (using sequelize), the result:
JSON from the database:
[{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]
In the controller I do the next:
vm.spareParts = data.data;
console.log('-------- ' + JSON.stringify(vm.spareParts));
for (var i = 0; i < vm.spareParts.length; i++) {
vm.items.push({
codSparePart: vm.spareParts[i].codSparePart,
name: vm.spareParts[i].name,
description: vm.spareParts[i].description,
available: vm.spareParts[i].available,
codManufacturer: vm.spareParts[i].codManufacturer,
nameManufacturer: vm.sparePart[i]['manufacturer.description'],
stock: vm.spareParts[i].stock,
codProject: vm.spareParts[i].codProject
});
All is working perfectly except in reading the variables with dot.
nameManufacturer: vm.sparePart[i]['manufacturer.description']
nameManufacturer: vm.sparePart[i]['manufacturer.name']
How can I read these variables, I'm looking for the solution but I think is too simple and there is not any information about it.
Thanks in advance for your help.

You have a typo, change vm.sparePart to vm.spareParts
This line: nameManufacturer: vm.sparePart[i]['manufacturer.description'],

Your code seems to be working perfectly:
const array =[{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]
console.log(array[0]['manufacturer.description']);
More information: How to get JSON objects value if its name contains dots?

Related

how to cast json to array in angular

I have file1.ts which has a variable menuGroups
menuGroups= Array();
i populate menuGroups with data so it looks something like this
i store this in localstorage : localStorage.setItem("menuGroups", JSON.stringify(this.menuGroups));
now in file2.ts, i want to assign above localstorage value to an array variable at class level. how to achieve it?
basically , in file2.html, i want to loop through the array menuGroups variable using ngFor.
i tried this in file2.ts but getting error
menuGroups: any[] = [];
populateNavBar() {
this.menuGroups = JSON.parse(localStorage.getItem('menuGroups')) || [];
}
Working ex
If you try to change
menuGroups: any;
Show run example stackblitz here.

Can't Figure Out Correct Protractor Syntax

I'm building Protractor tests and am seeking to refactor some code.
This line of code works fine:
element.all(by.css('div.modal-content ly-input input')).first().sendKeys("Workflow 1");
Then I tried doing this:
var name = element(by.css('div.modal-content ly-input'));
element(name).all(by.css('input')).sendKeys("Workflow 1")
There is something wrong with this last line of code, as I'm getting a "Failed: Invalid locator" error.
Might anyone know how I can introduce the name variable and use it in the last line of code?
Robert
My apologies, I finally figured it out:
name.all(by.css('input')).sendKeys("Workflow 1")
I see you figured it out. Here is how I write my sendKeys in order to have more clarity when I read my code:
var name = 'Workflow 1';
var input = element.all(by.css('div.modal-content ly-input'));
input.first().sendKeys(name);
// or if you have multiple inputs in that element above
var workflowData = {
name: 'Workflow 1',
description: 'a workflow',
type: 'simple'
}
var input = element.all(by.css('div.modal-content ly-input'));
input.get(0).sendKeys(workflowData.name);
input.get(1).sendKeys(workflowData.description);
input.get(2).sendKeys(workflowData.type);

How to acces array object in nodejs?

I am getting this req in my req.body . I want to parse the details .Below is my req.
"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
I want output like
number:
cmdb_ci:
category:
How do i parse this array object in nodejs. Please help
Use JSON.parse() like this:
var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
console.log('number : ' + aObjList[i].number);
console.log('cmdb_ci : ' + aObjList[i].cmdb_ci);
console.log('category : ' + aObjList[i].category);
}
You Can Use
JSON.parse(req.body);
This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.
You can use
var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;

Jade variable from array // not working

I'm passing an array to the jade document. Then I'd like to access the values of the array via a variable to keep the markup simple. Just see the example below. I have already picked up, that the jade syntax can be quite strange dealing with arrays (stuff like "arr.[0]"). Can you guys tell me what im overseeing here? Big thanks!
- var arr = [
{
name: 'foo',
id: 1
},
{
name: 'bar',
id: 2
}
]
- var item = arr[0];
h2 #{item.id} // doesn't work
h2 #{arr[0].id} // works
h2 #{arr[0].id}
Works because you are referencing the id for arr item in the '0' (first) position. This is because the array starts counting things with a zero, not a one.
As you can probably see from your results, this code would return '1', which means that you could expect h2 #{arr[0].name} To return 'foo'.
To get the ids from both items in the array 'arr', change your code to this.
h2 #{arr[0].id}
h2 #{arr[1].id}

Array Declaration in CFScript

Please consider the following documentation:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=CFScript_11.html#1161053
I'm studying the description of following code:
<cfscript>
//Set the variables
acceptedApplicants[1] = "Cora Cardozo";
acceptedApplicants[2] = "Betty Bethone";
acceptedApplicants[3] = "Albert Albertson";
rejectedApplicants[1] = "Erma Erp";
rejectedApplicants[2] = "David Dalhousie";
rejectedApplicants[3] = "Franny Farkle";
applicants.accepted=acceptedApplicants;
applicants.rejected=rejectedApplicants;
rejectCode=StructNew();
rejectCode["David Dalhousie"] = "score";
rejectCode["Franny Farkle"] = "too late";
The description says that, "Creates two one-dimensional arrays, one with the accepted applicants and another with the rejected applicants. "
I'm new to ColdFusion and I don't see any array declaration using the array keyword in the above code just like the following doc:
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=arrayStruct_03.html#1121128
Please explain.
You need to initialize your acceptedApplicants and rejectedApplicants arrays
There should be
acceptedApplicants = [];
rejectedApplicants= [];
somewhere above in the code. Like Peter said, if this is in a function make sure you var those variables.

Resources