So I have an array called 'products' which contains objects and each object has a unique 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.
The 'productsAdditionalInfo' may have many objects inside an array but will always have the same owner_id relative to that array.
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. 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>
product[index]inside the each loop is wrong -productis one of the elements ofproductsAdditionalInfoat this point already; andindexis its index inside of that. Eachproductitself is an array of objects again though, so you probably need a second, nested loop at this point.