【问题标题】:How to modify properties in array如何修改数组中的属性
【发布时间】:2019-07-29 13:24:32
【问题描述】:

我正在尝试修改数组中的属性并在其下添加新元素。

数组取自 - https://jsonplaceholder.typicode.com/users

我的代码只是添加了一个新属性而不是修改它们

我尝试了以下代码(通过使用 axios)

const axios = require('axios');

const getData = async() => {
    let {data: myData} = await axios.get('https://jsonplaceholder.typicode.com/users')

    myData.forEach((e) => {
        myData.push({
            phone: {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
        })
    })
    console.log(myData)
}

我想要得到 -

{
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "Sincere@april.biz",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone":{
     "phoneNumber": "1-770-736-8031 x56442",
      "phoneType": ''
},
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },

但我得到了

[ { id: 1,
    name: 'Leanne Graham',
    username: 'Bret',
    email: 'Sincere@april.biz',
    address:
     { street: 'Kulas Light',
       suite: 'Apt. 556',
       city: 'Gwenborough',
       zipcode: '92998-3874',
       geo: [Object] },
    phone: '1-770-736-8031 x56442',
    website: 'hildegard.org',
    company:
     { name: 'Romaguera-Crona',
       catchPhrase: 'Multi-layered client-server neural-net',
       bs: 'harness real-time e-markets' } },

  { phoneNumber: '1-770-736-8031 x56442', phoneType: '' },
]

【问题讨论】:

  • 为什么你有 两个 myData.forEach 循环在这里开始?
  • 这是我忘记删除的代码的一部分。刚刚更正了,抱歉。
  • 没有“json 数组”这样的东西。 JSON 是一种文本格式。一旦你从 axios 得到它,它只是一个数组。
  • myData 是一个数组,不知道为什么您希望在您的forEach 之后得到其他东西

标签: javascript arrays node.js


【解决方案1】:

你可以试试这个:

myData.forEach((e, index) => {
        myData[index].phone =  {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
    })

myData.forEach((e, index) => {
        e.phone =  {
                "phoneNumber": e.phone,
                "phoneType": ''
            }
    })

【讨论】:

    【解决方案2】:

    myData 是一个数组,因此通过调用 myData.push 可以向数组中添加一个对象。

    使用 forEach 将为您提供变量 e 中数组中的每个对象,因此您需要使用 e

    当你想更新数组中的对象时,你可能想要这样:

    const getData = async() => {
        let {data: myData} = await axios.get('https://jsonplaceholder.typicode.com/users')
    
        myData.forEach((e) => {
            e.phone = {
                    "phoneNumber": e.phone,
                    "phoneType": ''
                }
        })
        console.log(myData)
    }
    

    【讨论】:

      【解决方案3】:

      您想要转换现有的phone 属性而不是将新对象推送到数组中

      myData.forEach((e) => {
          e.phone = {
                  "phoneNumber": e.phone,
                  "phoneType": ''
              };       
      });
      

      【讨论】:

        【解决方案4】:

        使用地图数组函数

        const data = myData.map(user => ({ ...user, phone: {
            phoneNumber: user.phone,
            phoneType: ''
        }}));
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-27
          • 1970-01-01
          • 2013-05-17
          • 1970-01-01
          • 2012-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多