提问人:user1450966 提问时间:11/7/2023 更新时间:11/7/2023 访问量:26
无法获取数组 React JS 中元素的索引
unable to get the index of element in array react js
问:
const editAddHandler = (event, key) => {
const db = getDatabase();
onValue(ref(db, `users/${userId}/addressData`), (snapshot) => {
const addresslist = [];
snapshot.forEach((childSnapshot) => {
const keyName = childSnapshot.key;
const data = childSnapshot.val();
addresslist.push({ key: keyName, data: data });
});
props.setSavedAddress(addresslist);
});
console.log("array of element",props.savedAddress)
const clickedAddress = props.savedAddress.findIndex(element => element === key)
console.log("clicked key",clickedAddress)
const keyValue = props.savedAddress[key];
console.log("keyvalue",keyValue)
console.log("key",key)
if(key === props.savedAddress[key]) {
setValues({
name: props.savedAddress[key].data.name,
phone: props.savedAddress.phone,
pincode: props.savedAddress.pincode,
address: props.savedAddress.address,
city: props.savedAddress.city,
state: props.savedAddress.state,
landmark: props.savedAddress.landmark,
defaultAdd: props.savedAddress.defaultAdd,
})
}
props.showAddressHandler();
};
无法获取 props.savedAddress 列表的索引,需要比较 index 和 key onClick 事件。
Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业标准虚拟文本,
答:
0赞
user1450966
11/7/2023
#1
const clickedAddress = props.savedAddress.findIndex((element,i) => i === 键)
这将给出索引
评论
const clickedAddressIndex = addresslist.findIndex(element => element.key === key);
addresslist
===
if(key === props.savedAddress[key])