ReactJS: Map and Find - javascript

Introduction
I have a search filter in which I enter parameters such as sender and recipient.
In this way for the sender I make a call and get results.
For the recipient I make another call and get other results.
So I will have two arrays:
sent
sent1
what I would like to do is iterate through the first arrival comparing each element with each element of the second array and if a certain condition is exceeded save the element of the first array.
Now my Code is:
let filteredSent = {rows: []}
sent.rows.map( (elem) => { sent1.rows.find( (value) => {
if(value.key[2] === values.recipient.Code && value.key[0] === "RECIPIENT" && elem.key[1] === value.key[1] && elem.key[3] === value.key[3] && elem.value === value.value){
filteredSent.rows.push(elem)
}
})
} )
where values.recipient.Code is an input value that I choose from a list.
My problem is that I make this comparison, but I find myself certain results that shouldn't be there.
Is what I'm doing the map and find a wrong use?
Thank you all for your time.

Related

Searching Array of Objects with two search values

so I am trying to make an app that has two search criterias. The front-end app basically fetches data and you have two search bars to filter out the incoming data.
One search is by name and the other is by school name, the tricky part is that the either of the search also takes into account if there is some value in the other search parameter.
For example, if you search for "California University" and "Bob", you should get only Bobs that go to California University to render on the screen. But it seems like right now my DOM only renders the most recent search Ive made. What is the best way to go about a filter that filters both student name and school name using an event listener (keyup) on the search inputs?
searchByNameInput.addEventListener("keyup", (e) => {
const filterNameArray = studentArray.filter((student) => {
// code here to filter students with this name and render it on DOM
}
}
searchBySchoolName.addEventListener("keyup", (e) => {
//filters students who go to this school and render it on DOM
}
}
Write a single filtering function that checks both inputs, and call it from both event listeners.
function filterStudents() {
const nameFilter = searchByNameInput.value;
const schoolFilter = searchBySchoolName.value;
const filterArray = studentArray.filter(student =>
(nameFilter == '' || student.name.includes(nameFilter) &&
(schoolFilter == '' || student.school.includes(schoolFilter))
}
searchByNameInput.addEventListener("keyup", filterStudents);
searchBySchoolNameInput.addEventListener("keyup", filterStudents);
first filter your object and please try it include() method instead of filter().
as a above example
here filterData is my new filtered object and stu_data is my array.
get all search value from search input.
Example:-
var filterData = stu_data.filter((stu_filter) => {
return (stu_filter.firstname.toLowerCase().includes(filter) ||
stu_filter.lastname.toLowerCase().includes(filter))})
I hope this is help for you!
happy coding :)

Copy array by value is not working in foreach in Angular

I am trying to copy array in element of array and manipulate as per my need.
this.question?.labels.forEach((element) => {
element["options"] = [...this.question?.options]; // I've tried json.stringify() as well but not worked
element["options"].forEach(e => {
e.checked = this.question?.userAnswers.find(i => e.key === i.value && element.key === i.key) ? true : false;
});
});
I've used [...this.question?.options] to get new copy of option every time. but it always saved last value of array in each element
https://stackblitz.com/edit/pass-array-by-value?file=src%2Fapp%2Fapp.component.ts
I do see the right options array populated under each label element. Just formatted the json using pre tag in html to get a better view.
Stackblitz - https://stackblitz.com/edit/pass-array-by-value-b1f6aq?file=src/app/app.component.ts

taking one element of each type from a list and only taking the first occurrence not working as expected

So this function is supposed to pars an array of object and only take the first occurrence of each type of object there is like 4 different ones and each ones has a dozen of entries.
LastReadingOfEach(measurements){
let devicesList = []
devicesList.push(measurements.map((device) => {if (!devicesList.includes(device.Type) ) { return device} else console.log("removed :")}))
console.log("devicesList : ", devicesList)
}
I created an array and thought I could just push in a device of each type, but I think it doesn't compare against the array as it fill up because it at the end I find the devicesList has all elements on measurements
I believe what you're looking for can be done with a reducer and findIndex. If find index fails to match the device type it will return -1, we know that we don't have that type yet so it gets pushed into the aggregation.
LastReadingOfEach(measurements){
const devicesList = measurements.reduce((agg, device) => {
if (agg.findIndex(x => x.type === device.type) === -1) {
agg.push(device)
}
return agg
}, [])
console.log("devicesList : ", devicesList)
}

Check Duplicate Nested Object Values within Single Array of Objects

Some how it becomes very complicated for me to achieve this.
// HERE WE WILL ARE CREATING THE ARRAY OF OBJECTS
let getAllAssetsData = [];
$('#productList tr').not(':first').each(function(index,value){
let getPT = $(this).find('.dropdown').val();
let getPm = $(this).find('.pm').val();
let getSn = $(this).find('.sn').val();
getAllAssetsData.push({
'pt' : getPT,
'pm' : getPm,
'sn' : getSn,
});
});
// THIS IS WHAT I AM TRYING
$(getAllAssetsData).each(function(index1,value1){
$(getAllAssetsData).each(function(index2,value2){
// HERE I NEED TO COMPARE ALL ARRAY VALUES TO EACH OTHER AND MAKE DUPLICATE VALIDATION
if((value1.pt === value2.pt) && (value1.pm === value2.pm) && (value1.sn === value2.sn)){
// HERE DUPLICATE ELEMENT FOUND
console.log(value1.pt);
console.log(value2.pt);
console.log(value1.pm);
console.log(value2.pm);
console.log(value1.sn);
console.log(value2.sn);
alert('asd');
}
});
});
On Click on add button it is very easier for me to only add unique set of values, but if user have rights to change values after adding into the list, so how do i check duplication on click of "NEXT" button ?

How to reset filter in Angular 4

Currently I have multiple filters. I would like to reset the filter however, its not working.
showOnlyMyRequest(){
this.requests = this.requests.filter(request => request.requestedBy === 'John Doe');
}
showAllRequest(){
this.requests = this.requests.filter(request => request.requestedBy === '*');
}
In the above example the showAllRequest() doesn't reset the previous filter.
From MDN web doc :
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
So your requests array becomes the one .filter() returned, you should stock your filtered values somewhere else if you need to either show your requests filtered or not.
In case if you decide to send a parameter to your search function from your search text field you can try this.
let me know if there is a better solution so I can improve myself too. Thanks in advance.
showAllRequest(parameter1){
if (parameter1 === '') {
/* call getmethod or the whole array variable got during get mehtod
*/
}
this.requests = this.requests.filter(request => request.requestedBy
=== '*');
}
coded version below :
filterform(search) {
if (search === '') {
this.getFormSettings();
}
this.requests = this.requests.filter(request =>
request.requestedBy.indexOf(search.toLowerCase()) >= 0 === '*');
}

Categories