【问题标题】:need to pass an array with an object inside it需要传递一个里面有一个对象的数组
【发布时间】:2019-03-27 12:49:59
【问题描述】:
  • 在我的 post 请求中,我需要传递一个包含对象的数组。
  • 当我尝试在对象中添加新属性时,它正在添加。
  • 但是当我试图添加一个对象存在于数组中时,它没有添加。 我有 sportsvalues 作为数组 const sportsValues = [{ ...values }];
  • 我正在尝试构建这样的东西,以便我可以传入 api [ { "playerName": 3, "playerHeight": 1 } ]
  • 你能告诉我如何解决它吗?
  • 在下面提供我的代码 sn-p。
export function sports(values) {
    const sportsValues = [{ ...values }];
    sportsValues.push(playerName:'3');
    console.log("sportsValues--->", sportsValues);

    // sportsValues.playerName = 3//'';
    // sportsValues.playerHeight = 1//'';
    console.log("after addition sportsValues--->", sportsValues);
    console.log("after deletion sportsValues--->", sportsValues);

    return dispatch => {
        axios
            .post(`${url}/sport`, sportsValues)
            .then(() => {
                return;
            })
            .catch(error => {
                alert(`Error\n${error}`);
            });
    };
}

【问题讨论】:

    标签: javascript html arrays reactjs redux


    【解决方案1】:

    由于sportsValues 是一个对象数组,您可以将新对象推入其中。查看下面的代码。

    const sportsValues = [];
    sportsValues.push({ 
      playerName:'3',
      playerHeight: 1,
    });
    
    console.log(sportsValues);

    【讨论】:

      【解决方案2】:

      我不完全理解您要做什么,但这里有一些提示:

      如果你想更新数组中的对象,你首先必须选择数组中的对象,然后更新它的属性:

      sportsValues[0].playerName = 3

      虽然,我建议先正确构建对象,然后将其传递给数组,在我看来这样更容易理解:

      const sportsValues = [];
      const firstValue = { ...values };
      firstValue.playerName = '3';
      sportsValues.push(firstValue);
      

      const firstValue = { ...values };
      firstValue.playerName = '3';
      const sportsValues = [firstValue];
      

      const sportsValues = [{
        ...values,
        playername: '3',
      }];
      

      如果您尝试向数组中添加新对象,您可以这样做:

      const sportsValues = [{ ...values }];
      sportsValues.push({ playerName: '3' });
      etc...
      

      Array.push 将一个新项目添加到数组中,因此在您的代码中,您将拥有 2 个项目,因为您在开始时分配了 1 个项目,然后推送一个新项目:

      const ar = [];
      // []
      ar.push('item');
      // ['item']
      ar.push({ text: 'item 2' });
      // ['item', { text: 'item 2' }]
      

      等等……

      【讨论】:

        【解决方案3】:
        export function sports(values) {
          const sportsValues = [{ ...values }];
          sportsValues.push(playerName:'3');
          let playerName='3' 
          sportsValues.playerName= playerName; // you can bind in this way
          console.log("sportsValues--->", sportsValues);
        
          return dispatch => {
            axios
                .post(`${url}/sport`, sportsValues)
                .then(() => {
                    return;
                })
                .catch(error => {
                    alert(`Error\n${error}`);
                });
          };
        }
        

        【讨论】:

          猜你喜欢
          • 2017-02-21
          • 2020-05-15
          • 1970-01-01
          • 2015-11-12
          • 1970-01-01
          • 2021-06-11
          • 1970-01-01
          • 1970-01-01
          • 2013-08-01
          相关资源
          最近更新 更多