This question already has answers here:
Javascript Array.prototype.filter() not working
(1 answer)
Array.Filter not updating array
(2 answers)
Closed 3 years ago.
I haven't had too many opportunities to use filter() yet but I decided to a bit shorten my code, unfortunately It doesn't work as I wish it to be. I have written function to remove element from the array in good old fashion way which works nice:
this.error_log.map((error, i) => {
if (error.message.info == message) {
this.error_log.splice(i, 1)
}
})
it works pretty well actually but I wanted to rewrite it to filter, unfortunately it doesn't work and I have no idea why, could anyone explain me what am I doing wrong here?
this.error_log.filter(error => error.message.info !== message)
Try this:
this.error_log = this.error_log.filter(error => error.message.info !== message);
Related
This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 7 months ago.
How to call a nested function with only a string? eg:
function oot(wha) {
function inn(wha)
{
First.innerHTML+="Inner ["+wha+"]";
}
oot.inn = inn;
Second.innerHTML+="Outer ["+wha+"]";
}
oot("1");
oot.inn("2"); //works okay
window["oot"]("3"); //works okay
window["oot.inn"]("4"); //<The problem, doesn't work.
window["oot"]["inn"]("4"); //Works, thanks.
Edited to make the code more readable, and show a solution.
IF there is no way to make this work with a single string i can probably make do, but i will leave the question unanswered for a few hours to see if there is another solution.
You can reference nested Objects like this:
window["oot"]["inn"]("4");
or
window.oot.inn("4")
This question already has answers here:
Expected to return a value in arrow; function array-callback-return. Why?
(7 answers)
Closed 4 years ago.
static getDerivedStateFromProps(nextProps, prevState) {
const { claimList, claimOpenList } = nextProps
const claimsNew = [...claimList]
console.log('getDerivedStateFromProps > claimsList > ', claimList)
claimOpenList.map(item => {
const value = claimList.find(val => String(val.claimid) === String(item.claimid))
if (value === undefined) claimsNew.push(item)
})
return {
claims: claimsNew,
}
}
it works and do what is suppoused to do, but i want to get rid of this warning "Expected to return a value at the end of arrow function array-callback-return" it says the problem is on the line claimOpenList.map(item => { i really want to learn how to solve this warnings since its not the only place that it appears,i belive that understanding this one im going to be able to figure out the other ones, thanks for your time and help
i have looked another similar problems on stack but they dont seems to be the same as mine
map() is used to create a new array based on iterating another array. You are only using it to loop over an existing array.
Just change map() to forEach() which is intended for that purpose
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 7 years ago.
I get an array of objects from the backend as this one, currently only three elements but in the future will be more.
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}]
What is the most efficient way in javascript to find an element in this array by code, and return the text associated? Can we use lambdas expresions here?
you can use array filter to find the nested object(s) that matches your code property:
var arr =
[{"code":"lng.dropdown.home","text":"Inicio"},
{"code":"lng.dropdown.shop","text":"Tienda"},
{"code":"lng.button.support","text":"Soporte"}];
var res = arr.filter(function(x){
return x.code == 'lng.dropdown.home'
})
console.log(res[0])
[].filter returns an array with the objects that return true inside the callback.
As far as efficiency is concerned for loop are proved to be faster but using higher order methods you earn abstraction .
When not using other libraries, I usually map then find the indexOf, as follows:
data[data.map(function(a){ return a.code }).indexOf("code I am trying to find")];
This returns the element of data at the index where code matches the code you're trying to find.
When I have lodash as a dependency, I usually just do this however:
_.find(data, {code:"code I am trying to find"})
(I think that's the proper syntax)
Edit:
Didn't realize you were already using JQuery, the answer using $.grep is probably best.
This question already has answers here:
Why Array.indexOf doesn't find identical looking objects
(8 answers)
Closed 7 years ago.
I have an angularJS/Typescript application where I am trying to check if an object is already in a current list of objects
if (this.selectedFormatData.indexOf(item) === -1) {
//doesn't exist so add
this.selectedFormatData.push(item);
} else {
this.selectedFormatData.splice(this.selectedFormatData.indexOf(item), 1);
}
I have used this code before and it worked but isn't in this instance. Console output suggests it should work?
Any ideas?
Update: yeah correct looks like a duplicate sorry. I had a previous bit of code where i thought it worked because it was returning 0 instead of -1. Not sure why it would return 0 though
As the comments state, indexOf() is not meant to compare objects. This has been answered before here: Why Array.indexOf doesn't find identical looking objects.