JavaScript delete elements in array by a criteria [duplicate] - javascript

This question already has answers here:
Remove array element based on object property
(12 answers)
Closed 5 years ago.
I have an array, and I want to delete elements that have status equal to true (as seen in this part of code)
listArray.forEach((element, index) => {
if (element.status === true) {
listArray.splice(index, 1);
}
});
The problem is that, if, for example, the first, second and third elements have status true, than the second element will not be deleted

Try this:
listArray.filter(element => element.status === false)
You can also do:
listArray.filter(element => !element.status)

try this:
listArray = listArray.filter(element => !element.status);

Related

Uncaught Error in snapshot listener:, [FirebaseError: inequality filter property and first sort order must be the same: __name__ and date] [duplicate]

This question already has answers here:
Firestore: Inequality filter property and first sort order must be the same. Failing where condition and order in different fields. Is this Expected?
(1 answer)
The first sort property must be the same as the property to which the inequality filter is applied
(2 answers)
Firestore query order on field with filter on a different field
(3 answers)
Closed 5 months ago.
I try to get some documents with the onSnpashot method and in the same times apply where (for filter the collection) and orderBy (for sort my results). I make a composite index (with documentId() field, and date) but it don't work... I hope you can help me.
This is my structure
var ColRef = collection(DB_Firestore, "ListConv");
var q = query(ColRef, where(documentId(), "in", ["Oe4VQzzcLh1aKWedt7yU"]), orderBy("date", "desc"));
return onSnapshot(q, (snapshot) => {
if(!snapshot.empty) {
var changes = snapshot.docChanges();
changes.map(change => {
if(change.type == "added") {
var conv = change.doc.data();
conv["uid"] = change.doc.id;
setConv(Conv => [...Conv, conv]);
}
});
setLoadData(false);
} else {
setLoadData(false);
}
})
Also I try to put the inside my hook's Array "Conv"; And get my handler in order to unsubscribe it.
Thank's for all. Good Night !

How to get object keys which have common value in javascript? [duplicate]

This question already has answers here:
Filtering object properties based on value
(8 answers)
Closed 1 year ago.
I've a problem getting object keys by value for example:
issues: {
window_expired: 'Yes',
no_power: 'No',
no_display: 'Yes'
}
I want to get object keys which have 'Yes' Value as: window_expired, no_display
I've tried this method which is working but it's returning the 1st key i want to loop through and get the keys by value:
function getKeyByValue(object, value) {
return Object.keys(object).find((key) => object[key] === value);
}
you should use filter in your case.
Object.keys(issues).filter(e => issues[e] === 'Yes')
Have a good day

JavaScript: Test if input args are arrays; If yes, then merge arrays into one big array [duplicate]

This question already has answers here:
How can I check if an object is an array? [duplicate]
(51 answers)
Closed 3 years ago.
I have the following variables which are arrays:
const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];
const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];
Below I have the following function that uses the variables as input arguments:
function shallowCopy (arrOne, arrTwo) {
if (arrOne.constructor === 'Array'){
return [...arrOne, ...arrTwo];
}
else {
console.log('test this');
}
}
shallowCopy(gumBrands, mintBrands)
I am expecting my code to return:
[ 'orbit',
'trident',
'chiclet',
'strident',
'altoids',
'certs',
'breath savers',
'tic tac' ]
Instead the code runs my else statement and returns: test this
What am I doing wrong?
.constructor does not contain the string "Array" but a reference to the global Array object.
Note that arrays can be subclassed, and their .constructor is different. You might want to consider to check with instanceof Array.

javascript: 'return' is ignored and keeps looping [duplicate]

This question already has answers here:
What does `return` keyword mean inside `forEach` function? [duplicate]
(2 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 4 years ago.
function checkMagazine(magazine, note) {
var map = new Map();
var noteAr = note.split(" ");
var magazineAr = magazine.split(" ")
noteAr.forEach((note) => {
if (map.has(note)) {
map.set(note, map.get(note) + 1)
} else {
map.set(note)
}
});
magazineAr.forEach((word) => {
if (!map.has(word)) {
return "No"
}
});
return "Yes"
}
I'm checking to see if each word of a note is contained in the magazine by first hashing the values of the note and checking them in magazineAr. If the note word does not exist in the hash, then I am return "NOing. However, I keep getting a return "YES" in console.
I've watched it in debugger mode and I see it enter the statement where it hits 'return No' but then it just keeps on going with the forEach loop. I've even tried return false but same thing.
A forEach callback ignores the value that's returned - no matter what it is, every item in the array will be called with the forEach. For what you're trying to do, you should probably use every instead, which checks whether all items in the array fulfill the condition:
const hasEveryWord = magazineAr.every(word => map.has(word));
return hasEveryWord
? "Yes"
: "No";

How to check whether a given string is already present in an array or list in JavaScript? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Javascript - array.contains(obj)
Best way to find an item in a JavaScript Array ?
I want to check, for example, for the word "the" in a list or map. Is there is any kind of built in function for this?
In javascript you have Arrays (lists) and Objects (maps).
The literal versions of them look like this:
var mylist = [1,2,3]; // array
var mymap = { car: 'porche', hp: 300, seats: 2 }; // object
if you which to figure out if a value exists in an array, just loop over it:
for(var i=0,len=mylist.length;i<len;i++) {
if(mylist[i] == 2) {
//2 exists
break;
}
}
if you which to figure out if a map has a certain key or if it has a key with a certain value, all you have to do is access it like so:
if(mymap.seats !== undefined) {
//the key 'seats' exists in the object
}
if(mymap.seats == 2) {
//the key 'seats' exists in the object and has the value 2
}
Array.indexOf(element) returns -1 if element is not found, otherwise returns its index

Categories

Resources