My service give result as follows
$scope.ListOfPeople = [
{ PersonID: 10, FirstName: "John", LastName: "Smith", Sex: "Male" },
{ PersonID: 11, FirstName: "James", LastName: "Last", Sex: "Male" },
{ PersonID: 12, FirstName: "Mary", LastName: "Heart", Sex: "Female" },
{ PersonID: 13, FirstName: "Sandra", LastName: "Goldsmith", Sex: "Female" },
{ PersonID: 14, FirstName: "Shaun", LastName: "Sheep", Sex: "Male" },
{ PersonID: 15, FirstName: "Nicola", LastName: "Smith", Sex: "Male" }
];
I need to customise my datasource as follows
$scope.output= [
{ PersonID: 10, FirstName: "John" },
{ PersonID: 11, FirstName: "James" },
{ PersonID: 12, FirstName: "Mary" },
{ PersonID: 13, FirstName: "Sandra" },
{ PersonID: 14, FirstName: "Shaun" },
{ PersonID: 15, FirstName: "Nicola" }
];
What is the best way to do this in angularjs
You can use map to achieve your goal. Take in account that the map creates new array.
$scope.output = $scope.ListOfPeople.map(function(item){
return {
PersonID: item.PersonID,
FirstName: item.FirstName
};
});
I don't think you need to create a new array to reshape it's elements. Just delete unwanted properties from your elements.
//Iterate through the array
$scope.listOfPeople.forEach(function(obj){
//Iterate through properties
for (var property in obj) {
if (['PersonId', 'FirstName'].indexOf(property) < 0) //Delete anything you don't name here
delete obj[property];
}
})
Related
This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 9 months ago.
Does someone know how to write this code?
On clicking the button, create a new table from people table, which will only contain people whose age is greater than 18 . Display the resulting array in the console
const people = [
{
firstname: "Dreddy",
lastname: "Nussgen",
age: 70,
},
{
firstname: "Yves",
lastname: "Sear",
age: 49,
},
{
firstname: "Marcel",
lastname: "Cowderay",
age: 59,
},
{
firstname: "Dag",
lastname: "Binnall",
age: 31,
},
{
firstname: "Horten",
lastname: "Claesens",
age: 75,
},
{
firstname: "Charmian",
lastname: "Harnes",
age: 10,
},
{
firstname: "Sean",
lastname: "Littlejohns",
age: 37,
},
{
firstname: "Hazlett",
lastname: "Sprouls",
age: 87,
},
{
firstname: "Marcel",
lastname: "Hasted",
age: 66,
},
{
firstname: "Cary",
lastname: "Summerson",
age: 15,
},
{
firstname: "Feodor",
lastname: "Ollett",
age: 56,
},
{
firstname: "Kelly",
lastname: "Ranahan",
age: 62,
},
{
firstname: "Madelene",
lastname: "Davie",
age: 14,
},
{
firstname: "Bent",
lastname: "Karpe",
age: 82,
},
{
firstname: "Reinaldo",
lastname: "Grimbleby",
age: 81,
},
];
You can use Array.prototype.filter() combined with Destructuring assignment
Code:
const people = [{firstname: 'Dreddy',lastname: 'Nussgen',age: 70,},{firstname: 'Yves',lastname: 'Sear',age: 49,},{firstname: 'Marcel',lastname: 'Cowderay',age: 59,},{firstname: 'Dag',lastname: 'Binnall',age: 31,},{firstname: 'Horten',lastname: 'Claesens',age: 75,},{firstname: 'Charmian',lastname: 'Harnes',age: 10,},{firstname: 'Sean',lastname: 'Littlejohns',age: 37,},{firstname: 'Hazlett',lastname: 'Sprouls',age: 87,},{firstname: 'Marcel',lastname: 'Hasted',age: 66,},{firstname: 'Cary',lastname: 'Summerson',age: 15,},{firstname: 'Feodor',lastname: 'Ollett',age: 56,},{firstname: 'Kelly',lastname: 'Ranahan',age: 62,},{firstname: 'Madelene',lastname: 'Davie',age: 14,},{firstname: 'Bent',lastname: 'Karpe',age: 82,},{firstname: 'Reinaldo',lastname: 'Grimbleby',age: 81,},]
const result = people.filter(({ age }) => age > 18)
console.log(result)
So I have here two different arrays.
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
What I want is to get the same values from the two arrays using ES6 (filter if possible) or in any way that fits to achieve the output. The result that I want to get is :
[{ firstName: "John", lastName: "Doe" }]
You can try using Array.prototype.filter() and Array.prototype.some()
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
const res = test.filter(t1 => test2.some(t2 =>
t1.firstName == t2.firstName
&& t1.lastName == t2.lastName
));
console.log(res);
Thi swill still works if you have object with properties other than firstName and lastName
const test = [
{ firstName: "John", lastName: "Doe" },
{ firstName: "Michael", lastName: "Sins" },
{ firstName: "Alex", lastName: "Brown" }
];
const test2 = [
{ firstName: "Lisa", lastName: "Shore" },
{ firstName: "John", lastName: "Doe" },
{ firstName: "Justin", lastName: "Park" },
];
const tempTest2 = test2.map(item => JSON.stringify(item));
const result = test.filter(item => tempTest2.includes(JSON.stringify(item)));
console.log(result);
You can do:
const test = [{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Michael', lastName: 'Sins' },{ firstName: 'Alex',lastName: 'Brown' }]
const test2 = [{ firstName: 'Lisa', lastName: 'Shore' },{ firstName: 'John', lastName: 'Doe' },{ firstName: 'Justin',lastName: 'Park' }]
const getFullName = ({ firstName, lastName }) => firstName + lastName
const test2Names = test2.map(getFullName)
const result = test.filter((o) => test2Names.includes(getFullName(o)))
console.log(result)
There is a thatre establishment that has a number of theatre groups. Each groups is either international or not. For some reason it is necessary that each international group has at least one female actor , aka. actress. If there exists even one international group without any female actor, function checkGenderEquality must return false.
Theatre: [
{
groupNmae: 'Medea',
id: 1001,
international: false,
actors: [
{
firstName: 'Vilfrid',
birthDate: '1981-01-01',
gender: 'Male',
},
{
firstName: 'Nils',
birthDate: '1973-10-11',
gender: 'Male'
},
{
firstName: 'Valentina',
birthDate: '2001-05-09',
gender: 'Female'
}
]
}
,
{
groupNmae: 'Hamlet',
id: 2001,
international: true,
actors: [
{
firstName: 'Robin',
birthDate: '1999-07-20',
gender: 'Male'
},
{
firstName: 'Johannes',
birthDate: '1998-12-13',
gender: 'Male'
},
{
firstName: 'Ludwig',
birthDate: '1965-03-22',
gender: 'Male'
}
]
},
{
groupNmae: 'King Lear',
id: 3001,
international: true,
actors: [
{
firstName: 'Kristina',
birthDate: '1977-04-19',
gender: 'Female'
},
{
firstName: 'Pernilla',
birthDate: '1974-02-03',
gender: 'Female'
},
{
firstName: 'Bruno',
birthDate: '1970-02-23',
gender: 'Male'
}
]
},
{
groupNmae: 'Medea',
id: 4001,
international: false,
actors: [
{
firstName: 'Alfons',
birthDate: '1986-11-01',
gender: 'Male',
},
{
firstName: 'Ulrik',
birthDate: '1979-11-01',
gender: 'Male'
},
{
firstName: 'Oskar',
birthDate: '2000-10-10',
gender: 'Male'
}
]
}
]
public checkGenderEquality(theatreGroup: any[]) {
let equality = theatreGroup.every(({ t }) =>
(
(t.international != false) || (t.some((a) => a.gender == 'Female')))
);
return equality;
}
Currently checkGenderEquality returns always true. How can I modify to get the correct result (which in this case is false; becuse Hamlet is an international group but has no female actor)?
If the group.international === false, you don't need to check the actors. if the group.international === true, you need to check actors.
Updated: replace filter with every and some
const theatre = [
{
groupNmae: "Medea",
id: 1001,
international: true,
actors: [
{
firstName: "Valentina",
birthDate: "2001-05-09",
gender: "Female",
},
],
},
{
groupNmae: "Hamlet",
id: 2001,
international: true,
actors: [
{
firstName: "Ludwig",
birthDate: "1965-03-22",
gender: "Male",
},
],
},
];
const isEquality = theatre.every(
item =>
(item.international === true &&
item.actors.some(actor => actor.gender === "Female")) ||
item.international === false
);
console.log(isEquality);
I'm using lodash find, and on my test, it is only returning one result, is this the expected response? how to find all instances?
var users = [
{ firstName: "John", lastName: "Doe", age: 28, gender: "male" },
{ firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
{ firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
{ firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
];
var encontre = _.find(users, { lastName: "Doe" })
console.log("usuario encontre::", encontre)
response
usuario encontre:: { firstName: 'John', lastName: 'Doe', age: 28,
gender: 'male' }
so how to seethe 2 users with lastName: Doe?
thanks
Try with _.filter as _.find returns the first matched element.
_.filter will return an array of all matched elements.
var users = [
{ firstName: "John", lastName: "Doe", age: 28, gender: "male" },
{ firstName: "Jane", lastName: "Doe", age: 5, gender: "female" },
{ firstName: "Jim", lastName: "Carrey", age: 54, gender: "male" },
{ firstName: "Kate", lastName: "Winslet", age: 40, gender: "female" }
];
var encontre = _.filter(users, { lastName: "Doe" })
console.log("usuario encontre::", encontre)
jsfiddle for ref : https://jsfiddle.net/c_Dhananjay/b6ngxhvp/
I'd like create json with this structure inside the cycle:
{ id_foto:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ] }
I wish it were a variable id_foto
so that:
if (id_foto == n.foto_moderata) {
// add new { firstName: 'Anna', lastName: 'Smith' }
} else {
// create new "node" like
{ id_foto(NEW INDEX):
[ { firstName: 37, lastName: 'Doe' },] }
}
The Final result like:
{ 10:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ]
11:
[ { firstName: fff, lastName: 'fff' },
{ firstName: 'fff', lastName: 'fff' } ]
}
Then take all user of 11 index
One way to achieve a sequential ids for your data is to create:-
1. a place to store the current value of your id,
2. a function to increment and return your serial
You could store the current id value in your data object like so:-
{
seq : 11,
10:
[ { firstName: 37, lastName: 'Doe' },
{ firstName: 'Anna', lastName: 'Smith' },
{ firstName: 'Peter', lastName: 'Jones' } ],
11:
[ { firstName: fff, lastName: 'fff' },
{ firstName: 'fff', lastName: 'fff' } ]
}
and then use the following to increment & return next sequence id
function id_foto() {
return ++your_object.seq;//get,increment and return incremented value
}