reactjs 复制对象数组

vi4fp9gy  于 2022-11-22  发布在  React
关注(0)|答案(2)|浏览(195)

假设我们有一个对象数组,如下所示:

const example = [
  { id: "1", childIds: ["2"], parentId: "0" },
  { id: "2", childIds: ["3", "4"], parentId: "1" },
  { id: "3", childIds: [], parentId: "2" },
  { id: "4", childIds: ["5", "6"], parentId: "2" },
  { id: "5", childIds: [], parentId: "4" },
  { id: "6", childIds: [], parentId: "4" },
];

问题:通过生成新ID复制此对象。
ID可以是不同的,假设我们使用第三方ID生成器函数来处理它,但是新的ID应该匹配以保持父子关系。
那么,你的JS函数应该如何解决这个问题呢?
预期结果:

const copied = [
  { id: "10", childIds: ["20"], parentId: "0" },
  { id: "20", childIds: ["30", "40"], parentId: "10" },
  { id: "30", childIds: [], parentId: "20" },
  { id: "40", childIds: ["50", "60"], parentId: "20" },
  { id: "50", childIds: [], parentId: "40" },
  { id: "60", childIds: [], parentId: "40" },
];

这是我所尝试的(获取错误:TypeError:无法指派给对象'#'的只读属性'parentId')

const copied = [];

      example.forEach((child) => {
        const newId = generateId();

        // check if this child is parent of other elements, then update parentIds
        const childrenOfThisChild = example.filter(
          (el) => el.parentId === child.id
        );
        if (childrenOfThisChild.length) {
          childrenOfThisNode.forEach(
            (child) => (child = { ...child, parentId: newId })
          );
        }

        // check if this child is child of other elements, then update childIds
        example.forEach((el) => {
          const index = el.childIds.indexOf(child.id);
          if (index !== -1) {
            el.childIds[index] = newId;
          }
        });

        // add the currently iterated object to new copied array
        copied.push({ ...child, id: newId });
      });
yjghlzjz

yjghlzjz1#

循环数组一次,更改对象的所有id(并将更改的id保存为查找对象中的id)。然后再次循环,根据查找对象更改所有childId和parentId。

const example = [
  { id: "1", childIds: ["2"], parentId: null },
  { id: "2", childIds: ["3", "4"], parentId: "1" },
  { id: "3", childIds: [], parentId: "2" },
  { id: "4", childIds: ["5", "6"], parentId: "2" },
  { id: "5", childIds: [], parentId: "4" },
  { id: "6", childIds: [], parentId: "4" },
];

const newIds = [10, 20, 30, 40, 50, 60, 70]

//I'm just gonna make the assumption, that you are getting an array of unique IDs from the 3rd party
function replaceIds (array, idsArray) {
  const lookup = {};
  
  const retArr = array.map(obj => {return { ...obj }})
  
  retArr.forEach((obj, index) => {
    lookup[obj.id] = idsArray[index]
    obj.id = idsArray[index]
  })
  
  retArr.forEach(obj => {
    obj.parentId = lookup[obj.parentId] || null;
    obj.childIds = obj.childIds.map(id => lookup[id])
  })
  return retArr
}

console.log(replaceIds(example, newIds))

如果您对newIDs数组的要求有困难,您可以使用如下代码轻松创建一个数组:

const IdArray = newArray(example.length)
IdArray.map(x => generateId())
oo7oh9g9

oo7oh9g92#

您可以尝试以下操作:

function generateId() {
  return Math.floor(Math.random() * 100) + "";
}

const example = [
  { id: "1", childIds: ["2"], parentId: "0" },
  { id: "2", childIds: ["3", "4"], parentId: "1" },
  { id: "3", childIds: [], parentId: "2" },
  { id: "4", childIds: ["5", "6"], parentId: "2" },
  { id: "5", childIds: [], parentId: "4" },
  { id: "6", childIds: [], parentId: "4" },
];

var copied = [];
example.forEach((ex, i) => {
    const parentId = i === 0 ? "0" : copied[parseInt(i / 2)].id;
    copied.push({
        id: generateId(),
        childIds: Array.from({ length: ex.childIds.length }).map(() => generateId()),
        parentId
    });
});

相关问题