Javascript - pull values of an element with matching property in one array and save to another array -
Send me 2 arrays, sendInstalls and sendUsers. Both arrays have a property called objId with values matching two arrays, arrays are not sorted in a particular order
What are your requirements?
- For each element of the sendUsers array, find the element in the sendInstalls array with a matching ObjId property
- Return the value of the DEVTOKEN property of that element in sendInstalls
I tried the following:
However, console.log (matchInstall) returns line of 'undefined' instead of line mailing array index.
The data is not pasted because this user is sensitive, but is there anything obvious immediately that is wrong with the above? If the data is needed to resolve this, I can try and type a part and update this question.
Any help greatly appreciated.
Cheers
Now you do not mention the structure of your two arrays in your question But assuming that you have defined it correctly and coded it, you probably need to determine the function findwithAttr
loop where sendUsers cross out
array Like (unrated code):
function findWithAttr (array, attr, value) {For (var i = 0; i & lt; array.length; i + = 1) {if (Array [i] [attr] === value) {return i; }; }; }; SendUsers.forEach (function (theSentUser) {var matchInstall = findWithAttr (sendInstalls, 'DEVTOKEN', theSentUser.objId); console.log (matchInstall); theSentUser.objToken = sendInstalls [matchInstall] .devToken;});
You were using the function inside the loop and was using it which was probably causing trouble. I hope this will start you in the right direction.
Comments
Post a Comment