Array.indexOf Not working [duplicate] - javascript

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.

Related

Calling a nested function with a string (window[]) in JS [duplicate]

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")

Replace splice with filter [duplicate]

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);

Google script change a value in my array unexpectedly [duplicate]

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].

Javascript Array Equality Fails [duplicate]

This question already has answers here:
How to compare arrays in JavaScript?
(55 answers)
Closed 6 years ago.
I've got two lists. When I run array1 == array2, the console prints false. If I iterate through them and check equality for each item, it prints true every time. What's wrong?
for (var i=0; i<array1.length; i++) {
console.log(array1[i] == array2[i]);
}
From the Safari console:
All my google searches turned up things about array diffs and checking equality of unordered arrays. I thought there must be an easier way to solve the problem of two lists in the same order, and I couldn't find that online.
use array1[i].equals(array2[i]) Like it was stated in the comments you are not comparing the contents with the ==.

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Categories

Resources