[英]can i change the state of property of objects inside an array?
I want to change the matched property of objects inside an array but my function don't success to accomplish it this is the function please help me to fix the problem:我想更改数组内对象的匹配属性,但我的 function 没有成功完成它这是 function 请帮我解决问题:
let state = [
{id: 0, contents: 'Provider', visible: false, matched: false},
{id: 1, contents: 'Provider', visible: false, matched: false},
{id: 2, contents: 'selector', visible: false, matched: false},
{id: 3, contents: 'selector', visible: false, matched: false},
{id: 4, contents: 'useSelector()', visible: false, matched: false},
{id: 5, contents: 'useSelector()', visible: false, matched: false},
{id: 6, contents: 'useDispatch()', visible: false, matched: false},
{id: 7, contents: 'useDispatch()', visible: false, matched: false},
{id: 8, contents: 'Pure Function', visible: false, matched: false},
{id: 9, contents: 'Pure Function', visible: false, matched: false},
{id: 10, contents: 'react-redux', visible: false, matched: false},
{id: 11, contents: 'react-redux', visible: false, matched: false},
];
const cardFlip = (id) => {
let flipState = [...state];
const cardID = id;
flipState[cardID] = {...state[cardID], visible:true}
const [index1, index2] = flipState.filter(card => card.visible).map(card => card.id);
if (index2 !== undefined){
const card1 = flipState[index1];
const card2 = flipState[index2];
if (card1.contents === card2.contents) {
flipState[index1] = {...card1, matched: true}
flipState[index2] = {...card2, matched: true}
}
}
return flipState }
This is the result finally:这是最终的结果:
cardFlip(3)
(12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {id: 0, contents: 'Provider', visible: false, matched: false}
1: {id: 1, contents: 'Provider', visible: false, matched: false}
2: {id: 2, contents: 'selector', visible: false, matched: false}
3: {id: 3, contents: 'selector', visible: true, matched: false}
4: {id: 4, contents: 'useSelector()', visible: false, matched: false}
5: {id: 5, contents: 'useSelector()', visible: false, matched: false}
6: {id: 6, contents: 'useDispatch()', visible: false, matched: false}
7: {id: 7, contents: 'useDispatch()', visible: false, matched: false}
8: {id: 8, contents: 'Pure Function', visible: false, matched: false}
9: {id: 9, contents: 'Pure Function', visible: false, matched: false}
10: {id: 10, contents: 'react-redux', visible: false, matched: false}
11: {id: 11, contents: 'react-redux', visible: false, matched: false}]
to update one object with new Values try this,用新值更新一个 object 试试这个,
function updateFieldsInOneObjectInTheArray(
state,
itemKey,
newContents,
newVisible,
newMached
) {
const newState = state;
const id = state?.[itemKey]?.id;
const contents = newContents || state?.[itemKey]?.contents;
const visible = newVisible || state?.[itemKey]?.visible;
const matched = newMached || state?.[itemKey]?.visible;
const record = {
id: id,
contents: contents,
visible: visible,
matched: matched,
};
newState[itemKey] = record;
return newState;
}
Every element inside flipState
has the property visible set as false except for the one you change at the beginning of your function.除了您在 function 开头更改的元素之外,
flipState
中的每个元素都将属性 visible 设置为 false。 Because of this, after doing flipState.filter(card => card.visible)
it will return an array with a single element so this destructuring you're doing const [index1, index2] = flipState.filter(card => card.visible).map(card => card.id);
因此,在执行
flipState.filter(card => card.visible)
之后,它将返回一个包含单个元素的数组,因此您正在执行此解构const [index1, index2] = flipState.filter(card => card.visible).map(card => card.id);
will always return a value for index1
and undefined for index2
which means the if will always evaluate to false, meaning matched wont change for any element.将始终返回
index1
的值和未定义的index2
,这意味着 if 将始终评估为 false,这意味着任何元素的匹配都不会改变。
I'm not sure exactly what you intend to do but if you want this specific function to work you will have to have at least 2 elements with visible
as true.我不确定你到底打算做什么,但如果你想让这个特定的 function 工作,你必须至少有 2 个
visible
元素。
something like this?像这样的东西?
let state = [ {id: 0, contents: 'Provider', visible: false, matched: false}, {id: 1, contents: 'Provider', visible: false, matched: false}, {id: 2, contents: 'selector', visible: false, matched: false}, {id: 3, contents: 'selector', visible: false, matched: false}, {id: 4, contents: 'useSelector()', visible: false, matched: false}, {id: 5, contents: 'useSelector()', visible: false, matched: false}, {id: 6, contents: 'useDispatch()', visible: false, matched: false}, {id: 7, contents: 'useDispatch()', visible: false, matched: false}, {id: 8, contents: 'Pure Function', visible: false, matched: false}, {id: 9, contents: 'Pure Function', visible: false, matched: false}, {id: 10, contents: 'react-redux', visible: false, matched: false}, {id: 11, contents: 'react-redux', visible: false, matched: false}, ]; const cardFlip = (id) => { const flipState = state.map((el)=>{ if(el.id===id){ el.matched=.el;matched. //el.visible=;el,visible. if you also want to flip visible. I'm not sure if you want to. return el }else return el }) return flipState } console.log(JSON.stringify(cardFlip(3)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.