简体   繁体   English

为什么我的子数组没有被正确推入父数组? ReactJS

[英]Why are my subarrays not being pushed correctly into parent array? ReactJS

So i am implementing bubblesort algorithm using ReactJS.所以我正在使用 ReactJS 实现冒泡排序算法。 In my state, i have an array of 3 objects initially in descending order by the 'num' property.在我的状态下,我有一个包含 3 个对象的数组,它们最初按 'num' 属性按降序排列。 I have a button on my screen to click which i want to run the bubblesort function i created.我的屏幕上有一个按钮可以单击我想运行我创建的冒泡排序功能的按钮。 I want to keep track of each iteration of the bubblesort and add it to a parent array.我想跟踪冒泡排序的每次迭代并将其添加到父数组中。 However, after i run my bubblesort function, it console logs the parent array with subarrays but all the subarrays are showing sorted order instead of showing each iteration.但是,在我运行冒泡排序函数后,它控制台记录带有子数组的父数组,但所有子数组都显示排序顺序,而不是显示每次迭代。 Can anyone see what i am not understanding?谁能看到我不明白的地方? Thanks.谢谢。

EDIT: Updated Code Snippet.编辑:更新代码片段。 I have fixed issue i was having and am now logging each iteration of bubblesort.我已经解决了我遇到的问题,现在正在记录冒泡排序的每次迭代。 I have the 'compare' property of each object set to 'no' initially but want to set which two are being compared to 'yes' in each iteration (subarray).我最初将每个对象的“比较”属性设置为“否”,但想设置在每次迭代(子数组)中将哪两个与“是”进行比较。 I haven't been able to solve this.我一直无法解决这个问题。 Every objects' compare property in every iteration is getting set to 'yes' instead of just the two being compared.每次迭代中每个对象的比较属性都设置为“是”,而不仅仅是比较两个对象。 Any ideas?有任何想法吗?

EDIT: Update Code Snippet.编辑:更新代码片段。 Have a working solution.有一个有效的解决方案。 But is there a better solution than having to JSON.parse(JSON.stringify()) multiple times?但是有没有比多次 JSON.parse(JSON.stringify()) 更好的解决方案? Thanks.谢谢。

 class TodoApp extends React.Component { constructor(props) { super(props) this.state = { array: [ { index: 0, num: 6, compare: 'no'}, { index: 1, num: 5, compare: 'no'}, { index: 2, num: 4, compare: 'no'}, { index: 3, num: 3, compare: 'no'}, { index: 4, num: 2, compare: 'no'}, { index: 5, num: 1, compare: 'no'}, ] } this.onBtnClick = this.onBtnClick.bind(this); } onBtnClick() { let copy = JSON.parse(JSON.stringify(this.state.array)); let output = [JSON.parse(JSON.stringify(copy))]; for(let i = 0; i < copy.length; i++) { for (let j = 0; j < copy.length-i-1; j++) { copy.forEach(item => { item.compare = 'no' }); copy[j].compare = 'yes'; copy[j + 1].compare = 'yes'; if (copy[j].num > copy[j + 1].num) { let temp = copy[j + 1]; copy[j + 1] = copy[j]; copy[j] = temp; } output.push(JSON.parse(JSON.stringify(copy))); } } console.log(output); } render() { return ( <div> <h2>Test</h2> <button type='button' onClick={this.onBtnClick}>Click Me to console log</button> </div> ) } } ReactDOM.render(<TodoApp />, document.querySelector("#app"))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

You were logging it outside both the loops whereas you should log it inside the first loop but outside second one to see each iteration.您将其记录在两个循环之外,而您应该将其记录在第一个循环内但在第二个循环之外以查看每次迭代。

 class TodoApp extends React.Component { constructor(props) { super(props) this.state = { array: [{ index: 0, num: 3, otherProp: 'test' }, { index: 1, num: 2, otherProp: 'test' }, { index: 2, num: 1, otherProp: 'test' }, ] } this.onBtnClick = this.onBtnClick.bind(this); } onBtnClick() { let copy = JSON.parse(JSON.stringify(this.state.array)); let newCopy = [copy]; for (let i = 0; i < copy.length; i++) { for (let j = 0; j < copy.length - i - 1; j++) { copy[j].otherProp = 'notest'; if (copy[j].num > copy[j + 1].num) { let temp = copy[j + 1]; copy[j + 1] = copy[j]; copy[j] = temp; } newCopy.push(copy); } console.log(newCopy); } } render() { return ( < div > < h2 > Test < /h2> < button type = 'button' onClick = { this.onBtnClick } > Click Me to console log < /button> < /div> ) } } ReactDOM.render( < TodoApp / > , document.querySelector("#app"))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="app"></div>

数组是 js 中的引用类型,因此如果您将相同的数组保存在一个数组中,但在最后控制台输出最后一个数组将被多次输出,如果您想输出一个历史数组,您应该制作每个的副本将其推入“历史”数组时的数组

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM