Я новичок в React Js, у меня есть форма, в которой я получаю строку от пользователя, я разбиваю эту строку на пробелы, затем сохраняю ее в своей переменной состояния, этот код работает нормально:
handleSubmit(event) {
event.preventDefault();
let products = this.state.newProducts;
//split the string into an array
var string = this.state.QrCode.split(" ");
// check all the string in the form if the Qr match an exsitant => save it
for(var i =0; i < string.length; i++){ // fetch the entery
this.state.smth.map(prd => { // fetch the database
if(prd.Qr==string[i]){ // verification
products.push({
id: prd.id,
Qr: prd.Qr,
Stat: prd.Stat,
name: prd.name
});
}
}
)
}
// save it in newProducts
this.setState({
newProducts: products
});
// refresh the grid
this.grid.refresh();
}
, тогда я просто попытался добавить переменную в pu sh данные в массиве, если они действительно существуют "smth", но получаю эту ошибку: Ошибка анализа: унаследованные восьмеричные литералы не допускаются в строгом режиме.
handleSubmit(event) {
event.preventDefault();
let hasChanged = 0;
let products = this.state.newProducts;
//split the string into an array
var string = this.state.QrCode.split(" ");
// check all the string in the form if the Qr match an exsitant => save it
for(var i =0; i < string.length; i++){ // fetch the entery
this.state.smth.map(prd => { // fetch the database
if(prd.Qr==string[i]){ // verification
hasChanged=1;
products.push({
id: prd.id,
Qr: prd.Qr,
Stat: prd.Stat,
name: prd.name
});
}
if(hasChanged==0){
products.push({
id: 00,
Qr: string[i],
Stat: "unknown",
name: "XXXX"
});
}
}
)
}
// save it in newProducts
this.setState({
newProducts: products
});
// refresh the grid
this.grid.refresh();
}
, так что 1-й код работает нормально, но он действительно добавляет строку без соответствия, и это то, что я хочу добавить, надеюсь, я могу получить некоторую помощь. спасибо.