简体   繁体   English

循环遍历 arrays 数组中的每个 object 找到 id,将其与数组中的另一个 object 匹配并将它们连接在一起

[英]Loop through each object in a array of arrays find the id, match it to another object in an array and concatenate them together

So I have an array called 'products' which contains objects and each object has a unique id.所以我有一个名为“products”的数组,其中包含对象,每个 object 都有一个唯一的 ID。 I also have another array called 'productsAdditionalInfo' which contains additional info relating to 'products' and each object has a owner_id which matches to one of the objects in the 'products' array.我还有另一个名为“productsAdditionalInfo”的数组,其中包含与“产品”相关的附加信息,每个 object 都有一个与“产品”数组中的一个对象匹配的 owner_id。 The 'productsAdditionalInfo' may have many objects inside an array but will always have the same owner_id relative to that array. 'productsAdditionalInfo' 可能在一个数组中包含许多对象,但相对于该数组,它始终具有相同的 owner_id。

I'm, trying to loop through the 'productsAdditionalInfo' array get the 'owner_id' and match it to the 'id' in the products array then add the namespace as a key and the value as the value and then return it as a new array.我正在尝试遍历“productsAdditionalInfo”数组获取“owner_id”并将其与 products 数组中的“id”相匹配,然后将命名空间添加为键,将值添加为值,然后将其作为新的返回大批。 However, it appears I'm stuck on the first hurdle.但是,看来我被困在第一个障碍上。 I get the first key of "Duration" in the console then I get 'namespace' of undefined.我在控制台中得到“持续时间”的第一个键,然后得到未定义的“命名空间”。

I hope this makes sense if not I've commented out an example of how it's supposed to be.我希望这是有道理的,如果不是我已经注释掉了一个应该如何的例子。 I appreciate any help.我很感激任何帮助。

Edit: Some people have suggested that this is similar/the same as another question where you merge them together but I believe my question is different since I'm not completely merging them together just certain bits of info.编辑:有些人建议这与将它们合并在一起的另一个问题相似/相同,但我相信我的问题不同,因为我并没有将它们完全合并在一起只是某些信息。

 let products = [ {id: 102, type: "toy"}, {id: 245, type: "food"}, {id: 312, type: "clothes"} ] let productsAdditionalInfo = [ [{namespace: "Duration", value: 5, owner_id: 245}, {namespace: "effect", value: "Tail", owner_id: 245}], [{namespace: "Supplier", owner_id: 312, value: "rev"}], [{namespace: "amount", value: 0, owner_id: 102}, {namespace: "effect", value: "plush", owner_id: 102}] ] $.each(productsAdditionalInfo, function( index, product ) { console.log( product[index].namespace ); }); // let whatItShouldBe = [ {id: 102, type: "toy", amount: 0, effect: "plush" }, {id: 245, type: "food", Duration: 5, effect: "Tail"}, {id: 312, type: "clothes", Supplier: "rev"} ]
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

One way to achieve this would be to loop through each product, retrieving the associated productAdditionsInfo based on the owner_id .实现此目的的一种方法是遍历每个产品,根据owner_id检索关联的productAdditionsInfo From there you can create an object from the additions and merge the two together, something like this:从那里您可以从添加创建一个 object 并将两者合并在一起,如下所示:

 let products = [{id: 102, type: "toy"}, {id: 245, type: "food"}, {id: 312, type: "clothes"}] let productsAdditionalInfo = [ [{namespace: "Duration", value: 5, owner_id: 245}, {namespace: "effect", value: "Tail", owner_id: 245}], [{namespace: "Supplier", owner_id: 312, value: "rev"}], [{namespace: "amount", value: 0, owner_id: 102}, {namespace: "effect", value: "plush", owner_id: 102}] ] let merged = products.map(p => { var info = productsAdditionalInfo.filter(i => i[0].owner_id == p.id)[0].map(i => ({ [i.namespace]: i.value })); info.push(p); return Object.assign(...info); }); console.log(merged);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

This is assuming that the second-level arrays within productsAdditionalInfo are always grouped by the same owner_id .这是假设productsAdditionalInfo中的第二级 arrays始终按相同的owner_id分组。

 let products = [ {id: 102, type: "toy"}, {id: 245, type: "food"}, {id: 312, type: "clothes"} ] let productsAdditionalInfo = [ [{namespace: "Duration", value: 5, owner_id: 245}, {namespace: "effect", value: "Tail", owner_id: 245}], [{namespace: "Supplier", owner_id: 312, value: "rev"}], [{namespace: "amount", value: 0, owner_id: 102}, {namespace: "effect", value: "plush", owner_id: 102}] ] const flatPAI = productsAdditionalInfo.flat(); console.log( products.map( p => Object.assign(p, ...flatPAI.filter( info => info.owner_id == p.id ).map( info => ({[info.namespace]: info.value}))) ) ); // let whatItShouldBe = [ {id: 102, type: "toy", amount: 0, effect: "plush" }, {id: 245, type: "food", Duration: 5, effect: "Tail"}, {id: 312, type: "clothes", Supplier: "rev"} ]
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

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