JavaScript在多维对象中把两个数组映射为一个数组

问题描述 投票:0回答:1

我需要把两个独立的对象数组,映射到一个新的对象数组中。 但我似乎无法在ES6中解决这个问题。我知道我需要通过第一个数组进行映射,然后在第二个数组上进行缩减,但我开始迷茫了。 我附上了一个代码段,它在减少时失败了。Combined: acc[i].Combined.push(name.SId),

两个数组。

阵列1

[
    {
      "BId": 1,
      "Name": "Test1",
    },
    {
      "BId": 2,
      "Name": "Test2",
    }
]

阵列2

[
    {
      "Id": 1,
      "BId": 1,
      "SId": 2,
      "GId": 1
    },
    {
      "Id": 2,
      "BId": 1,
      "SId": 2,
      "GId": 2
    },
    {
      "Id": 3,
      "BId": 1,
      "SId": 3,
      "GId": 1
    },
    {
      "Id": 1,
      "BId": 2,
      "SId": 2,
      "GId": 1
    },
    {
      "Id": 2,
      "BId": 2,
      "SId": 2,
      "GId": 2
    },
    {
      "Id": 3,
      "BId": 2,
      "SId": 3,
      "GId": 1
    },
    {
      "Id": 3,
      "BId": 2,
      "SId": 3,
      "GId": 4
    },
]

的结果。

[
    {
      "BId": 1,
      "Name": Test1,
      "Combined": [{SId:2, GIds[1,2]}, {SId:3, GIds[1]}]
    }
    {
      "BId": 2,
      "Name": Test2,
      "Combined": [{SId:2, GIds[1,2]}, {SId:3, GIds[1,4]}]
    }
]

const array1 = [
        {"BId": 1,
          "Name": "Test1",
        },
        {
          "BId": 2,
          "Name": "Test2",
        }
    ];
    
const array2 = [
        {
          "Id": 1,
          "BId": 1,
          "SId": 2,
		  "GId": 1
        },
        {
          "Id": 2,
          "BId": 1,
          "SId": 2,
		  "GId": 2
        },
        {
          "Id": 3,
          "BId": 1,
          "SId": 3,
		  "GId": 1
        },
		{
          "Id": 1,
          "BId": 2,
          "SId": 2,
		  "GId": 1
        },
        {
          "Id": 2,
          "BId": 2,
          "SId": 2,
		  "GId": 2
        },
        {
          "Id": 3,
          "BId": 2,
          "SId": 3,
		  "GId": 1
        },
        {
          "Id": 3,
          "BId": 2,
          "SId": 3,
		  "GId": 4
        },
    ];
    
    
     const all = array1.map(a1 => {
      combined = array2
        .filter(a2 => a2.BId === a1.BId)
        .reduce((acc, name, i) => {

          const obj = {
            BId: name.BId,
            Name: name.Name,
            Combined: acc[i].Combined.push(name.SId),
          };
          acc.push(obj);

          return acc;
        }, []);
      return bv;
    });
javascript typescript multidimensional-array
1个回答
1
投票

希望能帮到你...

const array1 = [
        {"BId": 1,
          "Name": "Test1",
        },
        {
          "BId": 2,
          "Name": "Test2",
        }
    ];
    
const array2 = [
        {
          "Id": 1,
          "BId": 1,
          "SId": 2,
		  "GId": 1
        },
        {
          "Id": 2,
          "BId": 1,
          "SId": 2,
		  "GId": 2
        },
        {
          "Id": 3,
          "BId": 1,
          "SId": 3,
		  "GId": 1
        },
		{
          "Id": 1,
          "BId": 2,
          "SId": 2,
		  "GId": 1
        },
        {
          "Id": 2,
          "BId": 2,
          "SId": 2,
		  "GId": 2
        },
        {
          "Id": 3,
          "BId": 2,
          "SId": 3,
		  "GId": 1
        },
        {
          "Id": 3,
          "BId": 2,
          "SId": 3,
		  "GId": 4
        },
    ];
            
const all = array1.map(arr => {
  let combined = []
  array2
  .filter(e => arr.BId === e.BId)
  .forEach(e => {
    const {GId, SId} = e
    combined.push({GId, SId})
  })
  combined = combined.reduce((acc, cur) => {
  const {GId, SId} = cur
  const idx = acc.findIndex(e => e.SId === SId)
  if (idx !== -1) {acc[idx].GId.push(GId)}
  else {acc.push({SId, GId: [GId]})}
  return acc
  },[])
  return {...arr, combined}
 });
 
 console.log(all)

1
投票

let a_array = [
    {
      "BId": 1,
      "Name": "Test1",
    },
    {
      "BId": 2,
      "Name": "Test2",
    }
];

let b_array = [
    {
      "Id": 1,
      "BId": 1,
      "SId": 2,
      "GId": 1
    },
    {
      "Id": 2,
      "BId": 1,
      "SId": 2,
      "GId": 2
    },
    {
      "Id": 3,
      "BId": 1,
      "SId": 3,
      "GId": 1
    },
    {
      "Id": 1,
      "BId": 2,
      "SId": 2,
      "GId": 1
    },
    {
      "Id": 2,
      "BId": 2,
      "SId": 2,
      "GId": 2
    },
    {
      "Id": 3,
      "BId": 2,
      "SId": 3,
      "GId": 1
    },
    {
      "Id": 3,
      "BId": 2,
      "SId": 3,
      "GId": 4
    },
];

a_array.map( (a_object) => {
 if(!a_object.Combined) a_object.Combined = [];

 return b_array.map((b_object) => {

  if(a_object.BId === b_object.BId) {

   let c = a_object.Combined.filter( (_c) => ( _c.SId === b_object.SId ) );

   if(c && c[0]) {
    if(!c[0].GIds.includes(b_object.GId)) c[0].GIds.push(b_object.GId);

   } else {
    a_object.Combined.push({"SId": b_object.SId, "GIds": [b_object.GId]})

   }

   return a_object;

  }

 })

} )

console.log(a_array);

1
投票

我可能会写这样的东西。

const combine = (arr1, arr2) => arr1 .map (({BId, ...rest}) => ({
  BId, 
  ...rest,
  Combined: Object .entries (
      arr2 .filter (({BId: b}) => b == BId)
           .map (({SId, GId}) => ({SId, GId}))
           .reduce ((a, {SId, GId}) => ({...a, [SId]: [...(a [SId] || []), GId]}), {})
  ) .map (([sid, GIds]) => ({SId: Number (sid), GIds}))
}))


const arr1 = [{BId: 1, Name: "Test1"}, {BId: 2, Name: "Test2"}]
const arr2 = [{Id: 1, BId: 1, SId: 2, GId: 1}, {Id: 2, BId: 1, SId: 2, GId: 2}, {Id: 3, BId: 1, SId: 3, GId: 1}, {Id: 1, BId: 2, SId: 2, GId: 1}, {Id: 2, BId: 2, SId: 2, GId: 2}, {Id: 3, BId: 2, SId: 3, GId: 1}, {Id: 3, BId: 2, SId: 3, GId: 4}]


console .log (combine (arr1, arr2))
.as-console-wrapper {min-height: 100% !important; top: 0}

我想这是相当直接的。 我想这是相当简单的。BIdName 属性从第一个数组中获取(使用 ...rest 以包括其他属性(如果有的话)。) 我们计算出 Combined 像这样。

  • The filter 行找到匹配的条目。

  • 第一条 map 行提取我们关心的两个属性。

  • reduce 行将它们归为一个结构,如 {"2": [1, 2], "3": [1]}.

  • 我们称之为 Object.entries 在这一点上回过头来 [["2", [1, 2]], ["3", [1]]].

  • 然后是第二个 map 行将其变成 [{SId: 2, GIds: [1, 2]}, {SId: 3, GIds: [1]}). 因为我们用的是 SId 作为一个键,它被变成了一个字符串。 我们用 Number 在这里把它转换回来。

© www.soinside.com 2019 - 2024. All rights reserved.