How to delete an element from an array - Javascript P5 - javascript

This is the way I add an element to my array
assets.push(house_location + " " + "Cost" + " " + cost + " " + "Downpay"+ " " + downpay)
Now I'm looking for a way to delete that same specific element from my array. Does someone know the code for this? So basically, the opposite of array.push

What you are looking for is the splice method.

If you want to remove the last item you pushed, use assets.pop(). If you want to remove that exact item like you said, you could do something like this:
assets.filter(d => d !== house_location + " " + "Cost" + " " + cost + " " + "Downpay"+ " " + downpay)

I'm guessing you are looking for these methods.
Array.shift() // Removes the first item from an array, and then returns the item removed.
Array.pop() // Removes the last item from an array, and then returns the item removed.
Array.splice() // I really can't be bothered to explain this one, so look at the link below.
The splice method.

Related

Listing an array in a message

I want to list an array in a discord message. I've got the following code so far:
message.channel.send("Game is starting with " + playerNumber + " Players! \n"
+ memberIDs.forEach(element => ("<#" + element + "> \n")));
The memberIDs array contains all IDs from member in a specific channel, now I want to put all member who are in the channel into a message. I've tried it with the code above but it only gives me the message:
Game is starting with *playerNumber* Players!
undefined
I don't really understand why it is undefined because it also would give me the memberIDs in a console log or when I make a forEach loop with messages that get created for every memberID.
The Array in the console.log locks like the following:
[ '392776445375545355', '849388169614196737' ]
I appreciate any help.
short answer
forEach() is not your friend; you need to map() and join()
long answer
the undefined is the result of the forEach() invocation which does not return anything.
so you need to get an string instead.
to do so, you may use .map() which returns an array; and .join() that joins an array of strings into an string.
something like this:
memberIDs.map(element => "<#" + element + ">" ).join('\n');
forEach iterates over an array and doesn't return anything. Use map instead with join:
message.channel.send("Game is starting with " + playerNumber + " Players! \n"
+ memberIDs.map(element => `<#${element}>`).join('\n')
);
Array#map() returns an array of return values from the callback, while Array#forEach() returns undefined. Use .map()
memberIDs.map(m => `<#${m}>`)
.join("\n") // join each element with a newline
forEach doesn't return anything in JavaScript, meaning that memberIDs.forEach(element => ("<#" + element + "> \n"))) will always be undefined.
let idString = "";
memberIDs.forEach(element => (idString = idString + "<#" + element + "> \n"))
message.channel.send("Game is starting with " + playerNumber + " Players! \n"
+ idString);
What this code does is it defines a variable outside of the forEach loop, and then appends strings to it each time the loop runs.

how do I add two values to option value and seperate with with a comma javascript

I am trying to put two values in option value separating with comma using javascript but when I get the value from server it shows only the first value i.e course code
javascript Code
options += "<option value="+element.course_code+","+element.course_id+">(" + element.course_code + ") " + element.name + "</options>";
data.append('course_id', course_id);
Server
$myvalue = $request->get('course_id')
You should put quotes around the value so it is treated as one whole string
options += "<option value='"+element.course_code+","+element.course_id+"'>(" + element.course_code + ") " + element.name + "</options>";
You can try using the following code instead by declaring the single string.
The code should look like:
options += "<option value="+element.course_code+','+element.course_id+">(" + element.course_code + ") " + element.name + "</options>";

Concat QML Properties with Javascript

I'm setting properties on QML items with java script, where "gaugetext" is a property of my QML object.
function finishCreation(setWidth,setHeight,setX,setY,setMinValue,setMaxValue,setDecPlace,setUnit,setID,SetValueObject,SetValueProperty) {
...
"gaugetext": Qt.binding(function(){return SetValueObject[SetValueProperty] + " " + setUnit})
....
}
This works well, but I want to set numbers of decimals. So I tried this:
"gaugetext": Qt.binding(function(){return "(" + SetValueObject[SetValueProperty]+ ")" + ".toFixed(" + setDecPlace + ")" + " " + setUnit})
But this results that the correct Value is shown with ".toFixed(value)".
You return a String "(<someValue>).toFixed(<someOtherValue>) <someThirdValue>".
You probably want to omit some of your " since you probably want to have the pieces that you mark as string to be executed instead.
return SetValueObject[SetValueProperty].toFixed(setDecPlace) + " " + setUnit
~~~(untetested)
Most likely however using a "finalize creation" function is a bad approach.

How do I make a command with the length of how many arguments are in the command?

This may sound weirdly phrased, but I don't know how else to describe it. I'm trying to make a discord bot with a command where certain people can write javascript code to do things with the bot on the fly, but I don't know how to make it work with multiple spaces. I want it to work with as many spaces as possible or as little spaces as possible, but this only works with ones with exactly 10 spaces.
if(command === '!cmd') {
if(message.author.id != ownerid) {
bot.guilds.get(guildid).channeks.get(generalchan.sned("YOU ARE NOT ALLOWED TO USE THIS COMMAND\nTHIS IS YOUR ONLY WARNING"))
} else if(message.author.id === ownerid) {
eval(messageArray[1] + " " + messageArray[2] + " " + messageArray[3] + " " + messageArray[4] + " " + messageArray[5] + " " + messageArray[6] + " " + messageArray[7] + " " + messageArray[8] + " " + messageArray[9]);
message.delete();
}
}
Let evalStr = ""
For(let element of messageArray){
evalStr += element + " "
}
eval(evalStr)
You'll need to slice out the trailing space character.
But you're taking message.content then splitting each word into an array. Then trying to fuse the array back together adding the spaces back in. You should just delete the first x chars of message. content where x is prefix.length + command and eval the result.
Eval commands are very dangerous. Please make sure you know what you're doing before implementing these.
EDIT: This guide is worth taking a look at and bookmarking.
https://anidiotsguide.gitbooks.io/discord-js-bot-guide/examples/command-with-arguments.html

Issues using [attribute!=value] and addclass

Its a fairly small script, but for some reasons im not able to get it working.
this is the function
function stffsort(n) {
$("[data-stff=" + n + "]").removeClass("hidden");
$("[data-stff!=" + n + "]").addClass("hidden");
}
however, this piece here $("[data-stff!=" + n + "]") returns the whole page elements.
however, this piece here $("[data-stff!=" + n + "]") returns the whole page elements.
Yes; it would return everything that doesn't have a data-stff set to that value, including things that don't have a data-stff attribute at all.
Try [data-stff][data-stff!=" + n + "]" and see if that gets you what you're after.
This kind of selector requires the value to be in double-quote marks:
$('[data-stuff!="' + n + '"]')

Categories

Resources