简体   繁体   English

如何将一个包含数组的 object 拆分为多个对象

[英]How to split one object with an array in it to multiple objects

I want to know if there is it is possible to split up an object into multiple objects.我想知道是否可以将 object 拆分为多个对象。 I have an array of objects that has another array in it and I was wondering if it is possible to split those objects for each object in the inner array.我有一个对象数组,其中包含另一个数组,我想知道是否可以为内部数组中的每个 object 拆分这些对象。 Something like:就像是:

obj1 = [{
  a: 1,
  b: [{c: 2},{d: 3}],
  e: 4
}]

to

obj2 =[
  {
    a: 1,
    b: [{c: 2}],
    e: 4
  },
  {
    a: 1,
    b: [{d: 3}],
    e: 4
  }
]

The object is always in this form, whether it has one object or hundreds. object始终是这种形式,不管是一个object还是几百个。 While some objects may have more fields in them, there is only one field with an array.虽然某些对象中可能有更多字段,但只有一个字段带有数组。 Currently, I am mapping across the original array and then mapping again inside the b array to get to each object in there.目前,我正在映射原始数组,然后在b数组中再次映射以到达其中的每个 object。 However, I do not know where to go from there as the return object from that map would just be the original array.但是,我不知道 go 从那里到哪里,因为从 map 返回的 object 将只是原始数组。 I do not know how to split the b array and map it with the original one.我不知道如何将b阵列和 map 与原始阵列分开。 I thought of {...orig, b: map()} but I don't think it will work with every object我想到了 {...orig, b: map()} 但我认为它不适用于每个 object

You were on the right track according to the description in your post.根据您帖子中的描述,您走在正确的轨道上。 You have to loop through your source object, and inside each iteration, loop through your b array to extract each element and push it with the source iteration element in a new object into a target array.您必须遍历源 object,并在每次迭代中循环遍历b数组以提取每个元素,并将其与新 object 中的源迭代元素一起推送到目标数组中。

 var source = [{ a: 1, b: [{ c: 2 }, { d: 3 }], e: 4 }]; // define target as an array var target = []; // loop through source source.forEach((srcElement) => { // loop through `b` array attribute srcElement.b.forEach((bElement) => { // push object into target with source element attributes // and current `b` element wrapped into an array target.push({...srcElement, b: [bElement] }); }); }); console.log(target);

NOTE: this solution assumes that on each iteration on your source object, the b attribute exists and is of type Array .注意:此解决方案假定在您的源 object 的每次迭代中, b属性存在并且是Array类型。

暂无
暂无

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

相关问题 如何将数组中的 object 拆分为多个对象? - How to split an object within an array into multiple objects? 如何根据其中一个值中的数据将对象拆分为多个对象[数组中的唯一值] - How to split an object into multiple objects based on data in one of it's values[unique values in an array] 如何有效地将 object 拆分为数组中的多个对象 - how to split an object to multiple objects inside an array efficiently 根据属性将一个对象拆分为多个对象 - split one object into multiple objects based on the property 如何连接/拆分实际上是一个对象的对象 - How to join/split objects that are one object in fact 如何将一个对象数组拆分成多个普通数组,其中每个数组都由一个属性的值填充? - How to split an array of objects into multiple normal arrays where each array is populated by one property's values? 如何在Javascript中使用多个对象拆分数组的索引 - How to split index of array with multiple objects in Javascript 如何通过子值将对象数组拆分为多个对象数组 - how to split array of objects into multiple array of objects by subvalue 如何使用这些键及其值将具有一个 object 和多个键的数组转换为多个对象的数组? - How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values? 如何将多个对象依次推入一个对象 - how to push multiple objects into one object serially
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM