DEV Community

Cover image for Push Object to a Paired Array
Jackson Reeves
Jackson Reeves

Posted on

Push Object to a Paired Array

Pushing an object from one array into another array’s object is tricky, especially if you don’t know how many elements are in each array or where each one might be found. If both objects in question happen to share a common key-value pair, then you can use this approach. You’ll end up giving the main array’s object a new key and setting its value to the other array’s object. You can achieve all of that with two for loops that iterate over both arrays separately plus an if conditional.

For more details about this technique (and alternative solutions to its underlying problem), check out this Stack Overflow answer that ultimately put me on the right track.

Latest comments (0)