简体   繁体   English

JS。 如何为每个道具复制数组 object

[英]JS. How to copy array object per each prop

Limitations:限制:

  1. I am limited to not using ES6 syntax我仅限于使用 ES6 语法
  2. I need a way to do this without knowing the exact properties of the object.在不知道 object 的确切属性的情况下,我需要一种方法来做到这一点。 So without manually adding id , and name when pushing to the desiredOutput array, but rather copying all the properties automatically.因此,在推送到所需的desiredOutput数组时无需手动添加idname ,而是自动复制所有属性。

I have: an array with one object which has multiple, unique, props我有:一个带有一个 object 的数组,它有多个唯一的props

var items = [
  {
    id: 1,
    name: "item name",
    props: [
      {
        propId: 1,
        propName: "name one"
      }, 
      {
        propId: 2,
        propName: "name two"
      },
      {
        propId: 3,
        propName: "name three"
      }
    ]
  }
];

Desired outcome: a new array that contains one copy of the original object inside the array, per each prop期望的结果:一个新数组,其中包含数组内原始 object 的一个副本,每个prop

var desiredOutput = [
    {
      id: 1,
      name: "item name",
      props: [
        {
          propId: 1,
          propName: "name one"
        }
      ]
    },
    {
      id: 1,
      name: "item name",
      props: [
        {
          propId: 2,
          propName: "name two"
        }
      ]
    },
    {
      id: 1,
      name: "item name",
      props: [
        {
          propId: 3,
          propName: "name three"
        }
      ]
    }
  ];

 var items = [{ id: 1, name: "item name", props: [ { propId: 1, propName: "name one" }, { propId: 2, propName: "name two" }, { propId: 3, propName: "name three" } ] }]; var desiredOutput = []; for (var index = 0; index < items.length; index++) { for(var innerIndex = 0; innerIndex < items[index].props.length; innerIndex++) { desiredOutput.push({ id: items[index].id, name: items[index].name, props: [items[index].props[innerIndex]] }) } } console.log(desiredOutput);

just another way of getting the desired output with deep copy.只是另一种通过深拷贝获得所需 output 的方法。

var items = [{
  id: 1,
  name: "item name",
  props: [{
    propId: 1,
    propName: "name one"
  }, {
    propId: 2,
    propName: "name two"
  }, {
    propId: 3,
    propName: "name three"
  }]
}];

var desiredOutput = [];

function ExtractDataFromProp(jsObject, jsObjectPropKey) {
    if (Object.hasOwnProperty.call(jsObject, jsObjectPropKey)) {
        const propElementValue = jsObject[jsObjectPropKey];
        if (typeof propElementValue != "object") {
            const propString = `"${jsObjectPropKey}" : ${isNaN(propElementValue) ? ("\"" + propElementValue + "\"") : propElementValue}`;
            return propString;
        }
        return null;
    }
}

items.forEach((item) => {
    var itemObjArray = [];
    var itemProps = [];

    for (const itemKey in item)
        itemObjArray.push(ExtractDataFromProp(item, itemKey));

    item.props.forEach((prop) => {
        for (const propKey in prop)
            itemProps.push(ExtractDataFromProp(prop, propKey));

        var finalObject = `{ ${itemObjArray.join(',').replace(/,\s*$/, "")}, "props": { ${itemProps.join(',').replace(/,\s*$/, "")} } }` //

        desiredOutput.push(JSON.parse(finalObject))

    });
});

console.dir(desiredOutput);

You can use something like this:你可以使用这样的东西:

function copyProps(items) {
    var copy = [], i=0;
    items.forEach(function(item) {
        item.props.forEach(function(prop) {
            copy[i++] = { id: item.id, name: item.name, prop: [Object.assign({}, prop)]}
        })
    });
    return copy;
}

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

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