【问题标题】:How to change property of object in array javascript (React Native)如何更改数组javascript中对象的属性(React Native)
【发布时间】:2020-04-10 05:24:45
【问题描述】:

我的项目中有一些关于数组的问题,但不知道如何解决

我的数组看起来像:

const [array, setArray] = useState([
    { id: 1, name: 'Luca', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
    { id: 2, name: 'Jin', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
])
 < FlatList
data = { array }
renderItem = {({ item, index }) => {
    return (
        <View>
            <FlatList
                data={item.classes}
                renderItem={({ item, index }) => {
                    return (
                        <View >
                            <TextInput
                                placeholder={''}
                                onChangeText={(text) => onChangeClassName(item, text)}
                                value={item.className}
                            />
                        </View>
                    )
                }}
            ></FlatList>
        </View>
    )
}}
></FlatList >

onChangeClassName

 onChangeClassName = (item, text) => {
        const newArr = arr
        for (let i = 0; i < newArr.length; i++) {
            for (let z = 0; z < newArr[i].classes.length; z++) {
                if (newArr[i].classes[z] === item ) {
                    newArr[i].classes[z].className = text
                   } }]
                }
            }
        }
        setArray([...arr])
    }

所以每个学生都有姓名和班级,每个学生的班级都不同,我使用 textInput 更改它。我的问题是当我为其中一个学生设置 className 时,两个学生相同位置的 className 都发生了变化

例如:在 Luca 的 classes[0] 中点击 -> set text = '7A'

数组会这样改变。

const [array, setArray] = useState([
    { id: 1, name: 'Luca', classes: [{ id: 1, className: '7A', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
    { id: 2, name: 'Jin', classes: [{ id: 1, className: '7A', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
])

任何人有解决方案?请帮助我

【问题讨论】:

  • 首先改变你的命名约定,你的外循环也有项目,内循环也有项目,你需要区分它。如果它仍然不起作用,则问题可能是其他问题。我说的是组件中的循环
  • 最简单的方法是从平面列表中获取...行的索引...然后将其传递给您要更改文本的方法...然后使用索引从数组并改变它......这将是快速的方法......使用循环将在大型数据集中产生问题
  • @AbhishekKulkarni 感谢您的评论,但命名约定没有问题。它仍然可以正常工作,但我会清楚地更改名称。我认为当我将 className 设置为文本时有问题
  • @Pramod 是的,我根本不想使用循环。但我不知道如何以另一种方式做到这一点......

标签: javascript arrays react-native


【解决方案1】:

把你的代码改成这样就可以了:

import React, {useState} from 'react';
import {View,StyleSheet,Image,Button, FlatList, Text, TextInput} from 'react-native';



export default function App(){
    const [array, setArray] = useState([
        { id: 1, name: 'Luca', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
        { id: 2, name: 'Jin', classes: [{ id: 1, className: '', teacherName: '' }, { id: 2, className: '', teacherName: '' }] },
    ])
  const onChangeClassName = ( text,item1,itemID) => {
    let newArr = [...array];
 console.log("text is",text,item1,itemID);
               for(var i=0;i<array.length;i++){

                   if(array[i].name == item1)
                   {
                    for(var j=0;j<array[i].classes.length;j++){
                        if(array[i].classes[j].id == itemID)
                        {
                           newArr[i].classes[j].className = text;
                        }
                    }
                   }
               }
               setArray(newArr);
               console.log("array is",array);
        }


  return(
    <View style={styles.div}>
      <FlatList
        data={array}
        renderItem={({ item,index }) => {
        let item1 = item.name
    return(
        <View>
        <Text>{item.name}</Text>
        <FlatList
        data={item.classes}
        renderItem={({ item }) => 

        { let itemID = item.id;
        return(
            <TextInput style={{ width:"85%",height: 30, borderColor: "black", borderBottomWidth: 2}}
            placeholder="enter something"
            onChangeText={(text) => onChangeClassName( text,item1,itemID)}
            value={item.className}
        />
        )
        }}
        keyExtractor={item => item.id}
      />
        </View>
    )
    } }
        keyExtractor={item => item.id}
      />
    </View>
  );

}
const styles = StyleSheet.create({
  div: {
    flex:1,
    marginLeft:20,
    backgroundColor: 'white',
    marginTop:"30%"
  },


});

在 TextInput 中输入一些内容后

输入一些值后我的数组如下所示:

const [array, setArray] = useState([
    { id: 1, name: 'Luca', classes: [{ id: 1, className: '7A', teacherName: '' }, { id: 2, className: '7B', teacherName: '' }] },
    { id: 2, name: 'Jin', classes: [{ id: 1, className: '8A', teacherName: '' }, { id: 2, className: '8B', teacherName: '' }] },
])

希望这会有所帮助!

【讨论】:

  • tks 为您解答,我尝试以任何方式获取数组行并与 newArr[i] 进行比较。当我 console.log 只有一个案例成功。所以我认为我在设置 newArr[i].classes[z].className = text 时遇到了麻烦。但我不知道如何解决...... :(
  • 我已经编辑了我的答案,您可以通过使用索引检查这一次来更改文本
  • 同样的问题兄弟 :(,我想我的错误是在 FlatList 中使用 FlatList
  • 不,你可以在 FlatList 中使用 FlatList 我已经处理过你的代码,这里我正在编写一个工作示例检查一次。
  • tkank 你,我解决了我的问题。我的问题是我从另一个屏幕得到的数组,而不是文本输入。
猜你喜欢
  • 2018-09-23
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 2021-08-22
相关资源
最近更新 更多