This question already has answers here:
How can I update state.item[1] in state using setState?
(25 answers)
Closed 4 years ago.
I have an array in my state:
state = {
some_array: [1,1,1,3,6,3,6,23], // sorry for the syntax it's changed
}
Now I want to change value in that array that has index let's say 4, and in this case that would be number 6, or if I want to change index 1, that would be second number or array.
I know this is probabbly very simple but I'm just very confused.
If you need more info please comment.
Thanks!
I think that you can to use next code:
const some_array = [...this.state.some_array]
some_array[indexHere] = yourValue
this.setState({some_array:some_array})
This example --- true way for FP in react.
Related
This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 1 year ago.
I have this object, and I want Remove duplicates and make it into one array
var sports = [
['basketball','fotball','racing'],
['fotball','basketball','swimming'],
];
What is the best way to get it like this:
['basketball','fotball','racing','swimming'],
The flat() will make the sports into one array, and is probably the best option here?
Just have to remove the duplicates any tips?
use below:
[...new Set(sports.flat())]
Here sports.flat() flatten out the array. See the doc here
and new Set() will make them unique. See the doc here
This question already has answers here:
The useState set method is not reflecting a change immediately
(15 answers)
React setState not updating state
(11 answers)
Closed 1 year ago.
I'm trying to pushing a new object (after clicking a button) into my stated Array, my initial array has 25 cocktails.I want to push one, simply, first of all, this is my code about the insert.
console.log(drinks,"First, before pushing");
setDrinks([...drinks, {strPicture: null, strDrink: 'Vodka', strAlcoholic: 'Alcoholic', strCategory:'Cacharro'}]);
console.log(drinks,"Second, After pushing");
The first console log return an array with length 25, next i use my setDrinks, adding the previous drinks and the new one.
But the next console log returns an array with length 25, the same array of the first console log.
The next time i click my button it give me, the array with length 26, but i need to show on the first click, not on the second.
I tried many things like...
setDrinks((drinks) => [...drinks, {strPicture: null, strDrink: 'Vodka', strAlcoholic: 'Alcoholic', strCategory:'Cacharro'}]);
The only way that it have worked is with drinks.push but i read that is a very bad idea.
I'm using next js.
Thanks
This question already has answers here:
remove common elements of two arrays in jquery
(4 answers)
Closed 3 years ago.
Lets say i had two arrays
a.["mark#example.com", "bob#example.com"]
b.["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
How can i make a third array, using the variables that are present in the second one, but not on the first one?
the desired output should be this:
c.["carl#example.com", "josh#example.com"]
const arrayA = ["mark#example.com", "bob#example.com"]
const arrayB = ["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
const arrayC = arrayB.filter(x => !arrayA.includes(x))
console.log(arrayC)
This question already has answers here:
Convert [key1,val1,key2,val2] to a dict?
(12 answers)
Make dictionary from list with python [duplicate]
(5 answers)
Convert list into a dictionary [duplicate]
(4 answers)
Closed 4 years ago.
trying to figure out how to do this and have yet to find a good solution. I pulled this data out of an XML response. It was in a var tag. Now what I would like to do is create a dictionary out of it. The domain.com should be paired with the number right listed behind it.
This is the data:
[
'cb131.domain1.com', '147827',
'cb143.domain2.com', '147825',
'cb175.domain1.com', '147454',
'cb190.domain.com', '146210',
'cb201.domain.com', '146208',
'cb219.domain.com', '146042',
'cb225.domain.com', '146282',
'cb900.domain.com', '148461',
'cb901.domain.com', '148493',
'cb902.domain.com', '148495',
'cb903.domain.com', '148497',
'cb904.domain.com','148499',
'cb905.domain.com', '148501',
'cb906.domain.com', '148503',
'cb907.domain.com', '148505',
'cb908.domain.com', '148507',
'cb909.domain.com', '148509'
]
So for example cb131.domain1.com should be paired with 147827, cb143.domain2.com paired with 147825 and so on.
Drawing a blank on a good quick solution on how to do this. Hopefully someone can help.
Thanks!
Edited with answer I choose below:
I choose this answer and also to help anyone else I add a nice way to print out the results (data is the string I obtained):
import ast
i = iter(ast.literal_eval(data))
dic = dict(zip(i, i))
for key , value in dic.items():
print(key, " :: ", value)
This should do it. Assuming the list is saved to a variable l:
keys = l[::2]
vals = l[1::2]
dic = dict(zip(keys, vals))
You can create an iterator from the list after using ast.literal_eval to parse it from the input text, zip the iterator with itself, and pass the generated sequence of tuples to the dict constructor:
import ast
i = iter(ast.literal_eval(data))
dict(zip(i, i))
Assuming you have the above in a python array called data, you can do:
new_data = []
for i in range(0, len(data), 2):
new_data.append((data[i], data[i+1]))
Now new_data would be a list of tuples. You could certainly create a better data structure to hold these pairs if you want.
I do not yet know Python that I can write a snippet, but:
initialize an empty dictionary in Python
create a for loop counting index from 0 to length of your array in steps of two.
inside add a dictionary entry with key of value at index and value at index + 1
perhaps check for duplicates
Does this answer help you?
This is Python - quickly google'd:
dictionary = { }
for idx in range(0, len(data), 2)
dictionary[data[idx]] = data[idx + 1]
This question already has answers here:
How do I get the last 5 elements, excluding the first element from an array?
(9 answers)
Closed 6 years ago.
I would like to know how can I obtain the last two elements of this array like this:
array = [1,5,7,8,10,12,23,24];
I am trying to obtain with slice() function but it doesn´t work for me, beacuse I always I want to obtain the last two positions.
array = [23,24];
I hope that anyone can help me in this question.
Regards!
Use Array#slice with negative index.
var array = [1,5,7,8,10,12,23,24];
console.log(array.slice(-2));
You can learn more about this method also here.
array = [1,5,7,8,10,12,23,24];
console.log(array.slice(-2))
Use slice -2
aray.prototype.splice() also
Note: Splice it will update the original array also
var array = [1,5,7,8,10,12,23,24];
console.log(array.splice(-2));