reactjs 我正在尝试将数组中的响应保存到对象中

nuypyhwy  于 2023-03-01  发布在  React
关注(0)|答案(7)|浏览(104)
let response = [
    {
      id: 1,
      user_name: 'abc',
      phone_number: 1234567890
    },
    {
      id: 2,
      user_name: 'def',
      phone_number: 9823437483
    },
    {
      id: 3,
      user_name: 'ghi',
      phone_number: 9834763467
    }];

我正在尝试将响应保存在{abc:{ id: 1, user_name: 'abc', phone_number: 1234567890}, def: {id: 2, user_name: 'def', phone_number: 9823437483}}之类的对象中

am46iovg

am46iovg1#

您可以对response数组使用reduce()方法来创建一个对象,其中每个键都是user_name,对应的值是该用户的整个对象。

    • 代码:**
let response = [
    {
      id: 1,
      user_name: 'abc',
      phone_number: 1234567890
    },
    {
      id: 2,
      user_name: 'def',
      phone_number: 9823437483
    },
    {
      id: 3,
      user_name: 'ghi',
      phone_number: 9834763467
    }];

let obj = response.reduce((acc, curr) => {
  acc[curr.user_name] = curr;
  return acc;
}, {});

console.log(obj);
chhqkbe1

chhqkbe12#

你可以用一个简单的for循环把它转换成一个对象:

let resObj = {}
for (let item of response) {
   resObj[item.user_name] = item
}

结果:

{
    "abc": {
        "id": 1,
        "user_name": "abc",
        "phone_number": 1234567890
    },
    "def": {
        "id": 2,
        "user_name": "def",
        "phone_number": 9823437483
    },
    "ghi": {
        "id": 3,
        "user_name": "ghi",
        "phone_number": 9834763467
    }
}
lfapxunr

lfapxunr3#

可以使用reduce并执行以下操作:

const object = {};
return array.reduce((prev, curr) => {
    return {
      ...prev,
      [curr[curr.user_name]]: curr,
    };
}, object);
xbp102n0

xbp102n04#

你可以使用forEach loop

let response = [{
    id: 1,
    user_name: 'abc',
    phone_number: 1234567890
  },
  {
    id: 2,
    user_name: 'def',
    phone_number: 9823437483
  },
  {
    id: 3,
    user_name: 'ghi',
    phone_number: 9834763467
  }
];

const response_obj = {}
response.forEach(item => {
  response_obj[item.user_name] = {
    id: item.id,
    user_name: item.user_name,
    phone_number: item.phone_number
  }
})

或者你可以filter这个响应。比如

let response = [{
     id: 1,
     user_name: 'abc',
     phone_number: 1234567890
   },
   {
     id: 2,
     user_name: 'def',
     phone_number: 9823437483
   },
   {
     id: 3,
     user_name: 'ghi',
     phone_number: 9834763467
   }
 ];

 function getItemByUserName(userName) {
   return response.filter(item => 
     item.user_name == userName
   )[0]
 }

 console.log(getItemByUserName("def"))

注意!:user_name是否唯一没有逻辑

xsuvu9jc

xsuvu9jc5#

检查以下内容:

let response = [
    {
      id: 1,
      user_name: 'abc',
      phone_number: 1234567890
    },
    {
      id: 2,
      user_name: 'def',
      phone_number: 9823437483
    },
    {
      id: 3,
      user_name: 'ghi',
      phone_number: 9834763467
    }];

const data ={};
response.forEach(item=>{
    data[item.user_name] = {...item};
})

console.log(data);
pgpifvop

pgpifvop6#

假设你想把你的数据转换成对象,你可以使用reduce来实现,在结果对象中,原始数组中的每个对象都被赋给一个key,这个key是原始对象的user_name的值:

let response = [
  {
    id: 1,
    user_name: 'abc',
    phone_number: 1234567890
  },
  {
    id: 2,
    user_name: 'def',
    phone_number: 9823437483
  },
  {
    id: 3,
    user_name: 'ghi',
    phone_number: 9834763467
  }
];

let result = response.reduce((acc, curr) => (acc[curr.user_name] = curr, acc), {});
console.log(result);
qnakjoqk

qnakjoqk7#

Reduce方法对于数据转换非常有用。Read about Reduce method here !

let response = [
  {
    id: 1,
    user_name: "abc",
    phone_number: 1234567890,
  },
  {
    id: 2,
    user_name: "def",
    phone_number: 9823437483,
  },
  {
    id: 3,
    user_name: "ghi",
    phone_number: 9834763467,
  },
];

const result = response.reduce((acc, userDetail) => {
  acc[userDetail.user_name] = {
    ...userDetail,
  };
  return acc;
}, {});

console.log(result);

相关问题