简体   繁体   English

我想在 javascript 中初始化一个对象数组

[英]I want to initialize an array of objects in javascript

when I click new button, I have to clear all information like this.当我点击新按钮时,我必须清除所有这样的信息。 from

testInfo:{
        test1: 12
        test2: "test2"
        ...
        test99: "test99"
    }

to

testInfo:{
        test1: 0 (or "")
        test2: ""
        ...
        test99: ""
    }

any good idea?有什么好主意吗? thanks.谢谢。

for (const key in testInfo) {
  testInfo[key] = typeof testInfo[key] === 'number' ? 0 : '';
}

You may try creating an object from mapped entries like so:您可以尝试从映射条目创建一个对象,如下所示:

 const testInfo = { test1: 12, test2: "test2", test99: "test99", } console.log(Object.fromEntries(Object.keys(testInfo).map(key => [key, ""])));


const toReset = testInfo =>
  Object.fromEntries(Object.keys(testInfo).map(key => [key, '']));

setState({ testInfo: toReset(someState) });

The question uses object syntax, but I think you might want an array.该问题使用对象语法,但我认为您可能需要一个数组。

And if you need pre-created integer properties then you can do this:如果您需要预先创建的整数属性,那么您可以这样做:

 const a = [...Array(99)] // 99 / whatever console.log(JSON.stringify(a))

And if you really don't want an array:如果你真的不想要一个数组:

 const o = {...[...Array(99)]} // 99 / whatever console.log(o)

If the object is not part of your React state:如果对象不是你的 React 状态的一部分:

 // Initialize const testInfo = { test1: 12, test2: "test2", test99: "test99" }; // Update for (const key in testInfo) { testInfo[key] = ""; } console.log(testInfo);

If the object is part of your React state then you always need to create a fresh object from scratch, and set that as the new state.如果对象你的 React 状态的一部分,那么你总是需要从头开始创建一个新对象,并将其设置为新状态。

Example using React Hooks :使用React Hooks 的示例:

// Initialize
const [testInfo, setTestInfo] = useState({ test1: 12, test2: "test2", test99: "test99" });

// Update
const newInfo = testInfo.clone();
for (const key in newInfo) {
  newInfo[key] = "";
}
setTestInfo(newInfo);

Keep a state for your formInfo and a default state value in a constant.保持 formInfo 的状态和常量中的默认状态值。

const defaultTestInfo = {
      test1: 0,
      test2: "",
      test99: "",
    }

const resetFormInfo = () => {
  this.setState({ testInfo: defaultTestInfo })
}

And pass resetFormInfo in your button onClick并在您的按钮onClick传递resetFormInfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在javascript中初始化自定义对象数组 - Initialize array of custom objects in javascript 我想要按对象数组分组,例如 JavaScript 中的特定数组对象 - I want Group By Arrays of Objects like specific array objects in JavaScript Javascript想要将对象推入数组 - Javascript want to push objects into array 如何在Javascript中使用两个空对象初始化数组 - How to initialize an array with two empty objects in Javascript 我想将 2 个数组与 Javascript 结合 - I want to combine 2 array with Javascript 我想将JavaScript对象作为参数传递 - I want to pass JavaScript Objects as Arguments 我想在 javascript 中的特定索引处交换嵌套对象数组中的元素? - I want to swap the elements inside the nested array of objects inside at particular index in javascript? 我想使用HTML中特定ID的Javascript中的循环在数组内显示多个对象 - I want to display multiple objects inside an array using loop in Javascript in a specific id in HTML 我想在JavaScript中使用两个属性布尔值和Int对对象数组进行排序 - I want to sort an array of objects in JavaScript with Two properties Boolean value and Int 我想创建一个 object,其中键作为 object 的属性,值作为与键匹配的对象数组(javascript) - I want to create an object with the key as a property from the object and value as an array of objects matching the key (javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM