Displaying only the first item from the localStorage data array - javascript

I am trying to build a board that should display the first number from the list but I can't find $.each alternative.
Currently, I have a piece of code that displays all items from the localStorage. Does anyone have an idea of jQuery function that would take an array as argument and callback only the first item? Similar to this one - $.each( array, callback ).
const clients = JSON.parse(localStorage.getItem("data"));
const odontologas = clients.Odontologas;
$.each(odontologas, function(i, item) {
if (item.Būsena === "Eilėje") {
$("#odontologasQueue").append(
`<p class="boardNumber">${item.EilėsNr}</p>`
);
} else {
$("#odontologasQueue").append(`<p class="boardNumber">000</p>`);
}
});

Just needed to add return false to if statement.

So either exit the loop when you find the match or use array.find
var first = odontologas.find(item => item.Būsena === "Eilėje")
// var first = odontologas.find(function(item) { return item.Būsena === "Eilėje" })
console.log(first)

Long story short, you want index of an entry? It's
odontologas.indexOf("Eilėje"); // equals -1 if element isn't found in the array

Related

How can I find duplicate records from array in JS

I am trying to find duplicate records from a given array. I have tried the following function, but it did not work as expected, later I come to know I was comparing the same record with itself. I have written the following function.
identifyDupliateRecords(verifiedRecordsAll) {
let duplicateRecords : any;
if(verifiedRecordsAll.length > 1){
const tempDuplicateRecords = []
this.verifiedRecordsAll.forEach((record)=> {
if(verifiedRecordsAll.Some === verifiedRecordsAll.Some ||
verifiedRecordsAll.Gum === verifiedRecordsAll.Gum ||
verifiedRecordsAll.Thing === verifiedRecordsAll.Thing) {
tempDuplicateRecords.push(record);
duplicateRecords = tempDuplicateRecords
}
})
}
return duplicateRecords
}
For finding duplicate records I need to have such a condition that if Some or Gum or Thing property of one record should match the value of another record in the same array, then it should be stored as a duplicate.
I am new to JS and cannot think of any other solution.
Any help would be appreciated. Thank you.
this.verifiedRecordsAll=[] --Array need to add
this.verifiedRecordsAll =this.verifiedRecordsAll.filter((element, i) => i === this.verifiedRecordsAll.indexOf(element))
const yourArray=[]
let duplicatedRecords=[]
for(let k=0;k<array.length;k++){
for(let i=0;i<array.length;i++){
if(k!==i&&array[k]===array[i]){
duplicatedRecords.push(array[k])
}
}
}
duplicatedRecords= [...new Set(duplicatedRecords)]

How to pass a function only after an entire array has been read and not for each element?

I'm trying to run a function when reading an array, but instead of running the function to each element, which I'm currently using forEach for, I want to make the script to read the entire array and then pass a function.
What I'm trying to say is this:
data.forEach(movie => {
// Log each movie's title
//console.log(movie.title);
// Check if the userWord matches
if (movie.title.toUpperCase().includes(userWord.toUpperCase())) {
alert("YES");
} else {
alert("NO").
}
});
Let's say my array is: array = ["Aa", "Ba", "Ca", "Da"];
If the user enters: a, then the script would alert("YES") four times, and I want to make it alert just once, at the end of the iteration.
For the same example, if the users enters: B, then the script would first alert("NO") once, then alert("YES"), then alert("YES") 2 times, and I want to make it just alert("YES")once, in the end.
Finally, if the users enters: Ferrari, then the script would alert("NO") four times, and I just want it to alert("NO") at the end.
I tried to make it very clear here, that's why the three "cases" of what is happening.
In the end, I want to know if there is a method that is kinda the opposite of the forEach or the common for. Something that just executes the function after reading the entire array.
Change the alert to a bool variable
Remove else (it would only overwrite any match)
if bool statement outside the loop to perform actions
if you want a list of the results, you should store the names in an array and outside of the loop - print.
see below:
Non-loop method:
data = ["test","hello", "hello1"];
search = "lo";
const matches = data.filter(movie => movie.includes(search));
alert(matches) //array of only matches - only has hello and hello 1
I don't know if there are performance gains against a loop... I suppose you could do a side by side comparison on a large dataset
Loop method:
var matches = "";
data.forEach(movie => {
// Check if the userWord matches
if (movie.title.toUpperCase().includes(userWord.toUpperCase())) {
matches += movie.title + "<br> ";
}
});
if (matches.length > 0)
{
document.getElementById("results").innerHTML = matches;
} else {
alert("No match found");
}
You'll see more customization on the first loop, but I think filtering data first is the way to go.
I think closest what you can get is some. Here is example
let data = ["test","hello", "hello1"];
let userWord = "el";
let res = data.some(movie => movie.toUpperCase().includes(userWord.toUpperCase()));
console.log(res) // prints true- means there was at least one "match", so you can alert
You could use the filter array function to filter array elements that match your criteria and then finally alert only if a match is found. Using this method you could get all the elements that have matched to your userWord. That is, the match array will contain all the possible matches.
var data = ['Aa', 'Bb', 'Cc', 'Dd'];
var flag = false;
var userWord = 'F'
var match = data.filter(movie => movie.indexOf(userWord) !== -1);
if(match.length)
console.log('YES');
else
console.log('NO');
If I understand the question correctly, I believe you want to execute something if a predicate matches at least one of the items in the array (correct me if I'm wrong). For that you can use the some method like so:
if (movies.some((movie) => movie.toUpperCase().includes(userWord.toUpperCase()))) {
alert('YES');
} else {
alert('NO');
}

Using Javascript Array Filter method to apply logic [duplicate]

I have search through quite a lot of questions here, but havent found one that i think fits my bill, so if you know of one please link to it.
I have an array that i want to search through for a specific number and if that number is in the array, i then want to take an action and if not then another action.
I have something like this
var Array = ["1","8","17","14","11","20","2","6"];
for(x=0;x<=Array.length;x++)
{
if(Array[x]==8)
then change picture.src to srcpicture1
else
then change picture.src to srcpicture2
}
but this will run the lenght of the array and end up checking the last element of the array and since the last element is not 8 then it will change the picture to picture2.
Now i can see why this happens, i just dont have any ideas as to how to go about checking if an array contains a specific number.
Thanks in advance.
What you can do is write yourself a function to check if an element belongs to an array:
function inArray(array, value) {
for (var i = 0; i < array.length; i++) {
if (array[i] == value) return true;
}
return false;
}
And the just do:
var arr = ["1","8","17","14","11","20","2","6"];
if (inArray(arr, 8)) {
// change picture.src to srcpicture1
} else {
// change picture.src to srcpicture2
}
It's a lot more readable to me.
For extra points you can add the function to the array prototype like so:
Array.prototype.has = function (value) {
for (var i = 0; i < this.length; i++) {
if (this[i] === value) return true;
}
return false;
};
And then the call would be
if (arr.has(8)) // ...
Pushing this even further, you can check for indexOf() method on array and use it - if not - replace it with the code above.
P.S. Try not to use Array for a variable name, since it's reserved for the actual array type.
use this
http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf
ie version
https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf#Compatibility
Why don't just you abort the loop when you find the right number :
for(x=0;x<=Array.length;x++)
{
if(Array[x]==8) {
//change picture.src to srcpicture1
break;
}
}
You could sort the array first then check the array only up to the point at which a number would be in the array, were it to exist.
If you have unique keys and a faster retrieval is what you care about a lot, you can consider using a map instead of an array (if there's a hard-bound case of using an array, then it won't work of course). If using a map, you just check "if( num in arr ) ".

slice different arrays and return selected values

I am working on a problem. I do not know the right question to ask in order to solve this problem. I have gotten what seems to be the required results but the verification problem for the solution does not work. I am not sure if I am solving it correctly. Basically I am given an array and I have to filter out elements from that array by slicing certain ingredients.
question: "We only use the elements that the instruction tells us to. So, we need to create a slice of the given array of elements (taken from the beginning) to resemble only the elements we are using. If the instruction doesn't say anything, we only take the first element."
var hammerIngredients = ['iron', 'granite', 'copper'];
var spearIngredients = ['iron', 'granite', 'copper'];
var panIngredients = ['iron', 'granite', 'copper'];
take(hammerIngredients); // returns ['iron']
take(spearIngredients, 2); // returns ['iron', 'granite']
take(panIngredients, 3); // return ['iron', 'granite', 'copper']
"If the instruction says to use more than the elements listed, we use all the elements in the array. If the instruction says to use no elements, we return an empty array."
var plateIngredients = ['iron', 'granite', 'copper'];
take(plateIngredients, 0); // returns []
So I have tried to do the program and I have done the following. It appears to work, but when I try to verify it I get that it is invalid.
function take(ingredients, slicer) {
if (arguments.length === 1) {
slicer = 1;
}
if (ingredients === hammerIngredients){
return ingredients.slice(0, slicer);
} else if(ingredients === spearIngredients) {
return ingredients.slice(0,slicer);
} else if (ingredients === panIngredients) {
return ingredients.slice(0,slicer);
} else if (ingredients === plateIngredients) {
return ingredients.slice(0,slicer)
} else {
return;
}
}
And I have no idea why. Help please!
you have no logic for if the slicer parameter is 0, in which case you need to return an empty array.
Put this line in there and it should work, based on the requirements you gave:
if (slicer === 0) {
return [];
}
You code currently only works if one of those three exact arrays are used. Does the verification code create and use only those arrays?
Your code does not need to be tied to existing ingredient arrays. After setting the default slicer value you can just:
return ingredients.slice(0,slicer);

Simplest way to remove duplicates from an array, whilst still keeping the last one

What would be the most simple way to remove duplicates from an array that have a specific value the same, whilst still keeping the most recent one pushed in?
Let's say I have this function.
bucket.bucketList =[];
bucket.addItem = function(item) {
bucket.bucketList.push(item);
}
The function pushes an object called foo this into the array on every mouse-scroll:
Some foo's also have a property ,
foo.name = "something";
The question is, what is the best way to delete a duplicate based on their property name whilst keeping the most recents one pushed in?
So the concept now is to remove all duplicates, except the last one, that have a foo.name = "example".
PS: I am using jQuery already in my project, so if jQuery has a more elegant way of doing this than vanilla JS i'd be more than happy to use it.
Edit: This is my exact code snippet:
bucket.addItem = function(item) {
bucket.bucketList.push(item);
var dict = {}, item;
for (var i = bucket.bucketList.length - 1; i >= 0 ; i--) {
item = bucket.bucketList[i];
if (item.name) {
// if already in the dict, remove this array entry
if (dict[item.name] === true) {
bucket.bucketList.splice(i, 1);
} else {
// add it to the dict
dict[item.name] = true;
}
}
}
}
Unfortunately, the above function removes all duplicates from the array that have the same name property. What I want is just to remove duplicates that have a SPECIFIC property name. e.g : item.name ="example"
Try this:
bucket.addItem = function(item) {
bucket.bucketList = bucket.bucketList.filter(function(e){
return item.name !== "example" || e.name !== "example"
});
bucket.bucketList.push(item);
}
This basically removes all items that have the same name as the current item, then pushes the current item onto the list, but only if the name is "example"

Categories

Resources