I am trying to retrieve objects from an array and select them individually. When an object is returned my tables cells are fill with that object i.e([object Object]), instead of the properties of that object filling the table. I know I probably need to destructure the returned array to retrieve the individual objects. However, I cannot find a way to destructure the array and send the objects to my html page. This is my code:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', {LName: newArr});
}
Console.log(newArr) returns full array:
[{id:1, firstName: 'John', lastName: 'Doe'},
{id:1, firstName: 'James', lastName: 'Smith'},
{id:1, firstName: 'Jane', lastName: 'Doe'},
{.... ...]
I am trying to get something in return like this:
{id:1, firstName: 'John', lastName: 'Doe'}
{id:2, firstName: 'James', lastName: 'Smith'}
{id:3, firstName: 'Jane', lastName: 'Doe'}
Inside my html page I am trying to iterate over the returned objects and place each object in a single row and the corresponding properties in the other cells of a table.
<table border=1>
<tr>
{{#each LName}}
<td>
{{this}}
</td>
{{/each}}
</tr>
</table>
I found a temporary solution. However, with this solution I now need to create a loop that would give me the output for the res.render(). I create a loop thinking I could place the string in res.render but was given an error. This is what I did:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', {Per1: newArr[0], Per2: newArr[1], Per3:newArr[3] });
}
This is the look I would have used if it worked:
text ="{";
for (;newArr[i];)
{text+="Per"+i+":"+ "newArr["+i+"],";
i++;}
text+='}';
With this loop res.render would look like:
app.get('/lastName/:Name', function(req, res) {
var newArr = foo.lookupByLastName(req.params.Name);
res.type('text/html');
res.render('last', text);
}
Related
my Button in .html
<button (click)="deleteFirst()">Delete First</button>
My array and function in .ts:
persons = [
{surname: "Tom", lastname: "Brown"},
{surname: "Ben", lastname: "Smith"},
{surname: "Alan", lastname: "Black"},
{surname: "Nick", lastname: "White"}]
// this does not work
deleteFirst = () => {
this.persons[0].shift;
}
How can I remove the first / last Object of an array?
shift method does not work that way. You need to call it, preferably on an Array.
Your version should be like this, if you are ok with mutating source Array:
deleteFirst = () => {
this.persons.shift();
}
Im working with a library to create tables in react-pdf. And i want to fill it with api data. Is there a way of iterating inside the data{} and create various objects with data from api. Instead of sth like
data={[{firstName: "John", lastName: "Smith",country: "Australia"}]},
data={[{firstName: "Josh", lastName: "Pattison",country: "USA"}]}
have
data={[{firstName: "John", lastName: "Smith",country: "Australia"}],
[{firstName: "Josh", lastName: "Pattison",country: "USA"}]
}
Code
{Data.attributes.map((details) => (
<TableBody data={[
{firstName: details.attributes.filter(
(x) => x.displayName === "first name"
)[0].value,
lastName: details.attributes.filter(
(x) => x.displayName === "last name"
)[0].value,
country: details.attributes.filter(
(x) => x.displayName === "country"
)[0].value},
]}>
</TableBody>
...
you can use Object.entries() to get a 2D array of your object and use something like forEach or map to have loop over it and create the desired data shape:
const finalResult = {}
Object.entries(apiData).map(([key, value]) => {
// do what ever you want here and fill your desired structure
})
is there any shorthand for something like this?
var data =
{
name: $scope.admin.name,
email: $scope.admin.email,
roles: $scope.admin.roles
};
Usually after i query and input to model i can just use like this:
$scope.admin = {
name: value1,
email: value2,
roles: value3
}
Edited:
My exact question inside var data how can i make it more simple like above without keep typing "$scope.admin".
Thanks
If you do not want a deep copy with angular.copy(), but just want to type less signs in code, you can do
var x = $scope.admin;
var data =
{
name: x.name,
email: x.email,
roles: x.roles
};
If you need to copy all the properties, use angular.copy:
angular.copy($scope.admin, $scope.user)
If you need to pick a subset of properties, a library like lodash might be useful. You would use the pick function:
$scope.admin = {
firstname: 'John',
name: 'Doe',
email: 'john#mycompany.com',
roles: ['sysadmin']
};
$scope.user = _.pick($scope.admin, ['name', 'email', 'roles']);
// -> {name: 'Doe', email: 'john#mycompany.com', roles: ['sysadmin']}
So I have a model in Ember that is generating a hash with three objects. One of the objects is an array of objects with another array inside each object. I need to sort this innermost array, but I am having trouble doing so.
Here are my models.
App.Person = DS.Model.extend ({
first_name: DS.attr('string'),
last_name: DS.attr('string'),
age: DS.attr('string'),
gender: DS.attr('string'),
innerMostArray: DS.hasMany('innerMostObject')
});
App.innerMostObject = DS.Model.extend ({
person_id: DS.belongsTo('person'),
attr1: DS.attr('string'),
attr2: DS.attr('string')
});
Here is my Route
App.NestedArrayRoute = Ember.Route.extend({
model: function(params) {
return Ember.RSVP.hash({
object1: this.store.find('object1', params.object1_id),
people: this.store.all('person'),
object3: this.store.all('object3')
});
},
afterModel: function(model, transition) {
model.people.forEach(function(item, index, enumerable){
var innerMostArray = item.get('innerMostArray');
var sortedArray = innerMostArray.sortBy('attr1', 'attr2');
});
model.people.update();
}
});
I know that I am nowhere near doing this right but I just don't know how to sort this nested array. I've seen examples of array controllers, but I don't know how to use one to sort this nested array. If anyone could give an example of how to do this it would be very helpful. Thank you.
I agree with Kalmans answer, but I suggest you do this sorting with built-in methods to save you trouble:
App.Person = DS.Model.extend({
name: DS.attr('string'),
fruits: DS.hasMany('fruit', {async: true}),
fruitSorting: ['title', 'color'],
sortedFruits: Ember.computed.sort('fruits', 'fruitSorting')
});
I forked his example here: http://emberjs.jsbin.com/manutu/1/edit?html,js,output
One way to do this is to create a computed property on the model as follows:
App.Person = DS.Model.extend({
name: DS.attr('string'),
fruits: DS.hasMany('fruit', { async: true }),
sortedFruits: function(){
var fruits = this.get('fruits');
return fruits.sortBy('title', 'color');
}.property('fruits.#each.title', 'fruits.#each.color')
});
Working example here
I have a table with 3 columns 'firstname', 'lastname', 'birthday', like this:
<table id="table">
<tr>
<td id="firstname">John</td>
<td id="lastname">Smith</td>
<td id="birthday">Jan 1 2014</td>
<tr>
<tr>
<td id="firstname">Bill</td>
<td id="lastname">Matthews</td>
<td id="birthday">Jan 2 2014</td>
<tr>
</table>
I want to create a JSON from this table, like this:
{
firstname: "John",
lastname: "Smith",
birthday: "Jan 1 2014"
},
{
firstname: "Bill",
lastname: "Matthews",
birthday: "Jan 2 2014"
}
I've trying something like this:
var tableRows = [];
element.all(by.tagName('tr')).each( function(element) {
tableRows.push(
{
firstname: element(by.id('firstname')).getText(),
lastname: element(by.id('lastname')).getText(),
birthday: element(by.id('lastname')).getText()
}
);
});
Use map(), quote from the changelog:
Added a map function to element.all to apply a function to each
element and return the result of the transformation.
Resolve promises if there is an object that contains multiple
promises. Added index as a second argument to the map function
callback.
Key point here is that it would resolve multiple promises:
var items = element.all(by.tagName('tr')).map(function (tr) {
return {
firstname: tr.element(by.id('firstname')).getText(),
lastname: tr.element(by.id('lastname')).getText(),
birthday: tr.element(by.id('birthday')).getText()
};
});