简体   繁体   English

如何存储以前的值而不是将值存储在反应状态的对象数组中

[英]How can I store previous values instead of storing the value in array of objects in react state

I am storing the prev values in an array of objects, for example [{ActFollow: 'BlN'},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}] I want to store the key and values in an object like this {ActFollow: 'BlN',ActSendGift: 'BlY', ActSubscribe: 'BlY'}我将 prev 值存储在对象数组中,例如[{ActFollow: 'BlN'},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}]我想将键和值存储在对象中像这样 {ActFollow: 'BlN',ActSendGift: 'BlY', ActSubscribe: 'BlY'}

  const [activityTypes, setActivityTypes] = useState<any>([]); // state
 .then((response: any) => {
            setActivityTypes((oldArray: any) => [
              ...oldArray,
              {[item.channelSettingTypeId]: response.settingValue},
            ]);
          });

You can reduce the array of objects into an object.您可以将对象数组缩减为一个对象。

You can do it by spreading (...) the current object into the resultant object, as shown below:您可以通过将 (...) 当前对象传播到结果对象中来做到这一点,如下所示:

 const arrOfObjs = [{ ActFollow: "BlN" }, { ActSendGift: "BlY" }, { ActSubscribe: "BlY" }], obj = arrOfObjs.reduce((res, o) => ({ ...res, ...o }), {}); console.log(obj);

You can also do it using Object.assign , as shown below:您也可以使用Object.assign来完成,如下所示:

 const arrOfObjs = [{ ActFollow: "BlN" }, { ActSendGift: "BlY" }, { ActSubscribe: "BlY" }], obj = arrOfObjs.reduce((res, o) => Object.assign(res, o), {}); console.log(obj);

How about this, if the nesting is only one level deep这个怎么样,如果嵌套只有一层深

 const data = [{ActFollow: 'BlN',ActSendGift: 'BlY', ActSubscribe: 'BlY'}] console.log([{...data[0],"hey" : "world"}])

 const items = [{ActFollow: 'BlN'},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}] let object = {} items.forEach(item=>{ for (const [key, value] of Object.entries(item)) { object = { ...object, [key]: value } } }) console.log(object)

You can use this simple idea in React also.你也可以在 React 中使用这个简单的想法。 Just hold on the default empty object in state and update the object.只需保持状态中的默认空对象并更新对象即可。

Use Spread Operator使用展开运算符

 const items = [{ActFollow: 'BlN', Anurag: 26},{ActSendGift: 'BlY'},{ActSubscribe: 'BlY'}] let obj ={} items.forEach((item) => { obj = { ...obj, ...item } }) console.log(obj)

暂无
暂无

声明:本站的技术帖子网页,遵循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? 如何在不覆盖先前状态的情况下将状态更新为对象数组 - How can I update state as an array of objects without overriding previous state 我如何从表单提交的多个输入框中获取值并将这些值作为对象存储在React的状态数组中? - How can i get values from multiple input boxes on form submit and store the values as an object in an state array for React? 如何在 React useState 中存储和更新多个值? 如果其中一个值在数组中? - How can I store and update multiple values in React useState? if one of the value is in array? 如何在 React 状态中更新数组的奇异值 - How can I update singular value of Array in React State React 状态存储整数值的默认值 - Default value for React state storing integer values 我如何切出包含值的所有数组对象并将它们存储在新数组中 - How can I slice out all of the array objects containing a value and storing them in a new array 反应:将数组对象存储在状态中并使用键显示 - React: store array objects in state and display with keys 如何在 React 中的对象数组上 map,然后根据先前的值有条件地渲染组件? - How do I map over an array of objects in React and then conditionally render a component based on a previous value? 如何将所有数组值存储到单个 React state 中? - How to store all array values into a single React state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM