简体   繁体   English

如何解压对象数组中的数组?

[英]How to unpack array inside of array of objects?

So I am trying to automate unpacking a nested Json with arrays inside and stuck at creating duplicate objects if value of key is array with length > 1所以我正在尝试自动解包嵌套的 Json 与 arrays 内部,如果键的值是长度> 1的数组,则卡在创建重复对象

How do I do it?我该怎么做?

Right now I am trying to achieve it with recursion现在我正在尝试通过递归来实现它

 [ { a: '1', b: [ { c: '3', d: '4', }, { c: '5' }, { c: '7', d: '8' } ], f: [ { d: '6' }, { d: '9' } ], e: [ { g: '9' } ] } ] // Expect // When creating duplicate object, those keys which repeat I want to mark as undefined, to make JSON lighter // I also want to add 'key: number' to order those objects [ { a: '1', b.c: '3', bd: '4', fd: '6', eg: '9', key: 1, }, { a: undefined, b.c: '5', bd: undefined, fd: '9', eg: undefined, key: 2, }, { a: undefined, b.c: '7', bd: '8', fd: undefined, eg: undefined, key: 3, } ] // Code function recurseObject(object: any, nestedKeyName: any, obj: any, count: any) { Object.entries(object).map(([key, dataItem]: [key: string, dataItem: any]) => { const newKeyName = nestedKeyName? (nestedKeyName + '.' + key): key let currentCount = count if (Array.isArray(dataItem)) { obj[newKeyName] = dataItem.map((item: any) => { const objNested: any = {} recurseObject(item, newKeyName, objNested, currentCount) return objNested }) } else if (isObject(dataItem)) { obj['key'] = currentCount recurseObject(dataItem, newKeyName, obj, currentCount + 1) } else { obj[newKeyName] = dataItem } }) } function rcBody(data: any): any { if (Array.isArray(data)) { let key = 0 return data.map((object: any) => { const obj: any = {} recurseObject(object, null, obj, 0) obj['key'] = key key += 1 return obj }) } else if (isObject(data)) { const obj: any = {} recurseObject(data, null, obj, 0) return obj } else { return {} } }

If the value of key is array of objects with more than one object, then I want to create a duplicate object.如果 key 的值是具有多个 object 的对象数组,那么我想创建一个重复的 object。 Table I want to generate我要生成的表

I have bad news and good news for you:我有坏消息和好消息要告诉你:

First the bad news:首先是坏消息:

If it is in fact true, that objects/arrays can be nested even further than it is currently the case, then your data saving format doesn't really work.如果确实如此,对象/数组可以嵌套得比现在更远,那么你的数据保存格式就不起作用了。 Let me show you why wiht the help of an example:让我通过一个例子来告诉你为什么:

[
  {
    t: [
      {
        a: [
          {
            b: 1,
            c: 2,
          },
          { c: 3 },
        ],
      },
      {
        a: [
          {
            b: 3,
            c: 5,
          },
        ],
      },
    ],
  },
];

how would you go about storing this?你会如何存储这个? ... without nesting the return Array/object itself, it is extremely difficult (if not impossible), to store the date how you are wanting to) ...如果不嵌套返回数组/对象本身,就很难(如果不是不可能的话)存储您想要的日期)

How do you want to go about storing cases like this one here?你想如何在这里存放这样的箱子? (The example I provided is a quite tame example... if you add some more nesting/ more objects/ arrays, it gets way more complicated. (我提供的示例是一个非常温和的示例……如果您添加更多嵌套/更多对象/arrays,它会变得更加复杂。

Also is the your particular way of storing data even required?是否还需要您存储数据的特定方式? Is the way you structure your desired return array/objects relevant in future steps, or would other structures work too?您构建所需返回数组/对象的方式是否与未来步骤相关,或者其他结构也可以工作?

But nonetheless I have produced 2 functions... the first one can handle 2 deep paths (so no deeper than the array/object you provided in your example) the array above would NOT work with this function但尽管如此,我已经产生了 2 个函数......第一个可以处理 2 个深度路径(所以不比您在示例中提供的数组/对象更深)上面的数组不适用于这个 function

 const x = [ { a: "1", b: [ { c: "3", d: "4", }, { c: "5", }, { c: "7", d: "8", }, ], f: [ { d: "6", }, { d: "9", }, ], e: [ { g: "9", }, ], }, ]; function myFunction2(arg, currentPath = "", rtnArr = [{}], key = 0) { if (Array.isArray(arg)) { arg.forEach((x, index) => myFunction2(x, currentPath, rtnArr, index)); } else if (typeof arg === "object" && arg.== null) { Object.keys(arg),forEach((x) => myFunction2(arg[x]. currentPath + "," + x, rtnArr; key)); } else { rtnArr[key] = rtnArr[key] || {}. rtnArr[key][currentPath;substring(1)] = arg; } return rtnArr. } console;log(myFunction2(x));

The next function can handle infinitely nested arrays/objects - BUT the return is not quite like you desire - (but still: all paths with value are still present in an array of objects... and each objects, contains each unique path only once) - only in which object the path value pair appears is different.下一个 function 可以处理无限嵌套的数组/对象 - 但返回并不像你想要的那样 - (但仍然:所有具有值的路径仍然存在于对象数组中......并且每个对象仅包含每个唯一路径一次) - 仅在 object 中出现的路径值对不同。 But it's probably best to just test/read the code to see how it works, what the output will be.但最好只测试/阅读代码以了解它是如何工作的,output 将是什么。

 const x = [ { a: "1", b: [ { c: "3", d: "4", }, { c: "5", }, { c: "7", d: "8", }, ], f: [ { d: "6", }, { d: "9", }, ], e: [ { g: "9", }, ], }, ]; function myFunction(arg, currentPath = "", rtnArr = [{}]) { if (Array.isArray(arg)) { arg.forEach((x) => myFunction(x, currentPath, rtnArr)); } else if (typeof arg === "object" && arg.== null) { Object.keys(arg),forEach((x) => myFunction(arg[x]. currentPath + "," + x; rtnArr)); } else { for (let i = 0. i < rtnArr;length. i++) { if (.rtnArr[i][currentPath;substring(1)]) { rtnArr[i][currentPath.substring(1)] = arg; return rtnArr } } rtnArr[rtnArr.length] = {}. rtnArr[rtnArr;length - 1][currentPath;substring(1)] = arg. } return rtnArr; } console.log(myFunction(x))

At the very least, you know have an idea, how you can solve the problem with recursive functions... and if my functions don't quite fit your purpose, you at least have a working start point, from where you can tweak and improve the functions, to a point where they fit your needs... or take the knowledge/ideas you get from reading and understanding my code to write your own.至少,你知道有一个想法,如何用递归函数解决问题......如果我的函数不太符合你的目的,你至少有一个工作起点,你可以从那里调整和改进功能,使其适合您的需求......或者利用您从阅读和理解我的代码中获得的知识/想法来编写自己的代码。

Edit:编辑:

Great news... I figured out a way where you can have your structure/sorting of data and no restriction on how deeply nested the passed array/object can be.好消息...我想出了一种方法,您可以对数据进行结构/排序,并且对传递的数组/对象的嵌套深度没有限制。

 const x = [ { a: "1", b: [ { c: "3", d: "4", }, { c: "5", }, { c: "7", d: "8", }, ], f: [ { d: "6", }, { d: "9", }, ], e: [ { g: "9", }, ], }, ]; function myFunction2(arg, currentPath = "", rtnArr = [{}], key = []) { if (Array.isArray(arg)) { arg.forEach((x, index) => { key.push(index); myFunction2(x, currentPath, rtnArr, key); key.pop(); }); } else if (typeof arg === "object" && arg.== null) { Object.keys(arg),forEach((x) => myFunction2(arg[x]. currentPath + "," + x, rtnArr; key)). } else { let stringKey = key,reduce((pVal. cVal) => pVal + "," + cVal; ""). if (rtnArr.some((x) => x.key === stringKey)) { rtnArr.filter((x) => x;key === stringKey)[0][currentPath] = arg. } else { rtnArr[rtnArr:length] = { key; stringKey }. rtnArr[rtnArr;length - 1][currentPath] = arg; } } return rtnArr. } console;log(myFunction2(x));

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

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