I would like to set an array just with the values from another array. Should I use push for this?
well, in this case, I'm trying to get the selectedOptions array and set the selectedTags array just with selectedOptions value
but just push 1 value from the selectedOptions array!
So, someone please can spare a hint on how to map an array and get the values from it?
handleChange = (selectedOptions) => {
this.setState({ selectedOptions });
let options = [];
selectedOptions.map((v) => options.push(v.value));
this.setState((state) => ({
selectedTags: options
}));
}
You can use the Array.map function:
const data = [
{id: 1, value: '1'},
{id: 2, value: '2'},
{id: 3, value: '3'},
{id: 4, value: '4'},
{id: 5, value: '5'},
{id: 6, value: '6'},
{id: 7, value: '7'},
]
const onlyValues = data.map(d => d.value)
console.log(onlyValues)
const data = [1,2,3,4,5,6,7,8,9,10]
const toBeAdded = 11
const newData = [...data, toBeAdded]
console.log(newData)
const toBeAddedArray = [11,12,13,14,15,16]
const newDataArray = [...data, ...toBeAddedArray]
console.log(newDataArray)