Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a problem attaching a dynamic variable when Im using find method in ES6. Pls check my code below. I need to attached the productValue. Is this not possible?
const productValue = 1;
products.find((item => item.option[productValue] === 'food'))
This is not correct usage of find in array. Look at the sample:
products = ["vegetable", "food", "test"];
const productValue = 1;
var res = products.find((item => console.log(item)));
Now look at this one:
products = ["vegetable", "food", "test"];
const productValue = 1;
var res = products.find((item => console.log(item[1])));
See you are using "vegetable"[1] that is first character of each string
You know the index so easily check in if products[productValue] == "food"; Or of you want find by index use findIndex.
Upadte
After chattin with OP I understand he wants to append the productValue to one of products key value (option that is string), so you need to use + operator:
products.find((item => (item.option + productValue) === 'food'))
You can use the index parameter to check whether a specific item in an array equals some value.
sample:
const productValue = 1;
const products = ['beverages', 'food'];
console.log(products.find((item, index) => index === productValue && item === 'food'));
const products = ["vegetable", "food", "test"];
const productValue = 1;
const updatedFruits = products.map(product => {
if(product === "food"){
return product + productValue
}
return product;
});
console.log(updatedFruits)
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I would like to separate this in Javascript and create variables infos1, infos2 ....
So I need a function to separate all the info.
<option value="[infos1][infos2][infos3][infos4]"></option>
var InfosInitial = $('#insert').find("option:selected").val();
var infos1 = ?;
var infos2 = ?;
Thanks you in advance !
You can use Regex to do this. We will use the String.match() function in this example. It should look something like this:
const InfosInitial = '[infos1][infos2][infos3][infos4]';
const info = InfosInitial.match(/(?<=\[)(.*?)(?=\])/gm);
const infos1 = info[0];
const infos2 = info[1];
const infos3 = info[2];
const infos4 = info[3];
console.log(`${infos1}\n${infos2}\n${infos3}\n${infos4}`);
console.log('As an array:', info);
Also, I am just saying but having everything in 4 different variables is inefficient. So you should use the array instead.
this is really hacky but you can add new variables to the window element like this
window['myVar'] = 'Hello, world!'
now you can call and redefine myVar by simply doing this
console.log(myVar)
// 'Hello, world!'
myVar = 42069
console.log(myVar)
// 42069
knowing this you can create a function where it gets the attribute 'value'
and split it between the ] and [ with String.split() remove the brackets and loop through the remainders
your question little unclear to me but I guess this is what you want.
var InfosInitial = "[infos1][infos2][infos3][infos4]";
Array.from(InfosInitial.matchAll(/\[([^\]]*)\]/g)).forEach(value => {
console.log("all = " + value[0] + ", value = " + value[1]);
})
I want to have code that can add dynamic filters to an array and then the array run through a .filter that then checks if all the filters are true:
var checks = [item =>item.indexOf("e") == -1, item =>item.indexOf("r") == 0];
var words = ["roast", "wings", "chive", "pests", "rhyme"]
var filteredArr = words.filter(item => checks.every(f => f(item)));
I want it to take two inputs like "r" and the column it should be in (0) and add item =>item.indexOf("r") == 0 to the checks array.
The problem I have been running into is that when creating arrow functions to push into the checks array, the variables don't compute now because it is an arrow function. Ex:
var value = "r"
item =>item.indexOf(value) == -1
does not work because it will stay as value and not the value the variable is.
var value = "r"
var checks = [item =>item.indexOf(value) == -1];
var words = ["roast", "wings", "chive", "pests", "rhyme"]
var filteredArr = words.filter(item => checks.every(f => f(item)));
console.log(filteredArr);
I have looked through many other questions but they never are able to add filters like I am describing. I was wondering if there is a fix for this problem or another way to add checks to an array dynamically.
Edit:
var words = ["roast", "wings", "chive", "pests", "rhyme"];
var checks = [];
for(let i = 0; i < 30; i++){
var value = "r"
var column = i%5;
checks.push(item =>item.indexOf(value) == column);
}
var filteredArr = words.filter(item => checks.every(f => f(item)));
console.log(filteredArr);
Is it possible for the filter that I pushed to include the i%5 value?
Your code works for me. Please explain how this doesn't work for you.
var value = "e";
var checks = [item =>item.indexOf(value) == -1, item =>item.indexOf("r") == 0];
var words = ["roast", "wings", "chive", "pests", "rhyme"]
// prints roast
console.log("answer:"+words.filter(item => checks.every(f => f(item))));
value = "o";
// prints rhyme
console.log("answer:"+words.filter(item => checks.every(f => f(item))));
BTW are you making a wordle solver? Because I made one just this morning in Python ;)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm currently working on a project where I send data to a google sheet through app script.
I need to change the value of some data and used createTextFinder.
Thanks to #Mimi in this thread. But the fact is that I need to replace more than one text.
I need to change the text when the data is appending.
thanks in advance
Here is my code
function addUser(sheet) {
var FILE = SpreadsheetApp.openById("XXXXXXXXXXXXX");
var sheet = FILE.getSheetByName("Sheet1");
var id = "True";
var name = "FALSE";
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
From I need to change the text when the data is appending., in this case, how about the following modification?
From:
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
To:
var values = [id, name].map(e => {
var temp = e.toUpperCase();
return temp == "TRUE" ? "YES" : temp == "FALSE" ? "NO" : e;
});
sheet.appendRow(values);
For example, when you are required to use TextFinder, how about the following modification?
From
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
To
sheet.appendRow([id, name]);
sheet.createTextFinder("TRUE").replaceAllWith("YES");
sheet.createTextFinder("FALSE").replaceAllWith("NO");
or, when you want to use TextFinder to the appended row, you can also the following modified script.
sheet.appendRow([id, name]);
// SpreadsheetApp.flush(); // This might be not required to be used.
const range = sheet.getRange(sheet.getLastRow(), 1, 1, 2);
range.createTextFinder("TRUE").replaceAllWith("YES");
range.createTextFinder("FALSE").replaceAllWith("NO");
Multiple Replacement
function addUser() {
const txtA = ['txt1', 'txt2', 'text3'];
const rplA = ['rpl1', 'rpl2', 'repl3'];
var ss = SpreadsheetApp.openById("XXXXXXXXXXXXX");
var sh = ss.getSheetByName("Sheet1");
txtA.forEach((t, i) => {
let f = sh.createTextFinder(t).findAll();
if (f) {
f.forEach(r => { r.setValue(r.getValue().replace(txtA[i], rplA[i])); });
}
});
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
const toDoForm = document.querySelector(".todo__form"),
toDoInput = document.querySelector(".todo__input");
const toDoArray = [];
function addToDoList() {
toDoForm.addEventListener('submit', event => {
toDoArray.push(toDoInput.value);
event.preventDefault();
});
}
I made a code to put an input value to an array, and i want to put it in the list of html. How do i do that?
You can display the elements from your array dynamically by building a string and insert the string into the element.
const toDoArray = ["Max", "mortiz", "martin"];
function displayArrayInList(){
let output = `<ul>`
for(let i = 0; i < toDoArray.length; i++){
output += `<li>${toDoArray[i]}</li>`
}
output += `</ul>`
document.getElementById('displayer').innerHTML = output;
}
displayArrayInList();
<div id="displayer"></div>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to Receive a String in this format: "1-10" and create an array with the amount of numbers in the range. Print the array to screen using a for loop.
I.E - "1-5" received so they array will be: {1,2,3,4,5}
create for workflow with vCenter orchestrator.
You can split the string into array and then iterate in a loop to get the iteration.
let str = "1-5";
str = str.split('-');
for(let i = parseInt(str[0]); i<=parseInt(str[1]); i++) {
console.log(i);
}
You may use some cool ES6:
Array.range = function(s){
const [start,end] = s.split("-");
return Array.from({length:start-end}).map((_,i)=>i+ +start);
};
Usable like this:
Array.range("1-10") //[1,2,3...]
var input = "1-10"; //SAMPE INPUT DATA.
var foo = input.split("-"); //PASRING INPUT DATA.
var answer = [];
for(var i = foo[0]; i<= foo[1]; i++){
answer.push(parseInt(i)); //MAKE AN ARRAY.
}
console.log(answer);