I have an Array that has objects inside that looks like this
OrchardSite > Array(16) > 0: {blocks: Array (3), name: "asdasd", code: "R101A"}
1: {blocks: Array (7), name: "dasdas", code: "R555"}
I have JSX that looks like this
<select className="input-field-slider dropdown w-select" onChange={() => this.setOrchard}>
<option>All Sites</option>
{orchardSites
.map((orchardSite, i) => (
<option key={i} value={orchardSite.code}>
{orchardSite.code + " " + orchardSite.name}
</option>
))}
Every time the user selects an option from the dropdown I have this function that fires..
setOrchard = (event) => {
const orchardId = event.target.value;
console.log("orchardID", orchardId) // returns selected ID
console.log("OrchardSite", this.props.metadata.OrchardSites) // returns array
console.log("testing1", this.props.metadata.OrchardSites[parseInt("R1018A")] // undefined
console.log("testing2", this.props.metadata.OrchardSites[parseInt(orchardId)]) // undefined
console.log("new value", this.props.metadata.OrchardSites[parseInt(orchardId)].blocks) // Cannot read property 'blocks' of undefined
if (orchardId.length > 0) {
if (!this.props.metadata.OrchardSites[parseInt(orchardId)].blocks)
this.props.metadata.orchardSites[parseInt(orchardId)].blocks = []
console.log("SetttingOrchardLocation", this.props.metadata.orchardSites)
this.setState({ orchardId: orchardId, block: null, area: null, row: null, site: Object.assign({}, this.props.metadata.orchardSites[parseInt(orchardId)]) })
console.log("Testing OrchardId", this.props.metadata.orchardSites[parseInt(orchardId)])
// parseInt to get access to the array index, (Maybe I need to .ToString() somewhere?
}
}
Which returns..
Uncaught TypeError: Cannot read property 'blocks' of undefined
How can I gain access to this array?
If both the orchardSites (in your jsx and your onClick function) are the same, then you want to access the array using the array index (i in jsx) instead of the orchardSite.code.
This is because OrchardSites[orchardSite.code] can be undefined in certain cases but OrchardSites[parseInt(i)] will not be.
I suggest you change the ‘value’ attribute to “i” instead of orchardSite.code
Related
I receive an object from the api and I need to change the maximum quantity, I created a copy with let but the rejection still occurs as if I were using a const in this copy.
produtosComplementares = [
{
"codigo":null,
"codigoProduto":2,
"descricao":'ADICIONAL PDV',
"imagem":null,
"nome":null,
"quantidadeMaxima":1
},
{
"codigo":null,
"codigoProduto":941,
"descricao":'ADICIONAL Retaguarda',
"imagem":null,
"nome":null,
"quantidadeMaxima":0
}
];
let arrayTemporario: any = [...produtosComplementares];
arrayTemporario.map((item: any) => {
if(item.codigoProduto == 2{
item.quantidadeMaxima = 3
}
});
the following rejection occurs:
TypeError: Cannot assign to read only property 'quantidadeMaxima' of object '#'
when you are mapping over the object you are getting each key value of the object
so if the item key is codigoProduto and it equals 2 you can not item can not also be the key quantidadeMaxima.
I think it would be very easy for you to debug this or just print item inside the map function.
I'm trying to filter data according to input value. I want to create an array with an objects but im getting array of [object Object].
First Im creating an empty Array:
let beersArray = [];
Later during creating elements I want to add to array an object with item name:
function displayAllBeers(data) {
beersArray = data
.map((beer) => {
beersContainer.innerHTML += `
<div class='beerContainer'>
<h3 class='name'>${beer.name}</h3>
<img src='${beer.image_url}'></img>
<p>${beer.description}</p>
<button class="addFavBtn">Favourite</button>
</div>
`;
return { name: beer.name };
})
.join("");
}
Finally I add event listener to the input and want to filter the result
searchInput.addEventListener("input", (e) => {
const value = e.target.value;
console.log(beersArray);
beersArray.forEach((beer) => {
const isVisible = beer.includes(value);
beer.element.classList.toggle("hide", !isVisible);
});
});
I'm getting error:
app.js:17 Uncaught TypeError: beersArray.forEach is not a function
at HTMLInputElement. (app.js:17:16)
If I'm trying to do
Array.from(beersArray)
It puts in array every single letter as an element.
Please tell me what I'm doing wrong. Thank you in advance.
I have this function that returns an array of objects, every object represent a sticky, what I want is to change the value of "content" everytime I click one of the stickies
handleStickyEdition = (index) => {
const { currentStage } = this.props
const stickies = currentStage.get('stickies')
const updatedStickies = [...stickies]
console.log(updatedStickies)
}
And the result of calling the console.log is this array of objects:
If I do console.log(updatedStickies[index].get('content')) I will get the content of the object I want to change. For example name 3.
How can I replace this content with an empty string? in other words, if I click the object in the position 0, how can I make name 3 equals to ''
I would suggest using a map like so.
this.setState({
updatedStickies: this.state.updatedStickes.map(sticky => ({
...sticky
content: sticky.id === idOfStickyWeWantToUpdate ? "" : "content"
}))
});
I see you are reading stickies from props, I would suggest having a function in your parent component to run the above code which you can call from your child component if need be.
I'm making a primitive quiz app with 3 questions so far, all true or false. In my handleContinue method there is a call to push the users input from a radio form into the userAnswers array. It works fine for the first run of handleContinue, after that it throws an error: Uncaught TypeError: this.state.userAnswers.push is not a function(…)
import React from "react"
export default class Questions extends React.Component {
constructor(props) {
super(props)
this.state = {
questionNumber: 1,
userAnswers: [],
value: ''
}
this.handleContinue = this.handleContinue.bind(this)
this.handleChange = this.handleChange.bind(this)
}
//when Continue button is clicked
handleContinue() {
this.setState({
//this push function throws error on 2nd go round
userAnswers: this.state.userAnswers.push(this.state.value),
questionNumber: this.state.questionNumber + 1
//callback function for synchronicity
}, () => {
if (this.state.questionNumber > 3) {
this.props.changeHeader(this.state.userAnswers.toString())
this.props.unMount()
} else {
this.props.changeHeader("Question " + this.state.questionNumber)
}
})
console.log(this.state.userAnswers)
}
handleChange(event) {
this.setState({
value: event.target.value
})
}
render() {
const questions = [
"Blargh?",
"blah blah blah?",
"how many dogs?"
]
return (
<div class="container-fluid text-center">
<h1>{questions[this.state.questionNumber - 1]}</h1>
<div class="radio">
<label class="radio-inline">
<input type="radio" class="form-control" name="trueFalse" value="true"
onChange={this.handleChange}/>True
</label><br/><br/>
<label class="radio-inline">
<input type="radio" class="form-control" name="trueFalse" value="false"
onChange={this.handleChange}/>False
</label>
<hr/>
<button type="button" class="btn btn-primary"
onClick={this.handleContinue}>Continue</button>
</div>
</div>
)
}
}
Do not modify state directly! In general, try to avoid mutation.
Array.prototype.push() mutates the array in-place. So essentially, when you push to an array inside setState, you mutate the original state by using push. And since push returns the new array length instead of the actual array, you're setting this.state.userAnswers to a numerical value, and this is why you're getting Uncaught TypeError: this.state.userAnswers.push is not a function(…) on the second run, because you can't push to a number.
You need to use Array.prototype.concat() instead. It doesn't mutate the original array, and returns a new array with the new concatenated elements. This is what you want to do inside setState. Your code should look something like this:
this.setState({
userAnswers: this.state.userAnswers.concat(this.state.value),
questionNumber: this.state.questionNumber + 1
}
Array.push does not returns the new array. try using
this.state.userAnswers.concat([this.state.value])
this will return new userAnswers array
References: array push and array concat
You should treat the state object as immutable, however you need to re-create the array so its pointing to a new object, set the new item, then reset the state.
handleContinue() {
var newState = this.state.userAnswers.slice();
newState.push(this.state.value);
this.setState({
//this push function throws error on 2nd go round
userAnswers: newState,
questionNumber: this.state.questionNumber + 1
//callback function for synchronicity
}, () => {
if (this.state.questionNumber > 3) {
this.props.changeHeader(this.state.userAnswers.toString())
this.props.unMount()
} else {
this.props.changeHeader("Question " + this.state.questionNumber)
}
})
console.log(this.state.userAnswers)
}
Another alternative to the above solution is to use .concat(), since its returns a new array itself. Its equivalent to creating a new variable but is a much shorter code.
this.setState({
userAnswers: this.state.userAnswers.concat(this.state.value),
questionNumber: this.state.questionNumber + 1
}
The recommended approach in later React versions is to use an updater function when modifying states to prevent race conditions:
this.setState(prevState => ({
userAnswers: [...prevState.userAnswers, this.state.value]
}));
I have found a solution. This shoud work for splice and others too. Lets say that I have a state which is an array of cars:
this.state = {
cars: ['BMW','AUDI','mercedes']
};
this.addState = this.addState.bind(this);
Now, addState is the methnod that i will use to add new items to my array. This should look like this:
addState(){
let arr = this.state.cars;
arr.push('skoda');
this.setState({cars: arr});
}
I have found this solution thanks to duwalanise. All I had to do was to return the new array in order to push new items. I was facing this kind of issue for a lot of time. I will try more functions to see if it really works for all functions that normally won't. If anyone have a better idea how to achieve this with a cleaner code, please feel free to reply to my post.
The correct way to mutate your state when you want to push something to it is to do the following. Let's say we have defined a state as such:
const [state, setState] = useState([])
Now we want to push the following object into the state array. We use the concat method to achieve this operation as such:
let object = {a: '1', b:'2', c:'3'}
Now to push this object into the state array, you do the following:
setState(state => state.concat(object))
You will see that your state is populated with the object.
The reason why concat works but push doesn't is because of the following
Array.prototype.push() adds an element into original array and returns an integer which is its new array length.
Array.prototype.concat() returns a new array with concatenated element without even touching in original array. It's a shallow copy.
VIEW:
I have a rows repeating , with a save button on each row to save each object individually. I want this button to be disabled if no changes have been made.
<tr ng-repeat="option in options | filter:search">
<a ng-click="save(option)" ng-disabled="isUnchanged(option)">Save</a>
</tr>
CONTOLLER:
So I pass the option object to the function, I get its index position in the array. Then compare this 'option' object to its original self in apiKeyOptions[index] which is injected as a service.
angular.module('PartOfApp')
.controller('PartOfAppCtrl', function( $scope, ... apiKeyOptions) {
$scope.options = apiKeyOptions;
$scope.isUnchanged = function(option) {
var index = $scope.options.indexOf(option);
//compare object to the original
if(option.value == apiKeyOptions[index].value && apiKeyOptions[index].setting == option.setting){
//then no changes have been made to this
return true;
}else{
return false;
}
For some reason I get a console of 100's of errors when any data is changed, saying that the apiKeyOptions[index].value and apiKeyOptions[index].setting are undefind.
The app works perfectly as it should returning true if they are the same but still throws a
TypeError: Cannot read property 'value' of undefined
on apiKeyOptions[index]
if I console.log(apiKeyOptions[index].value) I get no undefined values and all log correctly.
Im guessing Im breaking some angular rules, if anyone could help that would be great.
apiKeyOptions overview:
apiKeyOptions is an array of up to 50 objects
each object is in the form
{
defaultValue: boolean,
description: null,
name: String,
setting: "Default" or Boolean,
value: Boolean
}
Added after comment below:
If I add-
console.log(index);
console.log(apiKeyOptions[index]);
to the function $scope.isUnchanged, I get the expected results
example :
13
Object {name: "LOREM IPSUM", description: null, defaultValue: false, setting: "default", value: false…}
So index is not always -1. The reason I pass the object to the function and not $index is because of the filter | search so the index will change depending on the search.
FIXED
As shown in the answer below . I was getting a index = -1 error but its was buried in 100's of CORRECT log outputs.
Oddly this did not stop the app from working and I will need to have a deeper look into how ng-disabled is bound to a value. To fix I simply replaced the indexOf with
for (var i = 0; i< $scope.options.length; i++ ){
if($scope.options[i].name == option.name){
var index = i;
}
}
The problem seems to be with the parameter passed to $scope.isUnchanged = function(option) {
Since ng-repeat creates a new scope for each loop, i suspect that the 'option' available to each loop would be a new object and will not have a reference to 'options' array.
<tr ng-repeat="option in options | filter:search">
Therefore your isUnchanged function will receive parameter as a new object and hence below code always returns -1. Because indexOf matches the given argument in the array and since the argument 'option' is an object and doesn't refer(reference comparison) the same element of array hence no match will found. i.e var a = {id:1};var b = [a]; b.indexOf({id:1}) === -1; b.indexOf(a) === 0;
var index = $scope.options.indexOf(option);//always be -1 in your case
// therefore apiKeyOptions[index] will always be undefined
As a workaround you should pass $index to isUnchanged from the view.