简体   繁体   English

在 javascript/React-native 中使用新值更新 object 数组键

[英]Update object array key with new value in javascript/React-native

I have the following Object array stored using AsyncStorage :我使用AsyncStorage存储了以下 Object 数组:

[
  {
    "groupId": 1,
    "id": 1,
    "name": "a",
  },
  {
    "groupId": 2,
    "id": 2,
    "name": "ab",
  },
  {
    "groupId": 3,
    "id": 3,
    "name": "abc",
  },
  {
    "groupId": 2,
    "id": 4,
    "name": "abcd",
  },
]

I'm retrieving the data to display it in a Flatlist but instead of displaying the groupId , I would like to display the groupName for each item.我正在检索数据以将其显示在 Flatlist 中,但我不想显示groupId ,而是想显示每个项目的groupName

I have another key with groups stored:我有另一个存储组的密钥:

[
 {
  "groupName": "g1",
  "id": 1,
 },
 {
  "groupName": "g2",
  "id": 2,
 }
]

I made a function to retrieve the groupName based on groupId .我做了一个 function 来根据groupId检索groupName This works fine:这工作正常:

const getGroupName = async (groupIdToFind) => {
    const existingsGroups = (await AsyncStorage.getItem("groups")) || "[]"; 
    const existingsGroupsParsed = JSON.parse(existingsGroups);

    const groupDataFound = existingsGroupsParsed.find(
      (results) => results.id.toString().indexOf(groupIdToFind) > -1
    );

    if (groupDataFound) {
      return groupDataFound.groupName;
    }
    return false;
  };

And I thought of modifying the data retrieved from the storage in my loadFlatlist function but this is where I'm struggling.我想修改从我的loadFlatlist function 中的存储中检索到的数据,但这是我苦苦挣扎的地方。

const loadFlatlist = async (storageKey) => {
try {
  let itemsOnStorage = await AsyncStorage.getItem(storageKey);

  itemsOnStorage = JSON.parse(itemsOnStorage);

  if (itemsOnStorage !== null) {
    let finalItemsOnStorage = "";
    itemsOnStorage.map(async function (obj) {
      console.log(obj.groupId);  // Correctly returns 1, 2, 3 ...

      let retrievedGroupName = await getGroupName(obj.groupId);
      console.log(retrievedGroupName);   // Correctly returns g1, g2 ...

      finalItemsOnStorage = await update(itemsOnStorage, {
        groupId: { $set: retrievedGroupName },
      });
    });

    console.log(finalItemsOnStorage);  // Returns same content as itemsOnStorage

    setDataOnDevice(itemsOnStorage);  // This will be loaded in my flatlist
  }
} catch (err) {
  alert(err);
}
  };

The problem is in loadFlatlist , I did not manage to replace the groupId by the groupName .问题出在loadFlatlist中,我没有设法将groupId替换为groupName finalItemsOnStorage has the same content as itemsOnStorage . finalItemsOnStorageitemsOnStorage具有相同的内容。

Can you see what is wrong with my function?你能看出我的 function 有什么问题吗? Is there a better way to do this?有一个更好的方法吗?

Try like this像这样试试

const loadFlatlist = async (storageKey) => {
try {
  let itemsOnStorage = await AsyncStorage.getItem(storageKey);
  itemsOnStorage = JSON.parse(itemsOnStorage);

  const existingsGroups = (await AsyncStorage.getItem("groups")) || "[]"; 
  const existingsGroupsParsed = JSON.parse(existingsGroups);

  if (itemsOnStorage !== null) {
    let finalItemsOnStorage = [];

    itemsOnStorage.forEach(item => {

       let index = existingsGroupsParsed.findIndex(obj => obj.id === 
     item.groupId)

            if(index != -1) {
                finalItemsOnStorage.push({...item,
                              groupId:existingsGroupsParsed[index].groupName})
                          }
                });

    console.log(finalItemsOnStorage);  

    setDataOnDevice(finalItemsOnStorage);   
  }
} catch (err) {
  alert(err);
}
  };

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

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