从API结果覆盖索引数组

时间:2019-06-01 18:10:48

标签: javascript arrays reactjs redux state

在redux状态下覆盖数组的索引时出现问题:/

我正在从端点接收数据,结果是:

例如

partial class WinControl
    {       
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code
        private void InitializeComponent()
        {
            this.HostedLabel = new System.Windows.Forms.Label();
            this.txtHost = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // HostedLabel
            // 
            this.HostedLabel.AutoSize = true;
            this.HostedLabel.Location = new System.Drawing.Point(15, 18);
            this.HostedLabel.Name = "HostedLabel";
            this.HostedLabel.Size = new System.Drawing.Size(67, 13);
            this.HostedLabel.TabIndex = 0;
            this.HostedLabel.Text = "HostedLabel";
            // 
            // txtHost
            // 
            this.txtHost.Location = new System.Drawing.Point(107, 18);
            this.txtHost.Name = "txtHost";
            this.txtHost.Size = new System.Drawing.Size(161, 20);
            this.txtHost.TabIndex = 1;
            // 
            // WinControl
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.txtHost);
            this.Controls.Add(this.HostedLabel);
            this.Name = "WinControl";
            this.Size = new System.Drawing.Size(284, 50);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label HostedLabel;
        private System.Windows.Forms.TextBox txtHost;

但是我想根据结果对象更改数组的索引。

export default ChildComponent => {
    class ComposedComponent extends Component {

        componentWillMount() {
            this.shouldNavigateAway();
        }
        componentWillUpdate() {
            this.shouldNavigateAway();
        }
        shouldNavigateAway() {
            if (!this.props.authenticated) {
                this.props.history.push('/')
            }
        }

        render() {
            return <ChildComponent {...this.props} />
        }
    }
}

然后将它们保存到减速器中。

这是从端点返回的确切数据。 enter image description here

我尝试过

import { createStackNavigator, createAppContainer} from 'react-navigation';
import  Home  from './views/Home';
import Favoriti from './views/Favoriti';

const MainNavigator = createStackNavigator({

    FavoritiRT: Favoriti,
    HomeRT: Home
  },
  {
    initialRouteName: "HomeRT"
  }
);

const MyRoutes = createAppContainer(MainNavigator);

export default MyRoutes;

得到了

的结果
data: 
 [{
    0: {id: 123, name: test1 },
    1: {id: 456, name: test2 },
    3: {id: 789, name: test3 },
 }]

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以使用df['T'].iat[x] = df['PL'].iat[x]mapObject.entries

Object.fromEntries

或者您可以使用let data = [{0: { id: 123,name: 'test1'},1: {id: 456,name: 'test2'}, 3: {id: 789,name: 'test3'},}] let op = data.map(val => Object.fromEntries(Object.entries(val).map(([key, value]) => [value.id, value]))) console.log(op)mapObject.values

reduce

答案 1 :(得分:0)

必须结合使用Array.map()Object.values()Array.reduce()

const data = [{

0: {id: 123, name: "test1" },

1: {id: 456, name: "test2" },

3: {id: 789, name: "test3" },

}];

const output = data.map(obj => Object.values(obj).reduce((acc, a) => ({ ...acc, [a.id]: a }), {}));

console.log(output);