简体   繁体   English

我可以更改数组内对象属性的 state 吗?

[英]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.

相关问题 我如何访问在react状态下设置的对象数组中的属性? - How can I access a property in an array of objects that is set in the state in react? JavaScript:如何更改数组中对象的属性名称? - JavaScript: How can I change property names of objects in an array? reactjs 中的对象数组 state 中 object 的不可变更改数组 - Immutable change array of object inside array of objects state in reactjs 如何从处于反应状态的数组内的对象中删除属性 - How to remove property from objects inside array in react state 如何在 React state 的对象数组中更改一个 object 的属性? - How to change property of one object in an array of objects in React state? 如何定位数组中的对象子集并将属性更改为每个对象的递增数字? - How can I target subset of objects in array and change a property to an incrementing number for each of those objects? 我可以使用用户在操作调用期间传入的值来更改 redux 状态内的属性值吗? - Can I change the value of a property inside a redux state using a value passed in during the action call by the user? 更改数组内部特定对象的属性的状态值 - Change the state value of the property of a particular object that is inside an array Javascript - 深度更改对象数组内的属性值? - Javascript - Deeply change property value inside array of objects? 如何在不改变容器的情况下更改数组值(对象数组内部)? javascript - How can I change array value(inside array of objects) without mutating the container? javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM